Skip to content

Commit b331fe5

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jakub Narebski <[email protected]> Acked-by: Petr Baudis <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ddb27a5 commit b331fe5

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

gitweb/gitweb.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,21 @@ span.match {
572572
div.binary {
573573
font-style: italic;
574574
}
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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,36 @@ BEGIN
227227
# Leave it undefined (or set to 'undef') to turn off load checking.
228228
our $maxload = 300;
229229

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+
230260
# You define site-wide feature defaults here; override them with
231261
# $GITWEB_CONFIG as necessary.
232262
our %feature = (
@@ -445,6 +475,19 @@ BEGIN
445475
'javascript-actions' => {
446476
'override' => 0,
447477
'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]},
448491
);
449492

450493
sub gitweb_get_feature {
@@ -5346,13 +5389,33 @@ sub git_blob {
53465389
open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
53475390
or die_error(500, "Couldn't cat $file_name, $hash");
53485391
my $mimetype = blob_mimetype($fd, $file_name);
5392+
# use 'blob_plain' (aka 'raw') view for files that cannot be displayed
53495393
if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
53505394
close $fd;
53515395
return git_blob_plain($mimetype);
53525396
}
53535397
# we can have blame only for text/* mimetype
53545398
$have_blame &&= ($mimetype =~ m!^text/!);
53555399

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+
53565419
git_header_html(undef, $expires);
53575420
my $formats_nav = '';
53585421
if (defined $hash_base && (my %co = parse_commit($hash_base))) {
@@ -5404,7 +5467,7 @@ sub git_blob {
54045467
$line = untabify($line);
54055468
printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
54065469
. "#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);
54085471
}
54095472
}
54105473
close $fd

0 commit comments

Comments
 (0)