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