grim/hgwebplus

Parents f6efc2034984
Children 0e516e743754
With a lot of help from av6 we created a keyword that dumps the rendered readme into the given page
  • +38 -3
    hgkeeper.py
  • --- a/hgkeeper.py Tue Apr 07 23:40:02 2020 -0500
    +++ b/hgkeeper.py Wed Apr 08 02:41:19 2020 -0500
    @@ -12,9 +12,11 @@
    registrar,
    templateutil
    )
    -from mercurial.hgweb import webutil
    +from mercurial.hgweb import webutil, hgweb_mod
    from mercurial.hgweb.common import paritygen
    +import cmarkgfm
    +
    filters = {}
    templatefilter = registrar.templatefilter(filters)
    @@ -200,6 +202,39 @@
    yield data
    +def repo_custom_templater(orig, self, req):
    + templater = orig(self, req)
    +
    + templatekeyword = registrar.templatekeyword(templater._proc._defaults)
    +
    + @templatekeyword(b'readme', requires={b'ctx', b'repo'})
    + def readme(context, mapping):
    + ctx = context.resource(mapping, b'ctx')
    +
    + # we iterate the files instead of a fileset, because we want to
    + # deterministically render readmes in the same order if there are more
    + # than one in a repository. With the fileset, we'd have to run through
    + # the generator and call .lower on each item again, and then we'd lose
    + # the ability to fallback if the prioritized one failed to render.
    + for filename in ctx:
    + lower = filename.lower()
    + if lower == b'readme.md':
    + raw_utf8 = ctx[filename].data().decode('utf-8')
    + return cmarkgfm.markdown_to_html(raw_utf8).encode('utf-8')
    +
    + if lower in [b'readme.txt', b'readme', b'readme.md']:
    + return ctx[filename].data().replace(b'\n', b'<br/>\n')
    +
    + return templater
    +
    +
    def extsetup(ui):
    - extensions.wrapfunction(webutil, "_diffstattmplgen", _diffstattmplgen)
    - extensions.wrapfunction(webutil, "_diffsgen", _diffsgen)
    + extensions.wrapfunction(webutil, '_diffstattmplgen', _diffstattmplgen)
    + extensions.wrapfunction(webutil, '_diffsgen', _diffsgen)
    +
    + # this nasty but amazingly working hack is from av6
    + extensions.wrapfunction(
    + hgweb_mod.requestcontext,
    + 'templater',
    + repo_custom_templater,
    + )