hgext/highlight/highlight.py
changeset 42923 a7abc6081bc5
parent 38383 23dc901cdf13
child 43076 2372284d9457
equal deleted inserted replaced
42922:8d9322b6e687 42923:a7abc6081bc5
    13 from mercurial import demandimport
    13 from mercurial import demandimport
    14 demandimport.IGNORES.update(['pkgutil', 'pkg_resources', '__main__'])
    14 demandimport.IGNORES.update(['pkgutil', 'pkg_resources', '__main__'])
    15 
    15 
    16 from mercurial import (
    16 from mercurial import (
    17     encoding,
    17     encoding,
       
    18     pycompat,
    18 )
    19 )
    19 
    20 
    20 from mercurial.utils import (
    21 from mercurial.utils import (
    21     stringutil,
    22     stringutil,
    22 )
    23 )
    59         if c in text:
    60         if c in text:
    60             text = text.replace(c, '')
    61             text = text.replace(c, '')
    61 
    62 
    62     # Pygments is best used with Unicode strings:
    63     # Pygments is best used with Unicode strings:
    63     # <http://pygments.org/docs/unicode/>
    64     # <http://pygments.org/docs/unicode/>
    64     text = text.decode(encoding.encoding, 'replace')
    65     text = text.decode(pycompat.sysstr(encoding.encoding), 'replace')
    65 
    66 
    66     # To get multi-line strings right, we can't format line-by-line
    67     # To get multi-line strings right, we can't format line-by-line
    67     try:
    68     try:
    68         lexer = guess_lexer_for_filename(fctx.path(), text[:1024],
    69         path = pycompat.sysstr(fctx.path())
       
    70         lexer = guess_lexer_for_filename(path, text[:1024],
    69                                          stripnl=False)
    71                                          stripnl=False)
    70     except (ClassNotFound, ValueError):
    72     except (ClassNotFound, ValueError):
    71         # guess_lexer will return a lexer if *any* lexer matches. There is
    73         # guess_lexer will return a lexer if *any* lexer matches. There is
    72         # no way to specify a minimum match score. This can give a high rate of
    74         # no way to specify a minimum match score. This can give a high rate of
    73         # false positives on files with an unknown filename pattern.
    75         # false positives on files with an unknown filename pattern.
    82 
    84 
    83     # Don't highlight text files
    85     # Don't highlight text files
    84     if isinstance(lexer, TextLexer):
    86     if isinstance(lexer, TextLexer):
    85         return
    87         return
    86 
    88 
    87     formatter = HtmlFormatter(nowrap=True, style=style)
    89     formatter = HtmlFormatter(nowrap=True, style=pycompat.sysstr(style))
    88 
    90 
    89     colorized = highlight(text, lexer, formatter)
    91     colorized = highlight(text, lexer, formatter)
    90     coloriter = (s.encode(encoding.encoding, 'replace')
    92     coloriter = (s.encode(pycompat.sysstr(encoding.encoding), 'replace')
    91                  for s in colorized.splitlines())
    93                  for s in colorized.splitlines())
    92 
    94 
    93     tmpl._filters['colorize'] = lambda x: next(coloriter)
    95     tmpl._filters['colorize'] = lambda x: next(coloriter)
    94 
    96 
    95     oldl = tmpl.cache[field]
    97     oldl = tmpl.cache[field]