Show More
@@ -1,87 +1,93 b'' | |||
|
1 | 1 | # |
|
2 | 2 | # A pair of directives for inserting content that will only appear in |
|
3 | 3 | # either html or latex. |
|
4 | 4 | # |
|
5 | 5 | |
|
6 | 6 | from docutils.nodes import Body, Element |
|
7 | 7 | from docutils.writers.html4css1 import HTMLTranslator |
|
8 | from sphinx.latexwriter import LaTeXTranslator | |
|
9 | 8 | from docutils.parsers.rst import directives |
|
10 | 9 | |
|
10 | # The sphinx API has changed, so we try both the old and new import forms | |
|
11 | try: | |
|
12 | from sphinx.latexwriter import LaTeXTranslator | |
|
13 | except ImportError: | |
|
14 | from sphinx.writers.latex import LaTeXTranslator | |
|
15 | ||
|
16 | ||
|
11 | 17 | class html_only(Body, Element): |
|
12 | 18 | pass |
|
13 | 19 | |
|
14 | 20 | class latex_only(Body, Element): |
|
15 | 21 | pass |
|
16 | 22 | |
|
17 | 23 | def run(content, node_class, state, content_offset): |
|
18 | 24 | text = '\n'.join(content) |
|
19 | 25 | node = node_class(text) |
|
20 | 26 | state.nested_parse(content, content_offset, node) |
|
21 | 27 | return [node] |
|
22 | 28 | |
|
23 | 29 | try: |
|
24 | 30 | from docutils.parsers.rst import Directive |
|
25 | 31 | except ImportError: |
|
26 | 32 | from docutils.parsers.rst.directives import _directives |
|
27 | 33 | |
|
28 | 34 | def html_only_directive(name, arguments, options, content, lineno, |
|
29 | 35 | content_offset, block_text, state, state_machine): |
|
30 | 36 | return run(content, html_only, state, content_offset) |
|
31 | 37 | |
|
32 | 38 | def latex_only_directive(name, arguments, options, content, lineno, |
|
33 | 39 | content_offset, block_text, state, state_machine): |
|
34 | 40 | return run(content, latex_only, state, content_offset) |
|
35 | 41 | |
|
36 | 42 | for func in (html_only_directive, latex_only_directive): |
|
37 | 43 | func.content = 1 |
|
38 | 44 | func.options = {} |
|
39 | 45 | func.arguments = None |
|
40 | 46 | |
|
41 | 47 | _directives['htmlonly'] = html_only_directive |
|
42 | 48 | _directives['latexonly'] = latex_only_directive |
|
43 | 49 | else: |
|
44 | 50 | class OnlyDirective(Directive): |
|
45 | 51 | has_content = True |
|
46 | 52 | required_arguments = 0 |
|
47 | 53 | optional_arguments = 0 |
|
48 | 54 | final_argument_whitespace = True |
|
49 | 55 | option_spec = {} |
|
50 | 56 | |
|
51 | 57 | def run(self): |
|
52 | 58 | self.assert_has_content() |
|
53 | 59 | return run(self.content, self.node_class, |
|
54 | 60 | self.state, self.content_offset) |
|
55 | 61 | |
|
56 | 62 | class HtmlOnlyDirective(OnlyDirective): |
|
57 | 63 | node_class = html_only |
|
58 | 64 | |
|
59 | 65 | class LatexOnlyDirective(OnlyDirective): |
|
60 | 66 | node_class = latex_only |
|
61 | 67 | |
|
62 | 68 | directives.register_directive('htmlonly', HtmlOnlyDirective) |
|
63 | 69 | directives.register_directive('latexonly', LatexOnlyDirective) |
|
64 | 70 | |
|
65 | 71 | def setup(app): |
|
66 | 72 | app.add_node(html_only) |
|
67 | 73 | app.add_node(latex_only) |
|
68 | 74 | |
|
69 | 75 | # Add visit/depart methods to HTML-Translator: |
|
70 | 76 | def visit_perform(self, node): |
|
71 | 77 | pass |
|
72 | 78 | def depart_perform(self, node): |
|
73 | 79 | pass |
|
74 | 80 | def visit_ignore(self, node): |
|
75 | 81 | node.children = [] |
|
76 | 82 | def depart_ignore(self, node): |
|
77 | 83 | node.children = [] |
|
78 | 84 | |
|
79 | 85 | HTMLTranslator.visit_html_only = visit_perform |
|
80 | 86 | HTMLTranslator.depart_html_only = depart_perform |
|
81 | 87 | HTMLTranslator.visit_latex_only = visit_ignore |
|
82 | 88 | HTMLTranslator.depart_latex_only = depart_ignore |
|
83 | 89 | |
|
84 | 90 | LaTeXTranslator.visit_html_only = visit_ignore |
|
85 | 91 | LaTeXTranslator.depart_html_only = depart_ignore |
|
86 | 92 | LaTeXTranslator.visit_latex_only = visit_perform |
|
87 | 93 | LaTeXTranslator.depart_latex_only = depart_perform |
General Comments 0
You need to be logged in to leave comments.
Login now