Show More
@@ -13,11 +13,17 b'' | |||||
13 | It depends on the Pygments syntax highlighting library: |
|
13 | It depends on the Pygments syntax highlighting library: | |
14 | http://pygments.org/ |
|
14 | http://pygments.org/ | |
15 |
|
15 | |||
16 |
There are t |
|
16 | There are the following configuration options:: | |
17 |
|
17 | |||
18 | [web] |
|
18 | [web] | |
19 | pygments_style = <style> (default: colorful) |
|
19 | pygments_style = <style> (default: colorful) | |
20 | highlightfiles = <fileset> (default: size('<5M')) |
|
20 | highlightfiles = <fileset> (default: size('<5M')) | |
|
21 | highlightonlymatchfilename = <bool> (default False) | |||
|
22 | ||||
|
23 | ``highlightonlymatchfilename`` will only highlight files if their type could | |||
|
24 | be identified by their filename. When this is not enabled (the default), | |||
|
25 | Pygments will try very hard to identify the file type from content and any | |||
|
26 | match (even matches with a low confidence score) will be used. | |||
21 | """ |
|
27 | """ | |
22 |
|
28 | |||
23 | import highlight |
|
29 | import highlight | |
@@ -32,12 +38,14 b" testedwith = 'internal'" | |||||
32 | def pygmentize(web, field, fctx, tmpl): |
|
38 | def pygmentize(web, field, fctx, tmpl): | |
33 | style = web.config('web', 'pygments_style', 'colorful') |
|
39 | style = web.config('web', 'pygments_style', 'colorful') | |
34 | expr = web.config('web', 'highlightfiles', "size('<5M')") |
|
40 | expr = web.config('web', 'highlightfiles', "size('<5M')") | |
|
41 | filenameonly = web.configbool('web', 'highlightonlymatchfilename', False) | |||
35 |
|
42 | |||
36 | ctx = fctx.changectx() |
|
43 | ctx = fctx.changectx() | |
37 | tree = fileset.parse(expr) |
|
44 | tree = fileset.parse(expr) | |
38 | mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) |
|
45 | mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None) | |
39 | if fctx.path() in fileset.getset(mctx, tree): |
|
46 | if fctx.path() in fileset.getset(mctx, tree): | |
40 |
highlight.pygmentize(field, fctx, style, tmpl |
|
47 | highlight.pygmentize(field, fctx, style, tmpl, | |
|
48 | guessfilenameonly=filenameonly) | |||
41 |
|
49 | |||
42 | def filerevision_highlight(orig, web, req, tmpl, fctx): |
|
50 | def filerevision_highlight(orig, web, req, tmpl, fctx): | |
43 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
|
51 | mt = ''.join(tmpl('mimetype', encoding=encoding.encoding)) |
@@ -20,7 +20,7 b' from pygments.formatters import HtmlForm' | |||||
20 | SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' |
|
20 | SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' | |
21 | 'type="text/css" />') |
|
21 | 'type="text/css" />') | |
22 |
|
22 | |||
23 | def pygmentize(field, fctx, style, tmpl): |
|
23 | def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False): | |
24 |
|
24 | |||
25 | # append a <link ...> to the syntax highlighting css |
|
25 | # append a <link ...> to the syntax highlighting css | |
26 | old_header = tmpl.load('header') |
|
26 | old_header = tmpl.load('header') | |
@@ -46,6 +46,12 b' def pygmentize(field, fctx, style, tmpl)' | |||||
46 | lexer = guess_lexer_for_filename(fctx.path(), text[:1024], |
|
46 | lexer = guess_lexer_for_filename(fctx.path(), text[:1024], | |
47 | stripnl=False) |
|
47 | stripnl=False) | |
48 | except (ClassNotFound, ValueError): |
|
48 | except (ClassNotFound, ValueError): | |
|
49 | # guess_lexer will return a lexer if *any* lexer matches. There is | |||
|
50 | # no way to specify a minimum match score. This can give a high rate of | |||
|
51 | # false positives on files with an unknown filename pattern. | |||
|
52 | if guessfilenameonly: | |||
|
53 | return | |||
|
54 | ||||
49 | try: |
|
55 | try: | |
50 | lexer = guess_lexer(text[:1024], stripnl=False) |
|
56 | lexer = guess_lexer(text[:1024], stripnl=False) | |
51 | except (ClassNotFound, ValueError): |
|
57 | except (ClassNotFound, ValueError): |
@@ -644,4 +644,43 b' errors encountered' | |||||
644 | % hgweb filerevision, html |
|
644 | % hgweb filerevision, html | |
645 | % errors encountered |
|
645 | % errors encountered | |
646 |
|
646 | |||
|
647 | We attempt to highlight unknown files by default | |||
|
648 | ||||
|
649 | $ killdaemons.py | |||
|
650 | ||||
|
651 | $ cat > .hg/hgrc << EOF | |||
|
652 | > [web] | |||
|
653 | > highlightfiles = ** | |||
|
654 | > EOF | |||
|
655 | ||||
|
656 | $ cat > unknownfile << EOF | |||
|
657 | > #!/usr/bin/python | |||
|
658 | > def foo(): | |||
|
659 | > pass | |||
|
660 | > EOF | |||
|
661 | ||||
|
662 | $ hg add unknownfile | |||
|
663 | $ hg commit -m unknown unknownfile | |||
|
664 | ||||
|
665 | $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid | |||
|
666 | $ cat hg.pid >> $DAEMON_PIDS | |||
|
667 | ||||
|
668 | $ get-with-headers.py localhost:$HGPORT 'file/tip/unknownfile' | grep l2 | |||
|
669 | <span id="l2"><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span></span><a href="#l2"></a> | |||
|
670 | ||||
|
671 | We can prevent Pygments from falling back to a non filename-based | |||
|
672 | detection mode | |||
|
673 | ||||
|
674 | $ cat > .hg/hgrc << EOF | |||
|
675 | > [web] | |||
|
676 | > highlightfiles = ** | |||
|
677 | > highlightonlymatchfilename = true | |||
|
678 | > EOF | |||
|
679 | ||||
|
680 | $ killdaemons.py | |||
|
681 | $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid | |||
|
682 | $ cat hg.pid >> $DAEMON_PIDS | |||
|
683 | $ get-with-headers.py localhost:$HGPORT 'file/tip/unknownfile' | grep l2 | |||
|
684 | <span id="l2">def foo():</span><a href="#l2"></a> | |||
|
685 | ||||
647 | $ cd .. |
|
686 | $ cd .. |
General Comments 0
You need to be logged in to leave comments.
Login now