Skip to contentSkip to conversation

Syntax highlighting for gitweb

PATCH Ed Schouten 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hello everyone,

I just wrote a really awful patch to add syntax highlighting to gitweb:

%%%

gitweb +16 -5
@@ -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,

Signature · 2 lines
 Ed Schouten <[email protected]>
 WWW: http://g-rave.nl/
#1 lore raw

Hi,

gitweb line 3193

Quoted 5 lines · Ed Schouten

I just wrote a really awful patch to add syntax highlighting to gitweb:

[...]

gitweb +1 -0
+	$pid = open2(\*HLR, \*HLW, "highlight -f --syntax c");

Since this fork()s an external program, which is possibly expensive, or can fail because the program is not there, you should at least

- document it, and
- make it optional.

Ciao,
Dscho

#2 lore raw
Quoted 6 lines · Johannes Schindelin
Quoted 4 lines · Ed Schouten

I just wrote a really awful patch to add syntax highlighting to gitweb:

[...]

Quoted 7 lines
Quoted 1 line

+ $pid = open2(\*HLR, \*HLW, "highlight -f --syntax c");

Since this fork()s an external program, which is possibly expensive, or can fail because the program is not there, you should at least

- document it, and
- make it optional.

It was planned (search for "[RFC] gitweb wishlist and TODO list" in archives), and gitweb-xmms2 (http://git.xmms.se/?p=gitweb-xmms2.git)
has it: see http://git.or.cz/gitwiki/InterfacesFrontendsAndTools#gitweb
but it was postponed together with committags support to have git config reader in Perl to use in gitweb.

Hmmm... perhaps we should use open2 in few places where we use non-list form of magic open "-|" (the one which spawns shell)?

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git