]> git.llucax.com Git - z.facultad/75.00/informe.git/blob - ext/fig.py
Arreglar rol con typo
[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 pad(nodes.Part, nodes.TextElement):
71     pass
72
73 def latex_visit_pad(self, node):
74     self.body.append('\\hspace{%sin}\n' % node['inches'])
75
76 def latex_depart_pad(self, node):
77     pass
78     #self.body.append('\\hspace{%sin}\n' % DEFAULT_PADDING) #node['inches'])
79
80
81 class fig(nodes.General, nodes.Element):
82     pass
83
84 def latex_visit_fig(self, node):
85     self.body.append('\\begin{figure}[%s]\n' % node['placement'])
86     self.body.append('' if node['nocenter'] else '\\centering\n')
87
88 def latex_depart_fig(self, node):
89     label = '\\label{%s}\n' % node['label'] if node['label'] else ''
90     self.body.append(label)
91     self.body.append('\\end{figure}\n\n')
92
93 class FigDirective(Directive):
94
95     has_content = True
96     required_arguments = 0
97     optional_arguments = 1
98     final_argument_whitespace = True
99     option_spec = dict(
100         label     = directives.unchanged,
101         placement = directives.unchanged,
102         padding   = float,
103         nocenter  = directives.flag,
104     )
105
106     def run(self):
107         self.assert_has_content()
108         fig_node = fig('\n'.join(self.content))
109         fig_node['label'] = self.options.get('label', None)
110         fig_node['placement'] = self.options.get('placement', DEFAULT_PLACES)
111         fig_node['padding'] = self.options.get('padding', DEFAULT_PADDING)
112         fig_node['nocenter'] = 'nocenter' in self.options
113         if self.arguments and self.arguments[0]:
114             fig_node['label'] = self.arguments[0]
115         node = nodes.Element() # anonymous container for parsing
116         self.state.nested_parse(self.content, self.content_offset, node)
117         first_node = node[0]
118         cap = None
119         if isinstance(first_node, nodes.paragraph):
120             cap = nodes.caption(first_node.rawsource, '', *first_node.children)
121             node = node[1:]
122         first = True
123         children = []
124         for n in node:
125             if isinstance(n, subfig):
126                 if first:
127                     first = False
128                 else:
129                     children.append(pad('', '', nodes.Text(''),
130                             inches=fig_node['padding']))
131             children.append(n)
132         fig_node += children
133         if cap:
134             fig_node += cap
135         return [fig_node]
136
137
138
139 def setup(app):
140     app.add_node(subcap, latex=(latex_visit_subcap, latex_depart_subcap))
141     app.add_node(subfig, latex=(latex_visit_subfig, latex_depart_subfig))
142     app.add_node(pad, latex=(latex_visit_pad, latex_depart_pad))
143     app.add_node(fig, latex=(latex_visit_fig, latex_depart_fig))
144     app.add_directive('fig', FigDirective)
145     app.add_directive('subfig', SubfigDirective)
146
147
148 # vim: set et sw=4 sts=4 :