@matheusbsbs / matheus-bagliano / commits / b331fe5476
Fork 0

gitweb: Syntax highlighting support

It requires the 'highlight' program to do all the heavy-lifting. This is loosely based on Daniel Svensson's and Sham Chukoury's work in gitweb-xmms2.git (it cannot be cherry-picked, as gitweb-xmms2 first forked wildly, then not contributed back, and then went stale). [jn: cherry picked from bc1ed6aafd9ee4937559535c66c8bddf1864bec6 in http://repo.or.cz/w/git/dscho.git, with a few changes] Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Jakub Narebski <jnareb@gmail.com> Acked-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 27, 2010 at 21:34 UTC b331fe54762770c647b8f95c8f5b16004c6120da
2 files changed +82 -1
gitweb/gitweb.css
+18
@@ -572,3 +572,21 @@ span.match {
572 div.binary {
573 font-style: italic;
574 }
575 +
576 +/* Style definition generated by highlight 2.4.5, http://www.andre-simon.de/ */
577 +
578 +/* Highlighting theme definition: */
579 +
580 +.num { color:#2928ff; }
581 +.esc { color:#ff00ff; }
582 +.str { color:#ff0000; }
583 +.dstr { color:#818100; }
584 +.slc { color:#838183; font-style:italic; }
585 +.com { color:#838183; font-style:italic; }
586 +.dir { color:#008200; }
587 +.sym { color:#000000; }
588 +.line { color:#555555; }
589 +.kwa { color:#000000; font-weight:bold; }
590 +.kwb { color:#830000; }
591 +.kwc { color:#000000; font-weight:bold; }
592 +.kwd { color:#010181; }
gitweb/gitweb.perl
+64 -1
@@ -227,6 +227,36 @@ our %avatar_size = (
227 # Leave it undefined (or set to 'undef') to turn off load checking.
228 our $maxload = 300;
229
230 +# syntax highlighting
231 +our %highlight_type = (
232 + # match by basename
233 + 'SConstruct' => 'py',
234 + 'Program' => 'py',
235 + 'Library' => 'py',
236 + 'Makefile' => 'make',
237 + # match by extension
238 + '\.py$' => 'py', # Python
239 + '\.c$' => 'c',
240 + '\.h$' => 'c',
241 + '\.cpp$' => 'cpp',
242 + '\.cxx$' => 'cpp',
243 + '\.rb$' => 'ruby',
244 + '\.java$' => 'java',
245 + '\.css$' => 'css',
246 + '\.php3?$' => 'php',
247 + '\.sh$' => 'sh', # Bash / shell script
248 + '\.pl$' => 'pl', # Perl
249 + '\.js$' => 'js', # JavaScript
250 + '\.tex$' => 'tex', # TeX and LaTeX
251 + '\.bib$' => 'bib', # BibTeX
252 + '\.x?html$' => 'xml',
253 + '\.xml$' => 'xml',
254 + '\.awk$' => 'awk',
255 + '\.bat$' => 'bat', # DOS Batch script
256 + '\.ini$' => 'ini',
257 + '\.spec$' => 'spec', # RPM Spec
258 +);
259 +
260 # You define site-wide feature defaults here; override them with
261 # $GITWEB_CONFIG as necessary.
262 our %feature = (
@@ -445,6 +475,19 @@ our %feature = (
475 'javascript-actions' => {
476 'override' => 0,
477 'default' => [0]},
478 +
479 + # Syntax highlighting support. This is based on Daniel Svensson's
480 + # and Sham Chukoury's work in gitweb-xmms2.git.
481 + # It requires the 'highlight' program, and therefore is disabled
482 + # by default.
483 +
484 + # To enable system wide have in $GITWEB_CONFIG
485 + # $feature{'highlight'}{'default'} = [1];
486 +
487 + 'highlight' => {
488 + 'sub' => sub { feature_bool('highlight', @_) },
489 + 'override' => 0,
490 + 'default' => [0]},
491 );
492
493 sub gitweb_get_feature {
@@ -5346,6 +5389,7 @@ sub git_blob {
5389 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5390 or die_error(500, "Couldn't cat $file_name, $hash");
5391 my $mimetype = blob_mimetype($fd, $file_name);
5392 + # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
5393 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5394 close $fd;
5395 return git_blob_plain($mimetype);
@@ -5353,6 +5397,25 @@ sub git_blob {
5397 # we can have blame only for text/* mimetype
5398 $have_blame &&= ($mimetype =~ m!^text/!);
5399
5400 + my $have_highlight = gitweb_check_feature('highlight');
5401 + my $syntax;
5402 + if ($have_highlight && defined($file_name)) {
5403 + my $basename = basename($file_name, '.in');
5404 + foreach my $regexp (keys %highlight_type) {
5405 + if ($basename =~ /$regexp/) {
5406 + $syntax = $highlight_type{$regexp};
5407 + last;
5408 + }
5409 + }
5410 +
5411 + if ($syntax) {
5412 + close $fd;
5413 + open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
5414 + "highlight --xhtml --fragment -t 8 --syntax $syntax |"
5415 + or die_error(500, "Couldn't open file or run syntax highlighter");
5416 + }
5417 + }
5418 +
5419 git_header_html(undef, $expires);
5420 my $formats_nav = '';
5421 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
@@ -5404,7 +5467,7 @@ sub git_blob {
5467 $line = untabify($line);
5468 printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
5469 . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5407 - $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5470 + $nr, $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
5471 }
5472 }
5473 close $fd