Debian Codegraph @

hgext/highlight/highlight.py

raw
1 # highlight.py - highlight extension implementation file
2 #
3 # Copyright 2007-2009 Adam Hupp <[email protected]> and others
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7 #
8 # The original module was split in an interface and an implementation
9 # file to defer pygments loading and speedup extension setup.
10
11 from __future__ import annotations
12
13 from mercurial import demandimport
14
15 demandimport.IGNORES.update(['pkgutil', 'pkg_resources', '__main__'])
16
17 from mercurial import (
18 encoding,
19 pycompat,
20 )
21
22 from mercurial.utils import stringutil
23
24 with demandimport.deactivated():
25 import pygments
26 import pygments.formatters
27 import pygments.lexers
28 import pygments.plugin
29 import pygments.util
30
31 for unused in pygments.plugin.find_plugin_lexers():
32 pass
33
34 highlight = pygments.highlight
35 ClassNotFound = pygments.util.ClassNotFound
36 guess_lexer = pygments.lexers.guess_lexer
37 guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename
38 TextLexer = pygments.lexers.TextLexer
39 HtmlFormatter = pygments.formatters.HtmlFormatter
40
41 SYNTAX_CSS = (
42 b'\n<link rel="stylesheet" href="{url}highlightcss" type="text/css" />'
43 )
44
45
46 def pygmentize(field, fctx, style, tmpl, guessfilenameonly=False):
47 # append a <link ...> to the syntax highlighting css
48 tmpl.load(b'header')
49 old_header = tmpl.cache[b'header']
50 if SYNTAX_CSS not in old_header:
51 new_header = old_header + SYNTAX_CSS
52 tmpl.cache[b'header'] = new_header
53
54 text = fctx.data()
55 if stringutil.binary(text):
56 return
57
58 # str.splitlines() != unicode.splitlines() because "reasons"
59 for c in b"\x0c", b"\x1c", b"\x1d", b"\x1e":
60 if c in text:
61 text = text.replace(c, b'')
62
63 # Pygments is best used with Unicode strings:
64 # <http://pygments.org/docs/unicode/>
65 text = text.decode(pycompat.sysstr(encoding.encoding), 'replace')
66
67 # To get multi-line strings right, we can't format line-by-line
68 try:
69 path = pycompat.sysstr(fctx.path())
70 lexer = guess_lexer_for_filename(path, text[:1024], stripnl=False)
71 except (ClassNotFound, ValueError):
72 # guess_lexer will return a lexer if *any* lexer matches. There is
73 # no way to specify a minimum match score. This can give a high rate of
74 # false positives on files with an unknown filename pattern.
75 if guessfilenameonly:
76 return
77
78 try:
79 lexer = guess_lexer(text[:1024], stripnl=False)
80 except (ClassNotFound, ValueError):
81 # Don't highlight unknown files
82 return
83
84 # Don't highlight text files
85 if isinstance(lexer, TextLexer):
86 return
87
88 formatter = HtmlFormatter(nowrap=True, style=pycompat.sysstr(style))
89
90 colorized = highlight(text, lexer, formatter)
91 coloriter = (
92 s.encode(pycompat.sysstr(encoding.encoding), 'replace')
93 for s in colorized.splitlines()
94 )
95
96 tmpl._filters[b'colorize'] = lambda x: next(coloriter)
97
98 oldl = tmpl.cache[field]
99 newl = oldl.replace(b'line|escape', b'line|colorize')
100 tmpl.cache[field] = newl