21

I use gitweb.

In the tree view I see my source code. But this code is all in black, I like configurated to added highlight

Pablo Morales
  • 687
  • 1
  • 5
  • 15
  • With Git 2.11+ (Q4 2016), you will be able to force language detection based on the *content* of the file (shebang). See [my answer below](http://stackoverflow.com/a/39846665/6309) – VonC Oct 04 '16 at 07:36

5 Answers5

26

To enable syntax highlighting in 'blob' view in modern gitweb, you need

  1. Have highlight program installed (the one from 'highlight' package, not from 'source-highlight' or 'src-highlight'; they are different programs).

  2. Configure gitweb to enable source highlighting. This can be done by adding the following line

    $feature{'highlight'}{'default'} = [1];
    

    to gitweb configuration file for your installation, usually /etc/gitweb.conf.

    See "Configuring gitweb features" section in gitweb.conf manpage (and said manpage in general), the part about 'highlight' feature, or relevant lines in gitweb sources.

    Note: if you use gitweb with persistent web server environment, like mod_perl, FastCGI or PSGI, you might need to restart gitweb script if it is configured to read configuration only once.


Original response (historical)

It is currently not supported, but take a look at gitweb/web branch of my git/jnareb-git.git repository - those two commits adding syntax highlighting to gitweb were sent to git mailing list (as RFC = Request For Comments patches). Those patches use highlight tool from http://www.andre-simon.de/

You can try to cherry pick those two commits: 5f7b6461 and 4edcf10d.

Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 3
    Judging from the other answers to this question, it looks like these patches were accepted into git itself, right? – Marius Gedminas Aug 14 '12 at 09:55
  • 2
    Nowadays your distro's git package should already contain highlight support. You just need to enable it in gitweb.conf. – cdleonard Jan 24 '13 at 14:36
  • Will not work for me (using gitweb from git v1.8.0 package). I also found out that you can place a gitweb_config.perl in the same directory as your gitweb.cgi script. –  Dec 14 '13 at 13:34
  • Oh well. It seems I _HAVE_ to use one, and only one: [Find "$highlight_bin"](https://www.kernel.org/pub/software/scm/git/docs/gitweb.conf.html) –  Dec 14 '13 at 13:40
  • 1
    This works for many programming languages but `*.md` files and `Dockerfile` are still displayed as-is though `highlight` [supports](http://www.andre-simon.de/doku/highlight/en/langs.php) them. See `man 5 gitweb.conf` for how to enable highlighting for such flies. – ynn Jul 15 '20 at 18:13
16

If you don't want to change the package files, an alternative to Todochangeusername procedure is to install highlight and then add to the bottom of the file /etc/gitweb.conf:

$feature{'highlight'}{'default'} = [1];

(without the \ escape character).

3
  1. install at least git version 1.7.5 Ubuntu: https://launchpad.net/~git-core/+archive/ppa
  2. install gitweb and programm called highlight Ubuntu: sudo apt-get install gitweb highlight
  3. Add to the file: sudo vim /usr/lib/git-core/git-instaweb
    line 586: \$feature{'highlight'}{'default'} = [1];
  • 2
    No, you don't have to edit git-instaweb to have highlighting in gitweb. Adding same line (without escape of course) to `/etc/gitweb.conf` is much better idea. – Olli Mar 22 '12 at 17:35
  • 1
    @Olli, the settings file created by `git instaweb` seems to override settings in `/etc/gitweb.conf` (at least on my machine), so this is a necessary workaround specific for `git instaweb`. – Turion Jul 15 '15 at 10:14
0

I had a similar issue: syntax highlighting was only enabled on a few languages (C, Makefile, etc.). It was not working for verilog, even though it is working when using highlight with the command line.

Looking at gitweb.cgi make me realize that gitweb only supports a subset of languages. It is also mentioned in the gitweb.conf man page mentionned in the first answer. The solution is to add this to gitweb.conf:

our %highlight_ext;
$highlight_ext{'v'} = 'verilog';
$highlight_ext{'ucf'} = 'verilog';

Change verilogbyt the language you would like to support. The full list of available languages is in /etc/highlight/filetypes.conf. Of course, you also have to adapt the file extensions to your case.

Tom
  • 834
  • 1
  • 14
  • 24
-1

With Git 2.11+ (Q4 2016), you can force the language detection for syntax highlighting based on the shebang (for instance: #!/bin/perl)

In other words, the syntax highlighting does not depend anymore only on filename or file extension. It can rely on its content if asked.

See commit 779a206, commit c151aa3 (24 Sep 2016) by Ian Kelling (ian-kelling).
(Merged by Junio C Hamano -- gitster -- in commit 3474084, 03 Oct 2016)

gitweb: use highlight's shebang detection

"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.

The "highlight" binary can, in some cases, determine the language type by the means of file contents, for example the shebang in the first line for some scripting languages.
Make use of this autodetection for files which syntax is not known by gitweb.
In that case, pass the blob contents to "highlight --force"; the parameter is needed to make it always generate HTML output (which includes HTML-escaping).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250