hgext/highlight/__init__.py
changeset 43077 687b865b95ad
parent 43076 2372284d9457
child 44009 e685fac56693
equal deleted inserted replaced
43076:2372284d9457 43077:687b865b95ad
    41 
    41 
    42 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
    42 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
    43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    44 # be specifying the version(s) of Mercurial they are tested with, or
    44 # be specifying the version(s) of Mercurial they are tested with, or
    45 # leave the attribute unspecified.
    45 # leave the attribute unspecified.
    46 testedwith = 'ships-with-hg-core'
    46 testedwith = b'ships-with-hg-core'
    47 
    47 
    48 
    48 
    49 def pygmentize(web, field, fctx, tmpl):
    49 def pygmentize(web, field, fctx, tmpl):
    50     style = web.config('web', 'pygments_style', 'colorful')
    50     style = web.config(b'web', b'pygments_style', b'colorful')
    51     expr = web.config('web', 'highlightfiles', "size('<5M')")
    51     expr = web.config(b'web', b'highlightfiles', b"size('<5M')")
    52     filenameonly = web.configbool('web', 'highlightonlymatchfilename', False)
    52     filenameonly = web.configbool(b'web', b'highlightonlymatchfilename', False)
    53 
    53 
    54     ctx = fctx.changectx()
    54     ctx = fctx.changectx()
    55     m = ctx.matchfileset(expr)
    55     m = ctx.matchfileset(expr)
    56     if m(fctx.path()):
    56     if m(fctx.path()):
    57         highlight.pygmentize(
    57         highlight.pygmentize(
    58             field, fctx, style, tmpl, guessfilenameonly=filenameonly
    58             field, fctx, style, tmpl, guessfilenameonly=filenameonly
    59         )
    59         )
    60 
    60 
    61 
    61 
    62 def filerevision_highlight(orig, web, fctx):
    62 def filerevision_highlight(orig, web, fctx):
    63     mt = web.res.headers['Content-Type']
    63     mt = web.res.headers[b'Content-Type']
    64     # only pygmentize for mimetype containing 'html' so we both match
    64     # only pygmentize for mimetype containing 'html' so we both match
    65     # 'text/html' and possibly 'application/xhtml+xml' in the future
    65     # 'text/html' and possibly 'application/xhtml+xml' in the future
    66     # so that we don't have to touch the extension when the mimetype
    66     # so that we don't have to touch the extension when the mimetype
    67     # for a template changes; also hgweb optimizes the case that a
    67     # for a template changes; also hgweb optimizes the case that a
    68     # raw file is sent using rawfile() and doesn't call us, so we
    68     # raw file is sent using rawfile() and doesn't call us, so we
    69     # can't clash with the file's content-type here in case we
    69     # can't clash with the file's content-type here in case we
    70     # pygmentize a html file
    70     # pygmentize a html file
    71     if 'html' in mt:
    71     if b'html' in mt:
    72         pygmentize(web, 'fileline', fctx, web.tmpl)
    72         pygmentize(web, b'fileline', fctx, web.tmpl)
    73 
    73 
    74     return orig(web, fctx)
    74     return orig(web, fctx)
    75 
    75 
    76 
    76 
    77 def annotate_highlight(orig, web):
    77 def annotate_highlight(orig, web):
    78     mt = web.res.headers['Content-Type']
    78     mt = web.res.headers[b'Content-Type']
    79     if 'html' in mt:
    79     if b'html' in mt:
    80         fctx = webutil.filectx(web.repo, web.req)
    80         fctx = webutil.filectx(web.repo, web.req)
    81         pygmentize(web, 'annotateline', fctx, web.tmpl)
    81         pygmentize(web, b'annotateline', fctx, web.tmpl)
    82 
    82 
    83     return orig(web)
    83     return orig(web)
    84 
    84 
    85 
    85 
    86 def generate_css(web):
    86 def generate_css(web):
    87     pg_style = web.config('web', 'pygments_style', 'colorful')
    87     pg_style = web.config(b'web', b'pygments_style', b'colorful')
    88     fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style))
    88     fmter = highlight.HtmlFormatter(style=pycompat.sysstr(pg_style))
    89     web.res.headers['Content-Type'] = 'text/css'
    89     web.res.headers[b'Content-Type'] = b'text/css'
    90     style_defs = fmter.get_style_defs(pycompat.sysstr(''))
    90     style_defs = fmter.get_style_defs(pycompat.sysstr(b''))
    91     web.res.setbodybytes(
    91     web.res.setbodybytes(
    92         ''.join(
    92         b''.join(
    93             [
    93             [
    94                 '/* pygments_style = %s */\n\n' % pg_style,
    94                 b'/* pygments_style = %s */\n\n' % pg_style,
    95                 pycompat.bytestr(style_defs),
    95                 pycompat.bytestr(style_defs),
    96             ]
    96             ]
    97         )
    97         )
    98     )
    98     )
    99     return web.res.sendresponse()
    99     return web.res.sendresponse()
   100 
   100 
   101 
   101 
   102 def extsetup(ui):
   102 def extsetup(ui):
   103     # monkeypatch in the new version
   103     # monkeypatch in the new version
   104     extensions.wrapfunction(
   104     extensions.wrapfunction(
   105         webcommands, '_filerevision', filerevision_highlight
   105         webcommands, b'_filerevision', filerevision_highlight
   106     )
   106     )
   107     extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
   107     extensions.wrapfunction(webcommands, b'annotate', annotate_highlight)
   108     webcommands.highlightcss = generate_css
   108     webcommands.highlightcss = generate_css
   109     webcommands.__all__.append('highlightcss')
   109     webcommands.__all__.append(b'highlightcss')