[aa36f96] | 1 | # |
---|
| 2 | # A pair of directives for inserting content that will only appear in |
---|
| 3 | # either html or latex. |
---|
| 4 | # |
---|
| 5 | |
---|
| 6 | from docutils.nodes import Body, Element |
---|
| 7 | from docutils.parsers.rst import directives |
---|
| 8 | |
---|
| 9 | class only_base(Body, Element): |
---|
| 10 | def dont_traverse(self, *args, **kwargs): |
---|
| 11 | return [] |
---|
| 12 | |
---|
| 13 | class html_only(only_base): |
---|
| 14 | pass |
---|
| 15 | |
---|
| 16 | class latex_only(only_base): |
---|
| 17 | pass |
---|
| 18 | |
---|
| 19 | def run(content, node_class, state, content_offset): |
---|
| 20 | text = '\n'.join(content) |
---|
| 21 | node = node_class(text) |
---|
| 22 | state.nested_parse(content, content_offset, node) |
---|
| 23 | return [node] |
---|
| 24 | |
---|
| 25 | def html_only_directive(name, arguments, options, content, lineno, |
---|
| 26 | content_offset, block_text, state, state_machine): |
---|
| 27 | return run(content, html_only, state, content_offset) |
---|
| 28 | |
---|
| 29 | def latex_only_directive(name, arguments, options, content, lineno, |
---|
| 30 | content_offset, block_text, state, state_machine): |
---|
| 31 | return run(content, latex_only, state, content_offset) |
---|
| 32 | |
---|
| 33 | def builder_inited(app): |
---|
| 34 | if app.builder.name == 'html': |
---|
| 35 | latex_only.traverse = only_base.dont_traverse |
---|
| 36 | else: |
---|
| 37 | html_only.traverse = only_base.dont_traverse |
---|
| 38 | |
---|
| 39 | def setup(app): |
---|
| 40 | app.add_directive('htmlonly', html_only_directive, True, (0, 0, 0)) |
---|
| 41 | app.add_directive('latexonly', latex_only_directive, True, (0, 0, 0)) |
---|
| 42 | app.add_node(html_only) |
---|
| 43 | app.add_node(latex_only) |
---|
| 44 | |
---|
| 45 | # This will *really* never see the light of day As it turns out, |
---|
| 46 | # this results in "broken" image nodes since they never get |
---|
| 47 | # processed, so best not to do this. |
---|
| 48 | # app.connect('builder-inited', builder_inited) |
---|
| 49 | |
---|
| 50 | # Add visit/depart methods to HTML-Translator: |
---|
| 51 | def visit_perform(self, node): |
---|
| 52 | pass |
---|
| 53 | def depart_perform(self, node): |
---|
| 54 | pass |
---|
| 55 | def visit_ignore(self, node): |
---|
| 56 | node.children = [] |
---|
| 57 | def depart_ignore(self, node): |
---|
| 58 | node.children = [] |
---|
| 59 | |
---|
| 60 | app.add_node(html_only, html=(visit_perform, depart_perform)) |
---|
| 61 | app.add_node(html_only, latex=(visit_ignore, depart_ignore)) |
---|
| 62 | app.add_node(latex_only, latex=(visit_perform, depart_perform)) |
---|
| 63 | app.add_node(latex_only, html=(visit_ignore, depart_ignore)) |
---|