]> git.llucax.com Git - z.facultad/75.00/informe.git/blob - ext/flt.py
Correcciones de typos y errores de redacción
[z.facultad/75.00/informe.git] / ext / flt.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  = 'btp'
8 DEFAULT_PADDING = '0.1'
9
10
11 # subflt
12 ############################################################################
13
14 class subcap(nodes.Part, nodes.TextElement):
15     pass
16
17 def latex_visit_subcap(self, node):
18     self.body.append('[')
19
20 def latex_depart_subcap(self, node):
21     self.body.append(']{')
22
23
24 class subflt(nodes.General, nodes.Element):
25     pass
26
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')
31
32 def latex_depart_subflt(self, node):
33     self.body.append('}\n')
34
35
36 class SubfltDirective(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         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)
56         first_node = node[0]
57         if isinstance(first_node, nodes.paragraph):
58             cap = subcap(first_node.rawsource, '', *first_node.children)
59             subflt_node += cap
60             subflt_node += node[1:]
61         else:
62             subflt_node += subcap('', '', nodes.Text(''))
63             subflt_node += node[:]
64         return [subflt_node]
65
66
67 # captions / padding
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 pad(nodes.Part, nodes.TextElement):
89     pass
90
91 def latex_visit_pad(self, node):
92     self.body.append('\\hspace{%sin}\n' % node['inches'])
93
94 def latex_depart_pad(self, node):
95     pass
96     #self.body.append('\\hspace{%sin}\n' % DEFAULT_PADDING) #node['inches'])
97
98
99
100 # flt
101 ############################################################################
102
103 class flt(nodes.General, nodes.Element):
104     pass
105
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')
109
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'])
114
115 class FltDirective(Directive):
116
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
121         # option_spec below.
122         return directives.choice(argument, FltDirective.flt_types)
123
124     has_content = True
125     required_arguments = 0
126     optional_arguments = 1
127     final_argument_whitespace = True
128     option_spec = dict(
129         type      = flt_type,
130         label     = directives.unchanged,
131         placement = directives.unchanged,
132         padding   = float,
133         nocenter  = directives.flag,
134     )
135
136     def run(self):
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)
148         first_node = None
149         second_node = None
150         try:
151             first_node = node[0]
152             second_node = node[1]
153         except IndexError:
154             pass
155         is_first_para = isinstance(first_node, nodes.paragraph)
156         is_second_para = isinstance(second_node, nodes.paragraph)
157         capt = None
158         capd = None
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)
162             node = node[2:]
163         elif is_first_para:
164             capt = captitle(first_node.rawsource, '', *first_node.children)
165             capd = capdesc(first_node.rawsource+'.', '',
166                     *(first_node.children + [nodes.Text('.')]))
167             node = node[1:]
168         elif is_second_para:
169             capt = captitle(second_node.rawsource, '', *second_node.children)
170             capd = capdesc(second_node.rawsource+'.', '',
171                     *(second_node.children + [nodes.Text('.')]))
172             node = node[1:]
173         first = True
174         children = []
175         for n in node:
176             if isinstance(n, subflt):
177                 if first:
178                     first = False
179                 else:
180                     children.append(pad('', '', nodes.Text(''),
181                             inches=flt_node['padding']))
182             children.append(n)
183         flt_node += children
184         if capt and capd:
185             flt_node += capt
186             flt_node += capd
187         return [flt_node]
188
189
190
191 def setup(app):
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)
200
201
202 # vim: set et sw=4 sts=4 :