threads / discuss / 6976

Syntax highlighting for gitweb

Subject: Syntax highlighting for gitweb

## tl;dr

3 messages between Feb 26, 2007 and Feb 27, 2007.

replies: 2people: 3as markdown or json

Ed Schouten· Feb 26, 2007, 18:18 UTC · lore
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/
Johannes Schindelin· Feb 26, 2007, 18:55 UTC · re: Ed Schouten · lore

Re: Syntax highlighting for gitweb

Hi,
On Mon, 26 Feb 2007, Ed Schouten wrote:
Show 5 quoted lines
> I just wrote a really awful patch to add syntax highlighting to gitweb:
> 
> [...]
>
> +	$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

Jakub Narebski· Feb 27, 2007, 01:45 UTC · re: Johannes Schindelin · lore

Re: Syntax highlighting for gitweb

Johannes Schindelin wrote:
Show 13 quoted lines
> On Mon, 26 Feb 2007, Ed Schouten wrote:
> 
>> I just wrote a really awful patch to add syntax highlighting to gitweb:
>> 
>> [...]
>>
>> +    $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)?

-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git

← back to recent threads