{"thread":{"id":"22700","subject":"[RFC/PATCH] gitweb: Syntax highlighting support","startedAt":"2010-02-17T21:43:36Z","lastAt":"2010-02-18T22:01:12Z","messageCount":4,"participants":["Jakub Narebski","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"134895","messageId":"20100217212855.2014.49834.stgit@localhost.localdomain","threadId":"22700","inReplyTo":null,"subject":"[RFC/PATCH] gitweb: Syntax highlighting support","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2010-02-17T21:43:36Z","receivedAt":"2010-02-17T21:43:36Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"From: Johannes Schindelin <johannes.schindelin@gmx.de>\n\nIt requires the 'highlight' program to do all the heavy-lifting:\nhttp://www.andre-simon.de / http://wiki.andre-simon.de\n\nThis is loosely based on Daniel Svensson's and Sham Chukoury's work in\ngitweb-xmms2.git (it cannot be cherry-picked, as gitweb-xmms2 first\nforked wildly, then not contributed back, and then went stale).\n\n[jn: cherry picked from bc1ed6aafd9ee4937559535c66c8bddf1864bec6\n in http://repo.or.cz/w/git/dscho.git, with a few changes]\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\nThis is a RFC, because there exist other syntax highlighters that can\nfunction as filter (take input from STDIN, and write result to STDOUT),\nand have (X)HTML among supported output formats.  Beside 'highlight'\nby Andre Simon, there is also 'source-highlight' (GNU Source Highlight),\nand probably other programs.  \n\nIt would be nice to be able to configure which program to use.  The\nquestion is if it should be configurable in per-repository basis, or\nthere should be global (per gitweb configuration) highlighter, while\nhighlighting can be turned on and off in per-repository basis.\n\nThe list of supported extensions is widened, but I see that I fotgot\nto include syntax highlighting for Perl.\n\nCompared to the version in Dscho repository, it makes gitweb do line\nnumbering by itself, instead of making 'highlight' do it... and then\nrewriting it to conform to the rest of gitweb.  We should probably\nleave tab expansion to gitweb, so that 'blob' view would look the same\nwith and without syntax hightlighting enabled.\n\nNote that gitweb.css contains default 'highlight' style, but perhaps\nother style would be better fit for deafule gitweb CSS.\n\nWARNING: No tests!\n\nHave fun playing with it!\n\n gitweb/gitweb.css  |   18 ++++++++++++++++\n gitweb/gitweb.perl |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 73 insertions(+), 2 deletions(-)\n\ndiff --git a/gitweb/gitweb.css b/gitweb/gitweb.css\nindex 50067f2..4132aab 100644\n--- a/gitweb/gitweb.css\n+++ b/gitweb/gitweb.css\n@@ -572,3 +572,21 @@ span.match {\n div.binary {\n \tfont-style: italic;\n }\n+\n+/* Style definition generated by highlight 2.4.5, http://www.andre-simon.de/ */\n+\n+/* Highlighting theme definition: */\n+\n+.num    { color:#2928ff; }\n+.esc    { color:#ff00ff; }\n+.str    { color:#ff0000; }\n+.dstr   { color:#818100; }\n+.slc    { color:#838183; font-style:italic; }\n+.com    { color:#838183; font-style:italic; }\n+.dir    { color:#008200; }\n+.sym    { color:#000000; }\n+.line   { color:#555555; }\n+.kwa    { color:#000000; font-weight:bold; }\n+.kwb    { color:#830000; }\n+.kwc    { color:#000000; font-weight:bold; }\n+.kwd    { color:#010181; }\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex 32b04a4..3b6df9f 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -227,6 +227,26 @@ our %avatar_size = (\n # Leave it undefined (or set to 'undef') to turn off load checking.\n our $maxload = 300;\n \n+# syntax highlighting\n+our %highlight = (\n+\t# match by basename\n+\t'SConstruct' => 'py',\n+\t'Program' => 'py',\n+\t'Library' => 'py',\n+\t'Makefile' => 'make',\n+\t# match by extension\n+\t'\\.py$' => 'py',\n+\t'\\.c$' => 'c',\n+\t'\\.h$' => 'c',\n+\t'\\.cpp$' => 'cpp',\n+\t'\\.cxx$' => 'cpp',\n+\t'\\.rb$' => 'ruby',\n+\t'\\.java$' => 'java',\n+\t'\\.css$' => 'css',\n+\t'\\.php3?$' => 'php',\n+\t'\\.sh$' => 'sh',\n+);\n+\n # You define site-wide feature defaults here; override them with\n # $GITWEB_CONFIG as necessary.\n our %feature = (\n@@ -445,6 +465,19 @@ our %feature = (\n \t'javascript-actions' => {\n \t\t'override' => 0,\n \t\t'default' => [0]},\n+\n+\t# Syntax highlighting support. This is based on Daniel Svensson's\n+\t# and Sham Chukoury's work in gitweb-xmms2.git.\n+\t# It requires the 'highlight' program, and therefore is disabled\n+\t# by default.\n+\n+\t# To enable system wide have in $GITWEB_CONFIG\n+\t# $feature{'highlight'}{'default'} = [1];\n+\n+\t'highlight' => {\n+\t\t'sub' => sub { feature_bool('highlight', @_) },\n+\t\t'override' => 0,\n+\t\t'default' => [0]},\n );\n \n sub gitweb_get_feature {\n@@ -5340,6 +5373,7 @@ sub git_blob {\n \topen my $fd, \"-|\", git_cmd(), \"cat-file\", \"blob\", $hash\n \t\tor die_error(500, \"Couldn't cat $file_name, $hash\");\n \tmy $mimetype = blob_mimetype($fd, $file_name);\n+\t# use 'blob_plain' (aka 'raw') view for files that cannot be displayed\n \tif ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {\n \t\tclose $fd;\n \t\treturn git_blob_plain($mimetype);\n@@ -5347,6 +5381,25 @@ sub git_blob {\n \t# we can have blame only for text/* mimetype\n \t$have_blame &&= ($mimetype =~ m!^text/!);\n \n+\tmy $have_highlight = gitweb_check_feature('highlight');\n+\tmy $syntax;\n+\tif ($have_highlight && defined($file_name)) {\n+\t\tmy $basename = basename($file_name);\n+\t\tforeach my $regexp (keys %highlight) {\n+\t\t\tif ($basename =~ /$regexp/) {\n+\t\t\t\t$syntax = $highlight{$regexp};\n+\t\t\t\tlast;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif ($syntax) {\n+\t\t\tclose $fd;\n+\t\t\topen $fd, quote_command(git_cmd(), \"cat-file\", \"blob\", $hash).\" | \".\n+\t\t\t          \"highlight --xhtml --fragment -t 8 --syntax $syntax |\"\n+\t\t\t\tor die_error(500, \"Couldn't open file or run syntax highlighter\");\n+\t\t}\n+\t}\n+\n \tgit_header_html(undef, $expires);\n \tmy $formats_nav = '';\n \tif (defined $hash_base && (my %co = parse_commit($hash_base))) {\n@@ -5395,10 +5448,10 @@ sub git_blob {\n \t\twhile (my $line = <$fd>) {\n \t\t\tchomp $line;\n \t\t\t$nr++;\n-\t\t\t$line = untabify($line);\n+\t\t\t$line = untabify($line) unless ($syntax);\n \t\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"\" . href(-replay => 1)\n \t\t\t\t. \"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\",\n-\t\t\t       $nr, $nr, $nr, esc_html($line, -nbsp=>1);\n+\t\t\t       $nr, $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);\n \t\t}\n \t}\n \tclose $fd\n"},{"id":"134940","messageId":"alpine.DEB.1.00.1002181103400.20986@pacific.mpi-cbg.de","threadId":"22700","inReplyTo":"20100217212855.2014.49834.stgit@localhost.localdomain","subject":"Re: [RFC/PATCH] gitweb: Syntax highlighting support","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2010-02-18T10:04:34Z","receivedAt":"2010-02-18T10:04:34Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Wed, 17 Feb 2010, Jakub Narebski wrote:\n\n> [jn: cherry picked from bc1ed6aafd9ee4937559535c66c8bddf1864bec6\n>  in http://repo.or.cz/w/git/dscho.git, with a few changes]\n\nThanks. These changes are greatly appreciated, and I will try to find the \ntime to install this in our production system tomorrow.\n\nCiao,\nDscho\n"},{"id":"134992","messageId":"201002182000.19721.jnareb@gmail.com","threadId":"22700","inReplyTo":"alpine.DEB.1.00.1002181103400.20986@pacific.mpi-cbg.de","subject":"Re: [RFC/PATCH] gitweb: Syntax highlighting support","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2010-02-18T19:00:18Z","receivedAt":"2010-02-18T19:00:18Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"On Thu, 18 Feb 2010, Johannes Schindelin wrote:\n> On Wed, 17 Feb 2010, Jakub Narebski wrote:\n> \n> > [jn: cherry picked from bc1ed6aafd9ee4937559535c66c8bddf1864bec6\n> >  in http://repo.or.cz/w/git/dscho.git, with a few changes]\n> \n> Thanks. These changes are greatly appreciated, and I will try to find the \n> time to install this in our production system tomorrow.\n\nThis patch is also available in 'gitweb/web' branch in my repository:\nhttp://repo.or.cz/w/git/jnareb-git.git\n\nP.S. I have applied your patch \"cherry-picking\" it from your repository\nby saving 'patch' view from gitweb and using git-am on it.  You can pull\n  git://repo.or.cz/git/jnareb-git.git gitweb/web\nor you can do the same\n  http://repo.or.cz/w/git/jnareb-git.git/patch/7bf179bb5c40518b98bc63ebcfa3ae7f0b437a42\n-- \nJakub Narebski\nPoland\n"},{"id":"135015","messageId":"m3sk8ymcga.fsf@localhost.localdomain","threadId":"22700","inReplyTo":"20100217212855.2014.49834.stgit@localhost.localdomain","subject":"Re: [RFC/PATCH] gitweb: Syntax highlighting support","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2010-02-18T22:01:12Z","receivedAt":"2010-02-18T22:01:12Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> From: Johannes Schindelin <johannes.schindelin@gmx.de>\n> \n> It requires the 'highlight' program to do all the heavy-lifting:\n> http://www.andre-simon.de / http://wiki.andre-simon.de\n\n[...]\n> ---\n> This is a RFC, because there exist other syntax highlighters that can\n> function as filter (take input from STDIN, and write result to STDOUT),\n> and have (X)HTML among supported output formats.  Beside 'highlight'\n> by Andre Simon, there is also 'source-highlight' (GNU Source Highlight),\n> and probably other programs.  \n\nActually GNU Source Highlight (http://www.gnu.org/software/src-highlite)\nas it is now wouldn't be a good fit for gitweb, because it lacks \nequivalent of Highlight '--fragment' option; even with '--no-doc' it\nprefixes contents with some comments and wraps the whole with <pre><tt>\ntags.\n\nOn the other hand side one can also use Perl modules from CPAN for\nsyntax highlighting.  It would be nice to have this option for syntax\nhighlighting in gitweb.\n\n-- \nJakub Narebski\nPoland\nShadeHawk on #git\n"}]}