Gitweb: add support for Alex Gorbatchev's SyntaxHighlighter in Javascript

Gitweb is not exactly what you would call server-friendly, so let's
offload one more task onto the client.

To enable this, put something like this into your gitweb_config.perl:

	$feature{'syntaxhighlighter_js'}{'default'} = [{
		url => '/SyntaxHighlighter/',
		style => 'Django',
		theme => 'FadeToGrey'
	}];

and clone git://github.com/alexgorbatchev/SyntaxHighlighter into the
directory you specified via the 'url' parameter.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2011-09-25 03:02:57 -05:00
parent 45cfa3506b
commit 458d6ffd0a

View file

@ -6461,7 +6461,19 @@ sub git_blob {
# we can have blame only for text/* mimetype
$have_blame &&= ($mimetype =~ m!^text/!);
my $highlight_js = gitweb_check_feature('syntaxhighlighter_js');
if ($highlight_js) {
push @stylesheets, $highlight_js->{url} . '/styles/shCore'
. $highlight_js->{style} . '.css';
push @stylesheets, $highlight_js->{url} . '/styles/shTheme'
. $highlight_js->{theme} . '.css';
}
my $highlight = gitweb_check_feature('highlight');
if ($highlight_js && $highlight) {
die_error(500, 'The highlight and syntaxhighlighter_js are'
. 'mutually exclusive');
}
my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
$fd = run_highlighter($fd, $highlight, $syntax)
if $syntax;
@ -6509,6 +6521,58 @@ sub git_blob {
href(action=>"blob_plain", hash=>$hash,
hash_base=>$hash_base, file_name=>$file_name) .
qq!" />\n!;
} elsif ($highlight_js) {
my $ext = $file_name;
$ext =~ s/.*\.//;
print qq!<pre class="brush:!.$ext.qq!">!;
while (my $line = <$fd>) {
$line =~ s!&!\&#38;!g;
$line =~ s!<!\&#60;!g;
print $line;
}
print qq!</pre>!;
foreach my $name ('Core', 'Autoloader') {
print qq!<script src="!.$highlight_js->{url}
.qq!/scripts/sh!.$name
.qq!.js" type="text/javascript"></script>!;
}
print qq!<script type="text/javascript">!;
print qq!SyntaxHighlighter.defaults["pad-line-numbers"] = 3;!;
print qq!SyntaxHighlighter.defaults["toolbar"] = false;!;
# for XHTML compliance
print qq!SyntaxHighlighter.config["space"] = '&#160;';!;
print qq!SyntaxHighlighter.autoloader(!;
my $brush_prefix = $highlight_js->{url} . '/scripts/shBrush';
foreach my $language ('applescript AppleScript',
'actionscript3 as3 AS3',
'bash shell Bash',
'clj Clojure',
'coldfusion cf ColdFusion',
'cpp c Cpp',
'c# c-sharp csharp CSharp',
'css Css',
'delphi pascal Delphi',
'diff patch pas Diff',
'erl erlang Erlang',
'groovy Groovy',
'java Java',
'jfx javafx JavaFX',
'js jscript javascript JScript',
'perl pl Perl',
'php Php',
'text plain Plain',
'py python Python',
'ruby rails ror rb Ruby',
'scala Scala',
'scm Scheme',
'sql Sql',
'vb vbnet Vb',
'xml xhtml xslt html Xml') {
my $lang = $language;
$lang =~ s! (\S+)$! $brush_prefix$1!;
print "'".$lang.qq!.js',!;
}
print qq!''); SyntaxHighlighter.all();</script>!;
} else {
my $nr;
while (my $line = <$fd>) {