]> git.llucax.com Git - z.facultad/75.00/informe.git/blob - ext/fig.py
Agregar glosario de términos
[z.facultad/75.00/informe.git] / ext / fig.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 # subfig
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 subfig(nodes.General, nodes.Element):
25     pass
26
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('\\subfigure')
31
32 def latex_depart_subfig(self, node):
33     self.body.append('}\n')
34
35
36 class SubfigDirective(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         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)
56         first_node = node[0]
57         if isinstance(first_node, nodes.paragraph):
58             cap = subcap(first_node.rawsource, '', *first_node.children)
59             subfig_node += cap
60             subfig_node += node[1:]
61         else:
62             subfig_node += subcap('', '', nodes.Text(''))
63             subfig_node += node[:]
64         return [subfig_node]
65
66
67 # fig
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 class fig(nodes.General, nodes.Element):
100     pass
101
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')
105
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')
110
111 class FigDirective(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         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)
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, subfig):
162                 if first:
163                     first = False
164                 else:
165                     children.append(pad('', '', nodes.Text(''),
166                             inches=fig_node['padding']))
167             children.append(n)
168         fig_node += children
169         if capt and capd:
170             fig_node += capt
171             fig_node += capd
172         return [fig_node]
173
174
175
176 def setup(app):
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)
185
186
187 # vim: set et sw=4 sts=4 :