~villemvainio/ipython/ipyrender-linuxfix

« back to all changes in this revision

Viewing changes to docs/sphinxext/only_directives.py

  • Committer: Fernando Perez
  • Date: 2008-09-14 04:27:13 UTC
  • mfrom: (1102.1.34 trunk-dev)
  • Revision ID: fernando.perez@berkeley.edu-20080914042713-piept3a5i2fdfuc1
Final doc updates for 0.9 release.

Unless problems arise in final testing, this revision will be 0.9.

Show diffs side-by-side

added added

removed removed

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