Merge branch 'ik/gitweb-force-highlight'

"gitweb" can spawn "highlight" to show blob contents with
(programming) language-specific syntax highlighting, but only
when the language is known.  "highlight" can however be told
to make the guess itself by giving it "--force" option, which
has been enabled.

* ik/gitweb-force-highlight:
  gitweb: use highlight's shebang detection
  gitweb: remove unused guess_file_syntax() parameter
This commit is contained in:
Junio C Hamano 2016-10-03 13:30:33 -07:00
commit 347408496a
3 changed files with 29 additions and 14 deletions

View File

@ -246,13 +246,20 @@ $highlight_bin::
Note that 'highlight' feature must be set for gitweb to actually
use syntax highlighting.
+
*NOTE*: if you want to add support for new file type (supported by
"highlight" but not used by gitweb), you need to modify `%highlight_ext`
or `%highlight_basename`, depending on whether you detect type of file
based on extension (for example "sh") or on its basename (for example
"Makefile"). The keys of these hashes are extension and basename,
respectively, and value for given key is name of syntax to be passed via
`--syntax <syntax>` to highlighter.
*NOTE*: for a file to be highlighted, its syntax type must be detected
and that syntax must be supported by "highlight". The default syntax
detection is minimal, and there are many supported syntax types with no
detection by default. There are three options for adding syntax
detection. The first and second priority are `%highlight_basename` and
`%highlight_ext`, which detect based on basename (the full filename, for
example "Makefile") and extension (for example "sh"). The keys of these
hashes are the basename and extension, respectively, and the value for a
given key is the name of the syntax to be passed via `--syntax <syntax>`
to "highlight". The last priority is the "highlight" configuration of
`Shebang` regular expressions to detect the language based on the first
line in the file, (for example, matching the line "#!/bin/bash"). See
the highlight documentation and the default config at
/etc/highlight/filetypes.conf for more details.
+
For example if repositories you are hosting use "phtml" extension for
PHP files, and you want to have correct syntax-highlighting for those

View File

@ -3913,7 +3913,7 @@ sub blob_contenttype {
# guess file syntax for syntax highlighting; return undef if no highlighting
# the name of syntax can (in the future) depend on syntax highlighter used
sub guess_file_syntax {
my ($highlight, $mimetype, $file_name) = @_;
my ($highlight, $file_name) = @_;
return undef unless ($highlight && defined $file_name);
my $basename = basename($file_name, '.in');
return $highlight_basename{$basename}
@ -3931,15 +3931,16 @@ sub guess_file_syntax {
# or return original FD if no highlighting
sub run_highlighter {
my ($fd, $highlight, $syntax) = @_;
return $fd unless ($highlight && defined $syntax);
return $fd unless ($highlight);
close $fd;
my $syntax_arg = (defined $syntax) ? "--syntax $syntax" : "--force";
open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
quote_command($^X, '-CO', '-MEncode=decode,FB_DEFAULT', '-pse',
'$_ = decode($fe, $_, FB_DEFAULT) if !utf8::decode($_);',
'--', "-fe=$fallback_encoding")." | ".
quote_command($highlight_bin).
" --replace-tabs=8 --fragment --syntax $syntax |"
" --replace-tabs=8 --fragment $syntax_arg |"
or die_error(500, "Couldn't open file or run syntax highlighter");
return $fd;
}
@ -7062,9 +7063,8 @@ sub git_blob {
$have_blame &&= ($mimetype =~ m!^text/!);
my $highlight = gitweb_check_feature('highlight');
my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
$fd = run_highlighter($fd, $highlight, $syntax)
if $syntax;
my $syntax = guess_file_syntax($highlight, $file_name);
$fd = run_highlighter($fd, $highlight, $syntax);
git_header_html(undef, $expires);
my $formats_nav = '';
@ -7117,7 +7117,7 @@ sub git_blob {
$line = untabify($line);
printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
$nr, esc_attr(href(-replay => 1)), $nr, $nr,
$syntax ? sanitize($line) : esc_html($line, -nbsp=>1);
$highlight ? sanitize($line) : esc_html($line, -nbsp=>1);
}
}
close $fd

View File

@ -709,6 +709,14 @@ test_expect_success HIGHLIGHT \
git commit -m "Add test.sh" &&
gitweb_run "p=.git;a=blob;f=test.sh"'
test_expect_success HIGHLIGHT \
'syntax highlighting (highlighter language autodetection)' \
'git config gitweb.highlight yes &&
echo "#!/usr/bin/perl" > test &&
git add test &&
git commit -m "Add test" &&
gitweb_run "p=.git;a=blob;f=test"'
# ----------------------------------------------------------------------
# forks of projects