2 from docutils import nodes, utils
3 from docutils.parsers.rst import directives
4 from sphinx.util.compat import Directive
7 DEFAULT_PLACES = 'hbtp'
8 DEFAULT_PADDING = '0.1'
12 ############################################################################
14 class subcap(nodes.Part, nodes.TextElement):
17 def latex_visit_subcap(self, node):
20 def latex_depart_subcap(self, node):
21 self.body.append(']{')
24 class subfig(nodes.General, nodes.Element):
27 def latex_visit_subfig(self, node):
28 space = '\\hspace{%sin}\n' % node['space'] if node['space'] else ''
29 self.body.append(space)
30 self.body.append('\\subfigure')
32 def latex_depart_subfig(self, node):
33 self.body.append('}\n')
36 class SubfigDirective(Directive):
39 required_arguments = 0
40 optional_arguments = 1
41 final_argument_whitespace = True
43 label = directives.unchanged,
48 self.assert_has_content()
49 subfig_node = subfig('\n'.join(self.content))
50 subfig_node['label'] = self.options.get('label', None)
51 subfig_node['space'] = self.options.get('space', None)
52 if self.arguments and self.arguments[0]:
53 subfig_node['label'] = self.arguments[0]
54 node = nodes.Element() # anonymous container for parsing
55 self.state.nested_parse(self.content, self.content_offset, node)
57 if isinstance(first_node, nodes.paragraph):
58 cap = subcap(first_node.rawsource, '', *first_node.children)
60 subfig_node += node[1:]
62 subfig_node += subcap('', '', nodes.Text(''))
63 subfig_node += node[:]
68 ############################################################################
70 class pad(nodes.Part, nodes.TextElement):
73 def latex_visit_pad(self, node):
74 self.body.append('\\hspace{%sin}\n' % node['inches'])
76 def latex_depart_pad(self, node):
78 #self.body.append('\\hspace{%sin}\n' % DEFAULT_PADDING) #node['inches'])
81 class fig(nodes.General, nodes.Element):
84 def latex_visit_fig(self, node):
85 self.body.append('\\begin{figure}[%s]\n' % node['placement'])
86 self.body.append('' if node['nocenter'] else '\\centering\n')
88 def latex_depart_fig(self, node):
89 label = '\\label{%s}\n' % node['label'] if node['label'] else ''
90 self.body.append(label)
91 self.body.append('\\end{figure}\n\n')
93 class FigDirective(Directive):
96 required_arguments = 0
97 optional_arguments = 1
98 final_argument_whitespace = True
100 label = directives.unchanged,
101 placement = directives.unchanged,
103 nocenter = directives.flag,
107 self.assert_has_content()
108 fig_node = fig('\n'.join(self.content))
109 fig_node['label'] = self.options.get('label', None)
110 fig_node['placement'] = self.options.get('placement', DEFAULT_PLACES)
111 fig_node['padding'] = self.options.get('padding', DEFAULT_PADDING)
112 fig_node['nocenter'] = 'nocenter' in self.options
113 if self.arguments and self.arguments[0]:
114 fig_node['label'] = self.arguments[0]
115 node = nodes.Element() # anonymous container for parsing
116 self.state.nested_parse(self.content, self.content_offset, node)
119 if isinstance(first_node, nodes.paragraph):
120 cap = nodes.caption(first_node.rawsource, '', *first_node.children)
125 if isinstance(n, subfig):
129 children.append(pad('', '', nodes.Text(''),
130 inches=fig_node['padding']))
140 app.add_node(subcap, latex=(latex_visit_subcap, latex_depart_subcap))
141 app.add_node(subfig, latex=(latex_visit_subfig, latex_depart_subfig))
142 app.add_node(pad, latex=(latex_visit_pad, latex_depart_pad))
143 app.add_node(fig, latex=(latex_visit_fig, latex_depart_fig))
144 app.add_directive('fig', FigDirective)
145 app.add_directive('subfig', SubfigDirective)
148 # vim: set et sw=4 sts=4 :