# Syntax highlighting for gitweb

3 messages from 2007-02-26 to 2007-02-27. Participants: Ed Schouten, Johannes Schindelin, Jakub Narebski.
Thread: https://gitlist.dev/t/6976

## Ed Schouten, 2007-02-26 18:18

Subject: Syntax highlighting for gitweb
Message-ID: <20070226181858.GC11232@hoeg.nl>
URL: https://gitlist.dev/e/20070226181858.GC11232%40hoeg.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/

```

## Johannes Schindelin, 2007-02-26 18:55

Subject: Re: Syntax highlighting for gitweb
Message-ID: <Pine.LNX.4.63.0702261953420.22628@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0702261953420.22628%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <20070226181858.GC11232@hoeg.nl>

```
Hi,

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.

Ciao,
Dscho

```

## Jakub Narebski, 2007-02-27 01:45

Subject: Re: Syntax highlighting for gitweb
Message-ID: <es02au$ocf$1@sea.gmane.org>
URL: https://gitlist.dev/e/es02au%24ocf%241%40sea.gmane.org
In-Reply-To: <Pine.LNX.4.63.0702261953420.22628@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin wrote:

> 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

```
