git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Syntax highlighting for gitweb

From
ESEd Schouten <ed@fxq.nl>
Date
Feb 26, 2007, 18:18 UTC
Message-ID
<20070226181858.GC11232@hoeg.nl>
Hello everyone,
I just wrote a really awful patch to add syntax highlighting to gitweb:

%%% --- gitweb Mon Feb 26 18:36:42 2007 +++ gitweb Mon Feb 26 19:12:20 2007

@@ -16,6 +16,7 @@
 use Fcntl ':mode';
 use File::Find qw();
 use File::Basename qw(basename);
+use IPC::Open2;
 binmode STDOUT, ':utf8';
 
 our $cgi = new CGI;
@@ -3186,15 +3187,25 @@
 	git_print_page_path($file_name, "blob", $hash_base);
 	print "<div class=\"page_body\">\n";
 	my $nr;
+
+	my $pid;
+	local (*HLR, *HLW);
+	$pid = open2(\*HLR, \*HLW, "highlight -f --syntax c");
 	while (my $line = <$fd>) {
-		chomp $line;
-		$nr++;
-		$line = untabify($line);
-		printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
-		       $nr, $nr, $nr, esc_html($line, -nbsp=>1);
+		print HLW $line;
 	}
+	close HLW
+		or print "Failed to start highlight.\n";
 	close $fd
 		or print "Reading blob failed.\n";
+
+	while (my $line = <HLR>) {
+		$nr++;
+		printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
+		       $nr, $nr, $nr, $line;
+	}
+	close HLR
+		or print "Failed to highlight blob.\n";
 	print "</div>";
 	git_footer_html();
 }
%%%

I am not a Perl programmer, so I kept it really simple by just
hardcoding '--syntax c'. Would it be possible for some real Perl
programmer to clean this up? I think a feature like this would be really
cool to have.

Yours,
-- 
 Ed Schouten <ed@fxq.nl>
 WWW: http://g-rave.nl/
Next: Johannes Schindelin
Message 1 of 3 in “Syntax highlighting for gitweb”
  1. Ed SchoutenFeb 26, 2007
  2. Johannes SchindelinFeb 26, 2007
  3. Jakub NarebskiFeb 27, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.