X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=doxygen%2Fsiphon%2Fprocess.py;h=57e323e48acbf2dfba729b9a0d9c9d68d95693b7;hb=70f879d2852dfc042ad0911a4a6e4a1714c0eb83;hp=34e829c5298dd5aa09b2956af274a6394ab2487f;hpb=af405f77c74d8b41523d4e7f39a91c7aec2fd93c;p=vpp.git diff --git a/doxygen/siphon/process.py b/doxygen/siphon/process.py index 34e829c5298..57e323e48ac 100644 --- a/doxygen/siphon/process.py +++ b/doxygen/siphon/process.py @@ -14,15 +14,24 @@ # Generation template class -import logging, os,sys, cgi, json, jinja2, HTMLParser +import html.parser +import json +import logging +import os +import sys + +import jinja2 # Classes register themselves in this dictionary """Mapping of known processors to their classes""" siphons = {} +"""Mapping of known output formats to their classes""" +formats = {} + -"""Generate rendered output for siphoned data.""" class Siphon(object): + """Generate rendered output for siphoned data.""" # Set by subclasses """Our siphon name""" @@ -51,29 +60,36 @@ class Siphon(object): """Template environment, if we're using templates""" _tplenv = None - def __init__(self, template_directory=None): + def __init__(self, template_directory, format): super(Siphon, self).__init__() self.log = logging.getLogger("siphon.process.%s" % self.name) - if template_directory is not None: - self.template_directory = template_directory - searchpath = [ - template_directory + "/" + self.name, - template_directory + "/" + "default", - ] - loader = jinja2.FileSystemLoader(searchpath=searchpath) - self._tplenv = jinja2.Environment( - loader=loader, - trim_blocks=True, - keep_trailing_newline=True) - - # Convenience, get a reference to the internal escape and - # unescape methods in cgi and HTMLParser. These then become - # available to templates to use, if needed. - self._h = HTMLParser.HTMLParser() - self.escape = cgi.escape - self.unescape = self._h.unescape - + # Get our output format details + fmt_klass = formats[format] + fmt = fmt_klass() + self._format = fmt + + # Sort out the template search path + def _tpldir(name): + return os.sep.join((template_directory, fmt.name, name)) + + self.template_directory = template_directory + searchpath = [ + _tpldir(self.name), + _tpldir("default"), + ] + loader = jinja2.FileSystemLoader(searchpath=searchpath) + self._tplenv = jinja2.Environment( + loader=loader, + trim_blocks=True, + keep_trailing_newline=True) + + # Convenience, get a reference to the internal escape and + # unescape methods in html.parser. These then become + # available to templates to use, if needed. + self._h = html.parser.HTMLParser() + self.escape = html.escape + self.unescape = html.unescape # Output renderers @@ -157,12 +173,11 @@ class Siphon(object): """Template processor""" def template(self, name, **kwargs): - tpl = self._tplenv.get_template(name + ".md") - return tpl.render( + tpl = self._tplenv.get_template(name + self._format.extension) + return tpl.render( this=self, **kwargs) - # Processing methods """Parse the input file into a more usable dictionary structure.""" @@ -185,11 +200,11 @@ class Siphon(object): for item in data["items"]: try: o = self._parser.parse(item['block']) - except: - self.log.error("Exception parsing item: %s\n%s" \ - % (json.dumps(item, separators=(',', ': '), - indent=4), - item['block'])) + except Exception: + self.log.error("Exception parsing item: %s\n%s" + % (json.dumps(item, separators=(',', ': '), + indent=4), + item['block'])) raise # Augment the item with metadata @@ -236,8 +251,8 @@ class Siphon(object): if group.startswith('_'): continue - self.log.info("Processing items in group \"%s\" (%s)." % \ - (group, group_sort_key(group))) + self.log.info("Processing items in group \"%s\" (%s)." % + (group, group_sort_key(group))) # Generate the section index entry (write it now) out.write(self.index_section(group)) @@ -249,8 +264,8 @@ class Siphon(object): return self.item_sort_key(self._cmds[group][key]) for key in sorted(self._cmds[group].keys(), key=item_sort_key): - self.log.debug("--- Processing key \"%s\" (%s)." % \ - (key, item_sort_key(key))) + self.log.debug("--- Processing key \"%s\" (%s)." % + (key, item_sort_key(key))) o = self._cmds[group][key] meta = { @@ -270,3 +285,33 @@ class Siphon(object): # Deliver the accumulated body output out.write(contents) + + +class Format(object): + """Output format class""" + + """Name of this output format""" + name = None + + """Expected file extension of templates that build this format""" + extension = None + + +class FormatMarkdown(Format): + """Markdown output format""" + name = "markdown" + extension = ".md" + + +# Register 'markdown' +formats["markdown"] = FormatMarkdown + + +class FormatItemlist(Format): + """Itemlist output format""" + name = "itemlist" + extension = ".itemlist" + + +# Register 'itemlist' +formats["itemlist"] = FormatItemlist