|
| 1 | +from docutils.parsers.rst import Directive |
| 2 | +from docutils.parsers.rst import directives |
| 3 | +from docutils.nodes import Element |
| 4 | +from sphinx.builders.html import StandaloneHTMLBuilder |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +class_map = { |
| 9 | + 'read': 'glyphicons-book-open', |
| 10 | + 'write': 'glyphicons-edit', |
| 11 | + 'transaction': 'glyphicons-database-plus', |
| 12 | + 'import_schema': 'glyphicons-disk-import' |
| 13 | +} |
| 14 | + |
| 15 | +class api_compat(Element): |
| 16 | + |
| 17 | + def __init__(self, api=None): |
| 18 | + self.api = api or {} |
| 19 | + super(api_compat, self).__init__() |
| 20 | + |
| 21 | +def visit_api_compat_node_html(self, node): |
| 22 | + self.body.append("<span>Supports: %s" % "".join( |
| 23 | + ['<span class="%s" title="%s">%s</span>' % |
| 24 | + (class_map.get(key), key, key) |
| 25 | + for key in node.api])) |
| 26 | + |
| 27 | + |
| 28 | +def depart_api_compat_node_html(self, node): |
| 29 | + self.body.append("</span>") |
| 30 | + |
| 31 | + |
| 32 | +def visit_api_compat_node_text(self, node): |
| 33 | + self.add_text("Supported API: %s" % ",".join(node.api)) |
| 34 | + |
| 35 | + |
| 36 | +def depart_api_compat_node_text(self, node): |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +def visit_api_compat_node_latex(self, node): |
| 41 | + # TODO: make it render in latex |
| 42 | + classes = node.get('classes', []) |
| 43 | + self.body.append(r'\DUspan{%s}{' % ','.join(classes)) |
| 44 | + |
| 45 | + |
| 46 | +def depart_api_compat_node_latex(self, node): |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +def setup(app): |
| 55 | + app.add_directive('api_compat', APICompatDirective) |
| 56 | + app.add_node(api_compat, |
| 57 | + html=(visit_api_compat_node_html, depart_api_compat_node_html), |
| 58 | + latex=(visit_api_compat_node_latex, depart_api_compat_node_latex), |
| 59 | + text=(visit_api_compat_node_text, depart_api_compat_node_text)) |
| 60 | + |
| 61 | + |
| 62 | +class APICompatDirective(Directive): |
| 63 | + |
| 64 | + has_content = True |
| 65 | + |
| 66 | + option_spec = { |
| 67 | + 'read': directives.flag, |
| 68 | + 'write': directives.flag, |
| 69 | + 'transaction': directives.flag, |
| 70 | + 'import_schema': directives.flag |
| 71 | + } |
| 72 | + |
| 73 | + def run(self): |
| 74 | + values = {key: key in self.options |
| 75 | + for key in self.option_spec} |
| 76 | + return [api_compat(api=values)] |
0 commit comments