]> Lady’s Gitweb - Gitweb/commitdiff
gitweb: Syntax highlighting support
authorJohannes Schindelin <redacted>
Tue, 27 Apr 2010 19:34:44 +0000 (21:34 +0200)
committerLady <redacted>
Mon, 6 Apr 2026 04:50:39 +0000 (00:50 -0400)
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 <redacted>
Signed-off-by: Jakub Narebski <redacted>
Acked-by: Petr Baudis <redacted>
Signed-off-by: Junio C Hamano <redacted>
gitweb.css
gitweb.perl

index a9b1ff04cfe705398e09fa02782a5c19b69a5956b35a188597090d55f620e53a..edc71c668da6cf59d4d470021b44d23747f820e5076dd1ff8d25b0ef1c380a96 100644 (file)
@@ -572,3 +572,21 @@ span.match {
 div.binary {
        font-style: italic;
 }
 div.binary {
        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; }
index 7d325ae984d91145b856e2daa2963dffcd4b9912d0899f9de9eba0cfc4704526..df52a7263a9c6967e4e27762f704b412ad86f15e1de0bb4d60ff4362cc67d37c 100755 (executable)
@@ -228,6 +228,36 @@ our %avatar_size = (
 # Leave it undefined (or set to 'undef') to turn off load checking.
 our $maxload = 300;
 
 # Leave it undefined (or set to 'undef') to turn off load checking.
 our $maxload = 300;
 
+# syntax highlighting
+our %highlight_type = (
+       # match by basename
+       'SConstruct' => 'py',
+       'Program' => 'py',
+       'Library' => 'py',
+       'Makefile' => 'make',
+       # match by extension
+       '\.py$' => 'py', # Python
+       '\.c$' => 'c',
+       '\.h$' => 'c',
+       '\.cpp$' => 'cpp',
+       '\.cxx$' => 'cpp',
+       '\.rb$' => 'ruby',
+       '\.java$' => 'java',
+       '\.css$' => 'css',
+       '\.php3?$' => 'php',
+       '\.sh$' => 'sh', # Bash / shell script
+       '\.pl$' => 'pl', # Perl
+       '\.js$' => 'js', # JavaScript
+       '\.tex$' => 'tex', # TeX and LaTeX
+       '\.bib$' => 'bib', # BibTeX
+       '\.x?html$' => 'xml',
+       '\.xml$' => 'xml',
+       '\.awk$' => 'awk',
+       '\.bat$' => 'bat', # DOS Batch script
+       '\.ini$' => 'ini',
+       '\.spec$' => 'spec', # RPM Spec
+);
+
 # You define site-wide feature defaults here; override them with
 # $GITWEB_CONFIG as necessary.
 our %feature = (
 # You define site-wide feature defaults here; override them with
 # $GITWEB_CONFIG as necessary.
 our %feature = (
@@ -446,6 +476,19 @@ our %feature = (
        'javascript-actions' => {
                'override' => 0,
                'default' => [0]},
        'javascript-actions' => {
                'override' => 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, 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 {
@@ -5381,6 +5424,7 @@ sub git_blob {
        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
                or die_error(500, "Couldn't cat $file_name, $hash");
        my $mimetype = blob_mimetype($fd, $file_name);
        open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
                or die_error(500, "Couldn't cat $file_name, $hash");
        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) {
                close $fd;
                return git_blob_plain($mimetype);
        if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
                close $fd;
                return git_blob_plain($mimetype);
@@ -5388,6 +5432,25 @@ sub git_blob {
        # we can have blame only for text/* mimetype
        $have_blame &&= ($mimetype =~ m!^text/!);
 
        # we can have blame only for text/* mimetype
        $have_blame &&= ($mimetype =~ m!^text/!);
 
+       my $have_highlight = gitweb_check_feature('highlight');
+       my $syntax;
+       if ($have_highlight && defined($file_name)) {
+               my $basename = basename($file_name, '.in');
+               foreach my $regexp (keys %highlight_type) {
+                       if ($basename =~ /$regexp/) {
+                               $syntax = $highlight_type{$regexp};
+                               last;
+                       }
+               }
+
+               if ($syntax) {
+                       close $fd;
+                       open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
+                                 "highlight --xhtml --fragment -t 8 --syntax $syntax |"
+                               or die_error(500, "Couldn't open file or run syntax highlighter");
+               }
+       }
+
        git_header_html(undef, $expires);
        my $formats_nav = '';
        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
        git_header_html(undef, $expires);
        my $formats_nav = '';
        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
@@ -5439,7 +5502,7 @@ sub git_blob {
                        $line = untabify($line);
                        printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
                                . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
                        $line = untabify($line);
                        printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
                                . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
-                              $nr, $nr, $nr, esc_html($line, -nbsp=>1);
+                              $nr, $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
                }
        }
        close $fd
                }
        }
        close $fd
This page took 0.249296 seconds and 4 git commands to generate.