Skip to content

Commit 9f82c7a

Browse files
committed
Merge 'gitweb-syntax' into HEAD
2 parents 158d024 + a4940fe commit 9f82c7a

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

gitweb/gitweb.perl

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4547,6 +4547,29 @@ sub git_print_page_path {
45474547
print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
45484548
hash_base=>$hb),
45494549
-title => $name}, esc_path($basename));
4550+
if (gitweb_check_feature('highlight')) {
4551+
print '    
4552+
<a id="lineNoToggle" href="#" onclick="toggleLineNumbers();"></a>
4553+
<script>
4554+
function toggleLineNumbers() {
4555+
e = document.getElementById("lineNoStyle");
4556+
e2 = document.getElementById("lineNoToggle");
4557+
if (e2.innerHTML == "[Hide line numbers]") {
4558+
e.innerHTML = ".linenr { display:none; }";
4559+
e2.innerHTML = "[Show line numbers]";
4560+
}
4561+
else {
4562+
e.innerHTML = "";
4563+
e2.innerHTML = "[Hide line numbers]";
4564+
}
4565+
}
4566+
var style = document.createElement("style");
4567+
style.setAttribute("id", "lineNoStyle");
4568+
document.getElementsByTagName("head")[0].appendChild(style);
4569+
toggleLineNumbers();
4570+
</script>
4571+
';
4572+
}
45504573
} elsif (defined $type && $type eq 'tree') {
45514574
print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
45524575
hash_base=>$hb),
@@ -7058,7 +7081,19 @@ sub git_blob {
70587081
# we can have blame only for text/* mimetype
70597082
$have_blame &&= ($mimetype =~ m!^text/!);
70607083

7084+
my $highlight_js = gitweb_check_feature('syntaxhighlighter_js');
7085+
if ($highlight_js) {
7086+
push @stylesheets, $highlight_js->{url} . '/styles/shCore'
7087+
. $highlight_js->{style} . '.css';
7088+
push @stylesheets, $highlight_js->{url} . '/styles/shTheme'
7089+
. $highlight_js->{theme} . '.css';
7090+
}
7091+
70617092
my $highlight = gitweb_check_feature('highlight');
7093+
if ($highlight_js && $highlight) {
7094+
die_error(500, 'The highlight and syntaxhighlighter_js are'
7095+
. 'mutually exclusive');
7096+
}
70627097
my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
70637098
$fd = run_highlighter($fd, $highlight, $syntax)
70647099
if $syntax;
@@ -7106,6 +7141,72 @@ sub git_blob {
71067141
href(action=>"blob_plain", hash=>$hash,
71077142
hash_base=>$hash_base, file_name=>$file_name) .
71087143
qq!" />\n!;
7144+
} elsif ($highlight_js) {
7145+
my $ext = $file_name;
7146+
$ext =~ s/.*\.//;
7147+
print qq!<pre class="brush:!.$ext.qq!">!;
7148+
while (my $line = <$fd>) {
7149+
$line =~ s!&!\&#38;!g;
7150+
$line =~ s!<!\&#60;!g;
7151+
print $line;
7152+
}
7153+
print qq!</pre>!;
7154+
foreach my $name ('Core', 'Autoloader') {
7155+
print qq!<script src="!.$highlight_js->{url}
7156+
.qq!/scripts/sh!.$name
7157+
.qq!.js" type="text/javascript"></script>!;
7158+
}
7159+
print qq!<script type="text/javascript">!;
7160+
print qq!SyntaxHighlighter.defaults["pad-line-numbers"] = 3;!;
7161+
print qq!SyntaxHighlighter.defaults["toolbar"] = false;!;
7162+
# for XHTML compliance
7163+
print qq!SyntaxHighlighter.config["space"] = '&#160;';!;
7164+
print qq!SyntaxHighlighter.autoloader(!;
7165+
my $brush_prefix = $highlight_js->{url} . '/scripts/shBrush';
7166+
foreach my $language ('applescript AppleScript',
7167+
'actionscript3 as3 AS3',
7168+
'bash shell Bash',
7169+
'clj Clojure',
7170+
'coldfusion cf ColdFusion',
7171+
'cpp c Cpp',
7172+
'c# c-sharp csharp CSharp',
7173+
'css Css',
7174+
'delphi pascal Delphi',
7175+
'diff patch pas Diff',
7176+
'erl erlang Erlang',
7177+
'groovy Groovy',
7178+
'java Java',
7179+
'jfx javafx JavaFX',
7180+
'js jscript javascript JScript',
7181+
'perl pl Perl',
7182+
'php Php',
7183+
'text plain Plain',
7184+
'py python Python',
7185+
'ruby rails ror rb Ruby',
7186+
'scala Scala',
7187+
'scm Scheme',
7188+
'sql Sql',
7189+
'vb vbnet Vb',
7190+
'xml xhtml xslt html Xml') {
7191+
my $lang = $language;
7192+
$lang =~ s! (\S+)$! $brush_prefix$1!;
7193+
print "'".$lang.qq!.js',!;
7194+
}
7195+
print qq!''); SyntaxHighlighter.all();!
7196+
.qq!function scrollTo(number) {!
7197+
.qq! var elements = document.getElementsByClassName(number);!
7198+
.qq! if (elements.length == 0) setTimeout('scrollTo("' + number + '");', 50);!
7199+
.qq! else {!
7200+
.qq! window.scroll(0, elements[0].offsetTop);!
7201+
.qq! window.scrollTo(0, elements[0].offsetTop);!
7202+
.qq! elements[0].style.color = '#ff0000';!
7203+
.qq! }!
7204+
.qq!}!
7205+
.qq!var lineRegex = /#l(\\d+)\$/;!
7206+
.qq!var lineNumber = lineRegex.exec(document.URL);!
7207+
.qq!if (lineNumber)!
7208+
.qq! scrollTo('number' + lineNumber[1]);!
7209+
.qq!</script>!;
71097210
} else {
71107211
my $nr;
71117212
while (my $line = <$fd>) {

0 commit comments

Comments
 (0)