--- /dev/null
+
+from docutils import nodes, utils
+from docutils.parsers.rst import directives
+from sphinx.util.compat import Directive
+
+
+DEFAULT_PLACES = 'hbtp'
+DEFAULT_PADDING = '0.1'
+
+
+# subtable
+############################################################################
+
+class subtcap(nodes.Part, nodes.TextElement):
+ pass
+
+def latex_visit_subtcap(self, node):
+ self.body.append('[')
+
+def latex_depart_subtcap(self, node):
+ self.body.append(']{')
+
+
+class subtable(nodes.General, nodes.Element):
+ pass
+
+def latex_visit_subtable(self, node):
+ space = '\\hspace{%sin}\n' % node['space'] if node['space'] else ''
+ self.body.append(space)
+ self.body.append('\\subfloat')
+
+def latex_depart_subtable(self, node):
+ self.body.append('}\n')
+
+
+class SubtableDirective(Directive):
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 1
+ final_argument_whitespace = True
+ option_spec = dict(
+ label = directives.unchanged,
+ space = float,
+ )
+
+ def run(self):
+ self.assert_has_content()
+ subtable_node = subtable('\n'.join(self.content))
+ subtable_node['label'] = self.options.get('label', None)
+ subtable_node['space'] = self.options.get('space', None)
+ if self.arguments and self.arguments[0]:
+ subtable_node['label'] = self.arguments[0]
+ node = nodes.Element() # anonymous container for parsing
+ self.state.nested_parse(self.content, self.content_offset, node)
+ first_node = node[0]
+ if isinstance(first_node, nodes.paragraph):
+ cap = subtcap(first_node.rawsource, '', *first_node.children)
+ subtable_node += cap
+ subtable_node += node[1:]
+ else:
+ subtable_node += subtcap('', '', nodes.Text(''))
+ subtable_node += node[:]
+ return [subtable_node]
+
+
+# ftable
+############################################################################
+
+class captitle(nodes.Part, nodes.TextElement):
+ pass
+
+def latex_visit_captitle(self, node):
+ self.body.append('\\caption[')
+
+def latex_depart_captitle(self, node):
+ self.body.append(']')
+
+class capdesc(nodes.Part, nodes.TextElement):
+ pass
+
+def latex_visit_capdesc(self, node):
+ self.body.append('{')
+
+def latex_depart_capdesc(self, node):
+ self.body.append('}\n')
+
+class tpad(nodes.Part, nodes.TextElement):
+ pass
+
+def latex_visit_tpad(self, node):
+ self.body.append('\\hspace{%sin}\n' % node['inches'])
+
+def latex_depart_tpad(self, node):
+ pass
+ #self.body.append('\\hspace{%sin}\n' % DEFAULT_PADDING) #node['inches'])
+
+
+class ftable(nodes.General, nodes.Element):
+ pass
+
+def latex_visit_ftable(self, node):
+ self.body.append('\\begin{table}[%s]\n' % node['placement'])
+ self.body.append('' if node['nocenter'] else '\\centering\n')
+
+def latex_depart_ftable(self, node):
+ label = '\\label{%s}\n' % node['label'] if node['label'] else ''
+ self.body.append(label)
+ self.body.append('\\end{table}\n\n')
+
+class FTableDirective(Directive):
+
+ has_content = True
+ required_arguments = 0
+ optional_arguments = 1
+ final_argument_whitespace = True
+ option_spec = dict(
+ label = directives.unchanged,
+ placement = directives.unchanged,
+ padding = float,
+ nocenter = directives.flag,
+ )
+
+ def run(self):
+ self.assert_has_content()
+ ftable_node = ftable('\n'.join(self.content))
+ ftable_node['label'] = self.options.get('label', None)
+ ftable_node['placement'] = self.options.get('placement', DEFAULT_PLACES)
+ ftable_node['padding'] = self.options.get('padding', DEFAULT_PADDING)
+ ftable_node['nocenter'] = 'nocenter' in self.options
+ if self.arguments and self.arguments[0]:
+ ftable_node['label'] = self.arguments[0]
+ node = nodes.Element() # anonymous container for parsing
+ self.state.nested_parse(self.content, self.content_offset, node)
+ first_node = None
+ second_node = None
+ try:
+ first_node = node[0]
+ second_node = node[1]
+ except IndexError:
+ pass
+ is_first_para = isinstance(first_node, nodes.paragraph)
+ is_second_para = isinstance(second_node, nodes.paragraph)
+ capt = None
+ capd = None
+ if is_first_para and is_second_para:
+ capt = captitle(first_node.rawsource, '', *first_node.children)
+ capd = capdesc(second_node.rawsource, '', *second_node.children)
+ node = node[2:]
+ elif is_first_para:
+ capt = captitle(first_node.rawsource, '', *first_node.children)
+ capd = capdesc(first_node.rawsource, '', *first_node.children)
+ node = node[1:]
+ elif is_second_para:
+ capt = captitle(second_node.rawsource, '', *second_node.children)
+ capd = capdesc(second_node.rawsource, '', *second_node.children)
+ node = node[1:]
+ first = True
+ children = []
+ for n in node:
+ if isinstance(n, subtable):
+ if first:
+ first = False
+ else:
+ children.append(tpad('', '', nodes.Text(''),
+ inches=ftable_node['padding']))
+ children.append(n)
+ ftable_node += children
+ if capt and capd:
+ ftable_node += capt
+ ftable_node += capd
+ return [ftable_node]
+
+
+
+def setup(app):
+ app.add_node(subtcap, latex=(latex_visit_subtcap, latex_depart_subtcap))
+ app.add_node(subtable, latex=(latex_visit_subtable, latex_depart_subtable))
+ app.add_node(captitle, latex=(latex_visit_captitle, latex_depart_captitle))
+ app.add_node(capdesc, latex=(latex_visit_capdesc, latex_depart_capdesc))
+ app.add_node(tpad, latex=(latex_visit_tpad, latex_depart_tpad))
+ app.add_node(ftable, latex=(latex_visit_ftable, latex_depart_ftable))
+ app.add_directive('ftable', FTableDirective)
+ app.add_directive('subtable', SubtableDirective)
+
+
+# vim: set et sw=4 sts=4 :