Merge branch 'jn/gitweb-syntax-highlight'

* jn/gitweb-syntax-highlight:
  gitweb: Refactor syntax highlighting support
  gitweb: Syntax highlighting support
This commit is contained in:
Junio C Hamano 2010-06-13 11:21:37 -07:00
commit a26df4cd2f
3 changed files with 123 additions and 3 deletions

View File

@ -572,3 +572,21 @@ span.match {
div.binary { div.binary {
font-style: italic; font-style: italic;
} }
/* Style definition generated by highlight 2.4.5, http://www.andre-simon.de/ */
/* Highlighting theme definition: */
.num { color:#2928ff; }
.esc { color:#ff00ff; }
.str { color:#ff0000; }
.dstr { color:#818100; }
.slc { color:#838183; font-style:italic; }
.com { color:#838183; font-style:italic; }
.dir { color:#008200; }
.sym { color:#000000; }
.line { color:#555555; }
.kwa { color:#000000; font-weight:bold; }
.kwb { color:#830000; }
.kwc { color:#000000; font-weight:bold; }
.kwd { color:#010181; }

View File

@ -445,6 +445,19 @@ our %feature = (
'javascript-actions' => { 'javascript-actions' => {
'override' => 0, 'override' => 0,
'default' => [0]}, 'default' => [0]},
# Syntax highlighting support. This is based on Daniel Svensson's
# and Sham Chukoury's work in gitweb-xmms2.git.
# It requires the 'highlight' program present in $PATH,
# and therefore is disabled by default.
# To enable system wide have in $GITWEB_CONFIG
# $feature{'highlight'}{'default'} = [1];
'highlight' => {
'sub' => sub { feature_bool('highlight', @_) },
'override' => 0,
'default' => [0]},
); );
sub gitweb_get_feature { sub gitweb_get_feature {
@ -3179,6 +3192,61 @@ sub blob_contenttype {
return $type; return $type;
} }
# guess file syntax for syntax highlighting; return undef if no highlighting
# the name of syntax can (in the future) depend on syntax highlighter used
sub guess_file_syntax {
my ($highlight, $mimetype, $file_name) = @_;
return undef unless ($highlight && defined $file_name);
# configuration for 'highlight' (http://www.andre-simon.de/)
# match by basename
my %highlight_basename = (
#'Program' => 'py',
#'Library' => 'py',
'SConstruct' => 'py', # SCons equivalent of Makefile
'Makefile' => 'make',
);
# match by extension
my %highlight_ext = (
# main extensions, defining name of syntax;
# see files in /usr/share/highlight/langDefs/ directory
map { $_ => $_ }
qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
# alternate extensions, see /etc/highlight/filetypes.conf
'h' => 'c',
map { $_ => 'cpp' } qw(cxx c++ cc),
map { $_ => 'php' } qw(php3 php4),
map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
'mak' => 'make',
map { $_ => 'xml' } qw(xhtml html htm),
);
my $basename = basename($file_name, '.in');
return $highlight_basename{$basename}
if exists $highlight_basename{$basename};
$basename =~ /\.([^.]*)$/;
my $ext = $1 or return undef;
return $highlight_ext{$ext}
if exists $highlight_ext{$ext};
return undef;
}
# run highlighter and return FD of its output,
# or return original FD if no highlighting
sub run_highlighter {
my ($fd, $highlight, $syntax) = @_;
return $fd unless ($highlight && defined $syntax);
close $fd
or die_error(404, "Reading blob failed");
open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
"highlight --xhtml --fragment --syntax $syntax |"
or die_error(500, "Couldn't open file or run syntax highlighter");
return $fd;
}
## ====================================================================== ## ======================================================================
## functions printing HTML: header, footer, error page ## functions printing HTML: header, footer, error page
@ -5380,6 +5448,7 @@ sub git_blob {
open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
or die_error(500, "Couldn't cat $file_name, $hash"); or die_error(500, "Couldn't cat $file_name, $hash");
my $mimetype = blob_mimetype($fd, $file_name); my $mimetype = blob_mimetype($fd, $file_name);
# use 'blob_plain' (aka 'raw') view for files that cannot be displayed
if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) { if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
close $fd; close $fd;
return git_blob_plain($mimetype); return git_blob_plain($mimetype);
@ -5387,6 +5456,11 @@ sub git_blob {
# we can have blame only for text/* mimetype # we can have blame only for text/* mimetype
$have_blame &&= ($mimetype =~ m!^text/!); $have_blame &&= ($mimetype =~ m!^text/!);
my $highlight = gitweb_check_feature('highlight');
my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
$fd = run_highlighter($fd, $highlight, $syntax)
if $syntax;
git_header_html(undef, $expires); git_header_html(undef, $expires);
my $formats_nav = ''; my $formats_nav = '';
if (defined $hash_base && (my %co = parse_commit($hash_base))) { if (defined $hash_base && (my %co = parse_commit($hash_base))) {
@ -5436,9 +5510,8 @@ sub git_blob {
chomp $line; chomp $line;
$nr++; $nr++;
$line = untabify($line); $line = untabify($line);
printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1) printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
. "#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, href(-replay => 1), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
$nr, $nr, $nr, esc_html($line, -nbsp=>1);
} }
} }
close $fd close $fd

View File

@ -647,4 +647,33 @@ test_expect_success \
gitweb_run "p=.git;a=summary"' gitweb_run "p=.git;a=summary"'
test_debug 'cat gitweb.log' test_debug 'cat gitweb.log'
# ----------------------------------------------------------------------
# syntax highlighting
cat >>gitweb_config.perl <<\EOF
$feature{'highlight'}{'override'} = 1;
EOF
highlight --version >/dev/null 2>&1
if [ $? -eq 127 ]; then
say "Skipping syntax highlighting test, because 'highlight' was not found"
else
test_set_prereq HIGHLIGHT
fi
test_expect_success HIGHLIGHT \
'syntax highlighting (no highlight)' \
'git config gitweb.highlight yes &&
gitweb_run "p=.git;a=blob;f=file"'
test_debug 'cat gitweb.log'
test_expect_success HIGHLIGHT \
'syntax highlighting (highlighted)' \
'git config gitweb.highlight yes &&
echo "#!/usr/bin/sh" > test.sh &&
git add test.sh &&
git commit -m "Add test.sh" &&
gitweb_run "p=.git;a=blob;f=test.sh"'
test_debug 'cat gitweb.log'
test_done test_done