Skip to content

Commit 66a72a9

Browse files
dschonalla
authored andcommitted
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 <[email protected]>
1 parent 6db04f9 commit 66a72a9

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

gitweb/gitweb.perl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7079,7 +7079,19 @@ sub git_blob {
70797079
# we can have blame only for text/* mimetype
70807080
$have_blame &&= ($mimetype =~ m!^text/!);
70817081

7082+
my $highlight_js = gitweb_check_feature('syntaxhighlighter_js');
7083+
if ($highlight_js) {
7084+
push @stylesheets, $highlight_js->{url} . '/styles/shCore'
7085+
. $highlight_js->{style} . '.css';
7086+
push @stylesheets, $highlight_js->{url} . '/styles/shTheme'
7087+
. $highlight_js->{theme} . '.css';
7088+
}
7089+
70827090
my $highlight = gitweb_check_feature('highlight');
7091+
if ($highlight_js && $highlight) {
7092+
die_error(500, 'The highlight and syntaxhighlighter_js are'
7093+
. 'mutually exclusive');
7094+
}
70837095
my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
70847096
$fd = run_highlighter($fd, $highlight, $syntax)
70857097
if $syntax;
@@ -7127,6 +7139,58 @@ sub git_blob {
71277139
href(action=>"blob_plain", hash=>$hash,
71287140
hash_base=>$hash_base, file_name=>$file_name) .
71297141
qq!" />\n!;
7142+
} elsif ($highlight_js) {
7143+
my $ext = $file_name;
7144+
$ext =~ s/.*\.//;
7145+
print qq!<pre class="brush:!.$ext.qq!">!;
7146+
while (my $line = <$fd>) {
7147+
$line =~ s!&!\&#38;!g;
7148+
$line =~ s!<!\&#60;!g;
7149+
print $line;
7150+
}
7151+
print qq!</pre>!;
7152+
foreach my $name ('Core', 'Autoloader') {
7153+
print qq!<script src="!.$highlight_js->{url}
7154+
.qq!/scripts/sh!.$name
7155+
.qq!.js" type="text/javascript"></script>!;
7156+
}
7157+
print qq!<script type="text/javascript">!;
7158+
print qq!SyntaxHighlighter.defaults["pad-line-numbers"] = 3;!;
7159+
print qq!SyntaxHighlighter.defaults["toolbar"] = false;!;
7160+
# for XHTML compliance
7161+
print qq!SyntaxHighlighter.config["space"] = '&#160;';!;
7162+
print qq!SyntaxHighlighter.autoloader(!;
7163+
my $brush_prefix = $highlight_js->{url} . '/scripts/shBrush';
7164+
foreach my $language ('applescript AppleScript',
7165+
'actionscript3 as3 AS3',
7166+
'bash shell Bash',
7167+
'clj Clojure',
7168+
'coldfusion cf ColdFusion',
7169+
'cpp c Cpp',
7170+
'c# c-sharp csharp CSharp',
7171+
'css Css',
7172+
'delphi pascal Delphi',
7173+
'diff patch pas Diff',
7174+
'erl erlang Erlang',
7175+
'groovy Groovy',
7176+
'java Java',
7177+
'jfx javafx JavaFX',
7178+
'js jscript javascript JScript',
7179+
'perl pl Perl',
7180+
'php Php',
7181+
'text plain Plain',
7182+
'py python Python',
7183+
'ruby rails ror rb Ruby',
7184+
'scala Scala',
7185+
'scm Scheme',
7186+
'sql Sql',
7187+
'vb vbnet Vb',
7188+
'xml xhtml xslt html Xml') {
7189+
my $lang = $language;
7190+
$lang =~ s! (\S+)$! $brush_prefix$1!;
7191+
print "'".$lang.qq!.js',!;
7192+
}
7193+
print qq!''); SyntaxHighlighter.all();</script>!;
71307194
} else {
71317195
my $nr;
71327196
while (my $line = <$fd>) {

0 commit comments

Comments
 (0)