]> git.llucax.com Git - z.facultad/75.00/informe.git/blob - ext/ftable.py
Actualizar historia de D y elección del compilador
[z.facultad/75.00/informe.git] / ext / ftable.py
1
2 from docutils import nodes, utils
3 from docutils.parsers.rst import directives
4 from sphinx.util.compat import Directive
5
6
7 DEFAULT_PLACES  = 'hbtp'
8 DEFAULT_PADDING = '0.1'
9
10
11 # subtable
12 ############################################################################
13
14 class subtcap(nodes.Part, nodes.TextElement):
15     pass
16
17 def latex_visit_subtcap(self, node):
18     self.body.append('[')
19
20 def latex_depart_subtcap(self, node):
21     self.body.append(']{')
22
23
24 class subtable(nodes.General, nodes.Element):
25     pass
26
27 def latex_visit_subtable(self, node):
28     space = '\\hspace{%sin}\n' % node['space'] if node['space'] else ''
29     self.body.append(space)
30     self.body.append('\\subfloat')
31
32 def latex_depart_subtable(self, node):
33     self.body.append('}\n')
34
35
36 class SubtableDirective(Directive):
37
38     has_content = True
39     required_arguments = 0
40     optional_arguments = 1
41     final_argument_whitespace = True
42     option_spec = dict(
43         label   = directives.unchanged,
44         space   = float,
45     )
46
47     def run(self):
48         self.assert_has_content()
49         subtable_node = subtable('\n'.join(self.content))
50         subtable_node['label'] = self.options.get('label', None)
51         subtable_node['space'] = self.options.get('space', None)
52         if self.arguments and self.arguments[0]:
53             subtable_node['label'] = self.arguments[0]
54         node = nodes.Element() # anonymous container for parsing
55         self.state.nested_parse(self.content, self.content_offset, node)
56         first_node = node[0]
57         if isinstance(first_node, nodes.paragraph):
58             cap = subtcap(first_node.rawsource, '', *first_node.children)
59             subtable_node += cap
60             subtable_node += node[1:]
61         else:
62             subtable_node += subtcap('', '', nodes.Text(''))
63             subtable_node += node[:]
64         return [subtable_node]
65
66
67 # ftable
68 ############################################################################
69
70 class captitle(nodes.Part, nodes.TextElement):
71     pass
72
73 def latex_visit_captitle(self, node):
74     self.body.append('\\caption[')
75
76 def latex_depart_captitle(self, node):
77     self.body.append(']')
78
79 class capdesc(nodes.Part, nodes.TextElement):
80     pass
81
82 def latex_visit_capdesc(self, node):
83     self.body.append('{')
84
85 def latex_depart_capdesc(self, node):
86     self.body.append('}\n')
87
88 class tpad(nodes.Part, nodes.TextElement):
89     pass
90
91 def latex_visit_tpad(self, node):
92     self.body.append('\\hspace{%sin}\n' % node['inches'])
93
94 def latex_depart_tpad(self, node):
95     pass
96     #self.body.append('\\hspace{%sin}\n' % DEFAULT_PADDING) #node['inches'])
97
98
99 class ftable(nodes.General, nodes.Element):
100     pass
101
102 def latex_visit_ftable(self, node):
103     self.body.append('\\begin{table}[%s]\n' % node['placement'])
104     self.body.append('' if node['nocenter'] else '\\centering\n')
105
106 def latex_depart_ftable(self, node):
107     label = '\\label{%s}\n' % node['label'] if node['label'] else ''
108     self.body.append(label)
109     self.body.append('\\end{table}\n\n')
110
111 class FTableDirective(Directive):
112
113     has_content = True
114     required_arguments = 0
115     optional_arguments = 1
116     final_argument_whitespace = True
117     option_spec = dict(
118         label     = directives.unchanged,
119         placement = directives.unchanged,
120         padding   = float,
121         nocenter  = directives.flag,
122     )
123
124     def run(self):
125         self.assert_has_content()
126         ftable_node = ftable('\n'.join(self.content))
127         ftable_node['label'] = self.options.get('label', None)
128         ftable_node['placement'] = self.options.get('placement', DEFAULT_PLACES)
129         ftable_node['padding'] = self.options.get('padding', DEFAULT_PADDING)
130         ftable_node['nocenter'] = 'nocenter' in self.options
131         if self.arguments and self.arguments[0]:
132             ftable_node['label'] = self.arguments[0]
133         node = nodes.Element() # anonymous container for parsing
134         self.state.nested_parse(self.content, self.content_offset, node)
135         first_node = None
136         second_node = None
137         try:
138             first_node = node[0]
139             second_node = node[1]
140         except IndexError:
141             pass
142         is_first_para = isinstance(first_node, nodes.paragraph)
143         is_second_para = isinstance(second_node, nodes.paragraph)
144         capt = None
145         capd = None
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)
149             node = node[2:]
150         elif is_first_para:
151             capt = captitle(first_node.rawsource, '', *first_node.children)
152             capd = capdesc(first_node.rawsource, '', *first_node.children)
153             node = node[1:]
154         elif is_second_para:
155             capt = captitle(second_node.rawsource, '', *second_node.children)
156             capd = capdesc(second_node.rawsource, '', *second_node.children)
157             node = node[1:]
158         first = True
159         children = []
160         for n in node:
161             if isinstance(n, subtable):
162                 if first:
163                     first = False
164                 else:
165                     children.append(tpad('', '', nodes.Text(''),
166                             inches=ftable_node['padding']))
167             children.append(n)
168         ftable_node += children
169         if capt and capd:
170             ftable_node += capt
171             ftable_node += capd
172         return [ftable_node]
173
174
175
176 def setup(app):
177     app.add_node(subtcap, latex=(latex_visit_subtcap, latex_depart_subtcap))
178     app.add_node(subtable, latex=(latex_visit_subtable, latex_depart_subtable))
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(tpad, latex=(latex_visit_tpad, latex_depart_tpad))
182     app.add_node(ftable, latex=(latex_visit_ftable, latex_depart_ftable))
183     app.add_directive('ftable', FTableDirective)
184     app.add_directive('subtable', SubtableDirective)
185
186
187 # vim: set et sw=4 sts=4 :