2 from docutils import nodes, utils
3 from docutils.parsers.rst import directives
4 from sphinx.util.compat import Directive
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 subflt(nodes.General, nodes.Element):
27 def latex_visit_subflt(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_subflt(self, node):
33 self.body.append('}\n')
36 class SubfltDirective(Directive):
39 required_arguments = 0
40 optional_arguments = 1
41 final_argument_whitespace = True
43 label = directives.unchanged,
48 self.assert_has_content()
49 subflt_node = subflt('\n'.join(self.content))
50 subflt_node['label'] = self.options.get('label', None)
51 subflt_node['space'] = self.options.get('space', None)
52 if self.arguments and self.arguments[0]:
53 subflt_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 subflt_node += node[1:]
62 subflt_node += subcap('', '', nodes.Text(''))
63 subflt_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'])
101 ############################################################################
103 class flt(nodes.General, nodes.Element):
106 def latex_visit_flt(self, node):
107 self.body.append('\\begin{%s}[%s]\n' % (node['type'], node['placement']))
108 self.body.append('' if node['nocenter'] else '\\centering\n')
110 def latex_depart_flt(self, node):
111 label = '\\label{%s}\n' % node['label'] if node['label'] else ''
112 self.body.append(label)
113 self.body.append('\\end{%s}\n\n' % node['type'])
115 class FltDirective(Directive):
117 flt_types = ('figure', 'table')
118 def flt_type(argument):
119 # This is not callable as self.align. We cannot make it a
120 # staticmethod because we're saving an unbound method in
122 return directives.choice(argument, FltDirective.flt_types)
125 required_arguments = 0
126 optional_arguments = 1
127 final_argument_whitespace = True
130 label = directives.unchanged,
131 placement = directives.unchanged,
133 nocenter = directives.flag,
137 self.assert_has_content()
138 flt_node = flt('\n'.join(self.content))
139 flt_node['type'] = self.options.get('type', 'figure')
140 flt_node['label'] = self.options.get('label', None)
141 flt_node['placement'] = self.options.get('placement', DEFAULT_PLACES)
142 flt_node['padding'] = self.options.get('padding', DEFAULT_PADDING)
143 flt_node['nocenter'] = 'nocenter' in self.options
144 if self.arguments and self.arguments[0]:
145 flt_node['label'] = self.arguments[0]
146 node = nodes.Element() # anonymous container for parsing
147 self.state.nested_parse(self.content, self.content_offset, node)
152 second_node = node[1]
155 is_first_para = isinstance(first_node, nodes.paragraph)
156 is_second_para = isinstance(second_node, nodes.paragraph)
159 if is_first_para and is_second_para:
160 capt = captitle(first_node.rawsource, '', *first_node.children)
161 capd = capdesc(second_node.rawsource, '', *second_node.children)
164 capt = captitle(first_node.rawsource, '', *first_node.children)
165 capd = capdesc(first_node.rawsource+'.', '',
166 *(first_node.children + [nodes.Text('.')]))
169 capt = captitle(second_node.rawsource, '', *second_node.children)
170 capd = capdesc(second_node.rawsource+'.', '',
171 *(second_node.children + [nodes.Text('.')]))
176 if isinstance(n, subflt):
180 children.append(pad('', '', nodes.Text(''),
181 inches=flt_node['padding']))
192 app.add_node(subcap, latex=(latex_visit_subcap, latex_depart_subcap))
193 app.add_node(subflt, latex=(latex_visit_subflt, latex_depart_subflt))
194 app.add_node(captitle, latex=(latex_visit_captitle, latex_depart_captitle))
195 app.add_node(capdesc, latex=(latex_visit_capdesc, latex_depart_capdesc))
196 app.add_node(pad, latex=(latex_visit_pad, latex_depart_pad))
197 app.add_node(flt, latex=(latex_visit_flt, latex_depart_flt))
198 app.add_directive('flt', FltDirective)
199 app.add_directive('subflt', SubfltDirective)
202 # vim: set et sw=4 sts=4 :