threads / patch / 24497

patchgitweb: move highlight config out of guess_file_syntax()

Subject: [PATCH] gitweb: move highlight config out of guess_file_syntax()

## tl;dr

6 messages between Jul 24, 2010 and Jul 28, 2010. Diffs are folded; open one to read it.

replies: 5people: 2as markdown or json

Alejandro R. Sedeño· Jul 24, 2010, 19:46 UTC · lore

Move highlight config out of guess_file_syntax() so that it can be extended/overridden by system/user configuration.

Signed-off-by: Alejandro R. Sedeño <asedeno@mit.edu>
---
 gitweb/gitweb.perl |   47 +++++++++++++++++++++++------------------------
 1 files changed, 23 insertions(+), 24 deletions(-)
Show changes to gitweb/gitweb.perl +23 −24
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index cedc357..e0e9532 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -232,6 +232,29 @@ our %avatar_size = (
 # Leave it undefined (or set to 'undef') to turn off load checking.
 our $maxload = 300;
 
+# configuration for 'highlight' (http://www.andre-simon.de/)
+# match by basename
+our %highlight_basename = (
+	#'Program' => 'py',
+	#'Library' => 'py',
+	'SConstruct' => 'py', # SCons equivalent of Makefile
+	'Makefile' => 'make',
+);
+# match by extension
+our %highlight_ext = (
+	# main extensions, defining name of syntax;
+	# see files in /usr/share/highlight/langDefs/ directory
+	map { $_ => $_ }
+		qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
+	# alternate extensions, see /etc/highlight/filetypes.conf
+	'h' => 'c',
+	map { $_ => 'cpp' } qw(cxx c++ cc),
+	map { $_ => 'php' } qw(php3 php4),
+	map { $_ => 'pl'  } qw(perl pm), # perhaps also 'cgi'
+	'mak' => 'make',
+	map { $_ => 'xml' } qw(xhtml html htm),
+);
+
 # You define site-wide feature defaults here; override them with
 # $GITWEB_CONFIG as necessary.
 our %feature = (
@@ -3316,30 +3339,6 @@ sub blob_contenttype {
 sub guess_file_syntax {
 	my ($highlight, $mimetype, $file_name) = @_;
 	return undef unless ($highlight && defined $file_name);
-
-	# configuration for 'highlight' (http://www.andre-simon.de/)
-	# match by basename
-	my %highlight_basename = (
-		#'Program' => 'py',
-		#'Library' => 'py',
-		'SConstruct' => 'py', # SCons equivalent of Makefile
-		'Makefile' => 'make',
-	);
-	# match by extension
-	my %highlight_ext = (
-		# main extensions, defining name of syntax;
-		# see files in /usr/share/highlight/langDefs/ directory
-		map { $_ => $_ }
-			qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
-		# alternate extensions, see /etc/highlight/filetypes.conf
-		'h' => 'c',
-		map { $_ => 'cpp' } qw(cxx c++ cc),
-		map { $_ => 'php' } qw(php3 php4),
-		map { $_ => 'pl'  } qw(perl pm), # perhaps also 'cgi'
-		'mak' => 'make',
-		map { $_ => 'xml' } qw(xhtml html htm),
-	);
-
 	my $basename = basename($file_name, '.in');
 	return $highlight_basename{$basename}
 		if exists $highlight_basename{$basename};
-- 
1.7.2
Alejandro R. Sedeño· Jul 25, 2010, 23:53 UTC · lore

Re: [PATCH] gitweb: move highlight config out of guess_file_syntax()

On 7/25/2010 7:35 PM, Jakub Narebski wrote:
> BTW. is this something _you_ needed (and what did you need to configure
> / to put in $GITWEB_CONFIG file), or just a generalization?

This is something I was using in previous versions of gitweb, though with the older (pre-592ea41) syntax. (I had applied the older highlighting patches, and was quite happy to see them merged.)

Before, $GITWEB_CONFIG contained lines like:
  $highlight_type{'\.lisp$'} = 'lisp';

As of 1.7.2, with syntax highlighting merged and refactored (592ea41), access to the hashes that define syntax highlighting mappings was lost. Moving the hashes out into the config area lets $GITWEB_CONFIG contain lines like:

  $highlight_ext{'lisp'} = 'lisp';
  $highlight_basename{'GNUmakefile'} = 'mak'
Jakub Narebski· Jul 26, 2010, 20:48 UTC · re: Alejandro R. Sedeño · lore

Re: [PATCH] gitweb: move highlight config out of guess_file_syntax()

On Mon, 26 Jul 2010, Alejandro R. Sedeño wrote:
> On 7/25/2010 7:35 PM, Jakub Narębski wrote:
Show 19 quoted lines
> > BTW. is this something _you_ needed (and what did you need to configure
> > / to put in $GITWEB_CONFIG file), or just a generalization?
> 
> 
> This is something I was using in previous versions of gitweb, though
> with the older (pre-592ea41) syntax. (I had applied the older
> highlighting patches, and was quite happy to see them merged.)
> 
> Before, $GITWEB_CONFIG contained lines like:
> 
>   $highlight_type{'\.lisp$'} = 'lisp';
> 
> As of 1.7.2, with syntax highlighting merged and refactored (592ea41),
> access to the hashes that define syntax highlighting mappings was lost.
> Moving the hashes out into the config area lets $GITWEB_CONFIG contain
> lines like:
> 
>   $highlight_ext{'lisp'} = 'lisp';
>   $highlight_basename{'GNUmakefile'} = 'mak';

O.K., I don't think we want to put all possible languages that 'highlight' supports in %highlight_ext, so you might want to add support for less common languages, or languages introduced in newer versions of tool.

Note that you can write here
    $highlight_basename{'GNUmakefile'} = 'make';
which might be slightly more readable.
-- 
Jakub Narebski
Poland
Alejandro R. Sedeño· Jul 26, 2010, 20:56 UTC · re: Jakub Narebski · lore

Re: [PATCH] gitweb: move highlight config out of guess_file_syntax()

On 7/26/2010 4:48 PM, Jakub Narebski wrote:
Show 17 quoted lines
>> As of 1.7.2, with syntax highlighting merged and refactored (592ea41),
>> > access to the hashes that define syntax highlighting mappings was lost.
>> > Moving the hashes out into the config area lets $GITWEB_CONFIG contain
>> > lines like:
>> > 
>> >   $highlight_ext{'lisp'} = 'lisp';
>> >   $highlight_basename{'GNUmakefile'} = 'mak';
> O.K., I don't think we want to put all possible languages that 'highlight'
> supports in %highlight_ext, so you might want to add support for less
> common languages, or languages introduced in newer versions of tool.
> 
> 
> Note that you can write here
> 
>     $highlight_basename{'GNUmakefile'} = 'make';
> 
> which might be slightly more readable.

Yeah, that was a typo on my part; I was recalling samples from memory. The actual entry reads 'make' not 'mak', just as you suggest. :)

-Alejandro
Jakub Narebski· Jul 26, 2010, 20:45 UTC · lore

Re: [PATCH] gitweb: move highlight config out of guess_file_syntax()

On Mon, 26 Jul 2010, Jakub Narębski wrote:
Show 22 quoted lines
> On Sat, 24 Jul 2010, Alejandro R. Sedeño wrote:
> 
> > Move highlight config out of guess_file_syntax() so that it can be
> > extended/overridden by system/user configuration.
> > 
> > Signed-off-by: Alejandro R. Sedeño <asedeno@mit.edu>
> 
> Good idea.
> 
> The only _possible_ problem is that this configuration is fairly specific
> with respect to currently the only one sipported hightlighter, i.e. the
> external binary 'highlight' (http://www.andre-simon.de).  If we are to
> add support for other programs (e.g. GNU Source Highlight) or Perl modules
> for syntax highlighting (e.g. Syntax::Highlight::Engine::Kate or 
> Beautifier) in the future, this could mean backward incompatibile change.
> 
> The culprit is using extensions (in 'highlight' main syntax files are 
> named by extensions) rather than format names in %highlight_basename
> and %highlight_ext, e.g. 'py' rather than 'python'.  I don't know if
> it would be much of the problem.
> 
> So perhaps I am worrying about nothing...

I am worrying about nothing. The default syntax highlighter would be 'highlight' program; if one wants to change highlighter, one can also modify required configuration (names of 'syntax' to choose from might be different for different highlighting engines).

Therefore:
Acked-by: Jakub Narebski <jnareb@gmail.com>
-- 
Jakub Narebski
Poland
Alejandro R. Sedeño· Jul 28, 2010, 18:40 UTC · re: Jakub Narebski · lore

Move highlight config out of guess_file_syntax() so that it can be extended/overridden by system/user configuration.

Signed-off-by: Alejandro R. Sedeño <asedeno@mit.edu>
Acked-by: Jakub Narebski <jnareb@gmail.com>
---
 gitweb/gitweb.perl |   47 +++++++++++++++++++++++------------------------
 1 files changed, 23 insertions(+), 24 deletions(-)
Show changes to gitweb/gitweb.perl +23 −24
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index cedc357..e0e9532 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -232,6 +232,29 @@ our %avatar_size = (
 # Leave it undefined (or set to 'undef') to turn off load checking.
 our $maxload = 300;
 
+# configuration for 'highlight' (http://www.andre-simon.de/)
+# match by basename
+our %highlight_basename = (
+	#'Program' => 'py',
+	#'Library' => 'py',
+	'SConstruct' => 'py', # SCons equivalent of Makefile
+	'Makefile' => 'make',
+);
+# match by extension
+our %highlight_ext = (
+	# main extensions, defining name of syntax;
+	# see files in /usr/share/highlight/langDefs/ directory
+	map { $_ => $_ }
+		qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
+	# alternate extensions, see /etc/highlight/filetypes.conf
+	'h' => 'c',
+	map { $_ => 'cpp' } qw(cxx c++ cc),
+	map { $_ => 'php' } qw(php3 php4),
+	map { $_ => 'pl'  } qw(perl pm), # perhaps also 'cgi'
+	'mak' => 'make',
+	map { $_ => 'xml' } qw(xhtml html htm),
+);
+
 # You define site-wide feature defaults here; override them with
 # $GITWEB_CONFIG as necessary.
 our %feature = (
@@ -3316,30 +3339,6 @@ sub blob_contenttype {
 sub guess_file_syntax {
 	my ($highlight, $mimetype, $file_name) = @_;
 	return undef unless ($highlight && defined $file_name);
-
-	# configuration for 'highlight' (http://www.andre-simon.de/)
-	# match by basename
-	my %highlight_basename = (
-		#'Program' => 'py',
-		#'Library' => 'py',
-		'SConstruct' => 'py', # SCons equivalent of Makefile
-		'Makefile' => 'make',
-	);
-	# match by extension
-	my %highlight_ext = (
-		# main extensions, defining name of syntax;
-		# see files in /usr/share/highlight/langDefs/ directory
-		map { $_ => $_ }
-			qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
-		# alternate extensions, see /etc/highlight/filetypes.conf
-		'h' => 'c',
-		map { $_ => 'cpp' } qw(cxx c++ cc),
-		map { $_ => 'php' } qw(php3 php4),
-		map { $_ => 'pl'  } qw(perl pm), # perhaps also 'cgi'
-		'mak' => 'make',
-		map { $_ => 'xml' } qw(xhtml html htm),
-	);
-
 	my $basename = basename($file_name, '.in');
 	return $highlight_basename{$basename}
 		if exists $highlight_basename{$basename};
-- 
1.7.1

← back to recent threads