hgext/highlight.py
changeset 6394 55bc0a035e1f
parent 6393 894875eae49b
child 6485 938319418d8c
equal deleted inserted replaced
6393:894875eae49b 6394:55bc0a035e1f
    18 The default is 'colorful'.  If this is changed the corresponding CSS
    18 The default is 'colorful'.  If this is changed the corresponding CSS
    19 file should be re-generated by running
    19 file should be re-generated by running
    20 
    20 
    21 # pygmentize -f html -S <newstyle>
    21 # pygmentize -f html -S <newstyle>
    22 
    22 
    23 
       
    24 -- Adam Hupp <adam@hupp.org>
    23 -- Adam Hupp <adam@hupp.org>
    25 
       
    26 
       
    27 """
    24 """
    28 
    25 
    29 from mercurial import demandimport
    26 from mercurial import demandimport
    30 demandimport.ignore.extend(['pkgutil',
    27 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',])
    31                             'pkg_resources',
       
    32                             '__main__',])
       
    33 
    28 
    34 from mercurial.hgweb import webcommands, webutil
    29 from mercurial.hgweb import webcommands, webutil
    35 from mercurial import util
    30 from mercurial import util
    36 from mercurial.templatefilters import filters
    31 from mercurial.templatefilters import filters
    37 
    32 
    41 from pygments.formatters import HtmlFormatter
    36 from pygments.formatters import HtmlFormatter
    42 
    37 
    43 SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
    38 SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
    44               'type="text/css" />')
    39               'type="text/css" />')
    45 
    40 
    46 def pygmentize(self, tmpl, fctx, field):
    41 def pygmentize(field, fctx, style, tmpl):
       
    42 
    47     # append a <link ...> to the syntax highlighting css
    43     # append a <link ...> to the syntax highlighting css
    48     old_header = ''.join(tmpl('header'))
    44     old_header = ''.join(tmpl('header'))
    49     if SYNTAX_CSS not in old_header:
    45     if SYNTAX_CSS not in old_header:
    50         new_header =  old_header + SYNTAX_CSS
    46         new_header =  old_header + SYNTAX_CSS
    51         tmpl.cache['header'] = new_header
    47         tmpl.cache['header'] = new_header
    52 
    48 
    53     text = fctx.data()
    49     text = fctx.data()
    54     if util.binary(text):
    50     if util.binary(text):
    55         return
    51         return
    56 
    52 
    57     style = self.config("web", "pygments_style", "colorful")
       
    58     # To get multi-line strings right, we can't format line-by-line
    53     # To get multi-line strings right, we can't format line-by-line
    59     try:
    54     try:
    60         lexer = guess_lexer_for_filename(fctx.path(), text,
    55         lexer = guess_lexer_for_filename(fctx.path(), text,
    61                                          encoding=util._encoding)
    56                                          encoding=util._encoding)
    62     except ClassNotFound:
    57     except ClassNotFound:
    81 
    76 
    82 web_filerevision = webcommands._filerevision
    77 web_filerevision = webcommands._filerevision
    83 web_annotate = webcommands.annotate
    78 web_annotate = webcommands.annotate
    84 
    79 
    85 def filerevision_highlight(web, tmpl, fctx):
    80 def filerevision_highlight(web, tmpl, fctx):
    86     pygmentize(web, tmpl, fctx, 'fileline')
    81     style = web.config('web', 'pygments_style', 'colorful')
       
    82     pygmentize('fileline', fctx, style, tmpl)
    87     return web_filerevision(web, tmpl, fctx)
    83     return web_filerevision(web, tmpl, fctx)
    88 
    84 
    89 def annotate_highlight(web, req, tmpl):
    85 def annotate_highlight(web, req, tmpl):
    90     fctx = webutil.filectx(web.repo, req)
    86     fctx = webutil.filectx(web.repo, req)
    91     pygmentize(web, tmpl, fctx, 'annotateline')
    87     style = web.config('web', 'pygments_style', 'colorful')
       
    88     pygmentize('annotateline', fctx, style, tmpl)
    92     return web_annotate(web, req, tmpl)
    89     return web_annotate(web, req, tmpl)
    93 
    90 
    94 # monkeypatch in the new version
    91 # monkeypatch in the new version
    95 
    92 
    96 webcommands._filerevision = filerevision_highlight
    93 webcommands._filerevision = filerevision_highlight