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('\\subfloat')
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 captitle(nodes.Part, nodes.TextElement):
73 def latex_visit_captitle(self, node):
74 self.body.append('\\caption[')
76 def latex_depart_captitle(self, node):
79 class capdesc(nodes.Part, nodes.TextElement):
82 def latex_visit_capdesc(self, node):
85 def latex_depart_capdesc(self, node):
86 self.body.append('}\n')
88 class pad(nodes.Part, nodes.TextElement):
91 def latex_visit_pad(self, node):
92 self.body.append('\\hspace{%sin}\n' % node['inches'])
94 def latex_depart_pad(self, node):
96 #self.body.append('\\hspace{%sin}\n' % DEFAULT_PADDING) #node['inches'])
99 class fig(nodes.General, nodes.Element):
102 def latex_visit_fig(self, node):
103 self.body.append('\\begin{figure}[%s]\n' % node['placement'])
104 self.body.append('' if node['nocenter'] else '\\centering\n')
106 def latex_depart_fig(self, node):
107 label = '\\label{%s}\n' % node['label'] if node['label'] else ''
108 self.body.append(label)
109 self.body.append('\\end{figure}\n\n')
111 class FigDirective(Directive):
114 required_arguments = 0
115 optional_arguments = 1
116 final_argument_whitespace = True
118 label = directives.unchanged,
119 placement = directives.unchanged,
121 nocenter = directives.flag,
125 self.assert_has_content()
126 fig_node = fig('\n'.join(self.content))
127 fig_node['label'] = self.options.get('label', None)
128 fig_node['placement'] = self.options.get('placement', DEFAULT_PLACES)
129 fig_node['padding'] = self.options.get('padding', DEFAULT_PADDING)
130 fig_node['nocenter'] = 'nocenter' in self.options
131 if self.arguments and self.arguments[0]:
132 fig_node['label'] = self.arguments[0]
133 node = nodes.Element() # anonymous container for parsing
134 self.state.nested_parse(self.content, self.content_offset, node)
139 second_node = node[1]
142 is_first_para = isinstance(first_node, nodes.paragraph)
143 is_second_para = isinstance(second_node, nodes.paragraph)
146 if is_first_para and is_second_para:
147 capt = captitle(first_node.rawsource, '', *first_node.children)
148 capd = capdesc(second_node.rawsource, '', *second_node.children)
151 capt = captitle(first_node.rawsource, '', *first_node.children)
152 capd = capdesc(first_node.rawsource, '', *first_node.children)
155 capt = captitle(second_node.rawsource, '', *second_node.children)
156 capd = capdesc(second_node.rawsource, '', *second_node.children)
161 if isinstance(n, subfig):
165 children.append(pad('', '', nodes.Text(''),
166 inches=fig_node['padding']))
177 app.add_node(subcap, latex=(latex_visit_subcap, latex_depart_subcap))
178 app.add_node(subfig, latex=(latex_visit_subfig, latex_depart_subfig))
179 app.add_node(captitle, latex=(latex_visit_captitle, latex_depart_captitle))
180 app.add_node(capdesc, latex=(latex_visit_capdesc, latex_depart_capdesc))
181 app.add_node(pad, latex=(latex_visit_pad, latex_depart_pad))
182 app.add_node(fig, latex=(latex_visit_fig, latex_depart_fig))
183 app.add_directive('fig', FigDirective)
184 app.add_directive('subfig', SubfigDirective)
187 # vim: set et sw=4 sts=4 :