/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5188.1.2 by Vincent Ladeuil
Add a skeleton texinfo builder for sphinx.
1
# Copyright (C) 2010 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""A sphinx/docutil writer producing texinfo output."""
18
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
19
from docutils import (
20
    nodes,
21
    writers,
22
    )
23
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
24
DEBUG = 0
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
25
26
class TexinfoWriter(writers.Writer):
27
28
    supported = ('texinfo',)
29
    settings_spec = ('No options here.', '', ())
30
    settings_defaults = {}
31
32
    output = None
33
34
    def __init__(self, builder):
35
        writers.Writer.__init__(self)
36
        self.builder = builder
37
38
    def translate(self):
39
        visitor = TexinfoTranslator(self.document, self.builder)
40
        self.document.walkabout(visitor)
41
        self.output = visitor.body
42
43
44
class TexinfoTranslator(nodes.NodeVisitor):
45
5193.6.13 by Vincent Ladeuil
Implement the section generation.
46
    section_names = ['chapter', 'section', 'subsection', 'subsubsection']
47
    """texinfo section names differ from the sphinx ones.
48
49
    Since this can be confusing, the correspondences are shown below
50
    (shpinx -> texinfo):
51
    part       -> chapter
52
    chapter    -> section
53
    section    -> subsection
54
    subsection -> subsubsection
55
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
56
    Additionally, sphinx defines subsubsections and paragraphs which are
57
    handled as @heading (unnumbered).
5193.6.13 by Vincent Ladeuil
Implement the section generation.
58
    """
59
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
60
    def __init__(self, document, builder):
61
        nodes.NodeVisitor.__init__(self, document)
5193.6.29 by Vincent Ladeuil
Start implementing references, most of them are @uref ones.
62
        self.builder = builder
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
63
        # toctree uses some nodes for different purposes (namely:
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
64
        # compact_paragraph, bullet_list) that needs to know when they are
5193.6.29 by Vincent Ladeuil
Start implementing references, most of them are @uref ones.
65
        # processing a toctree.
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
66
        self.in_toctree = False
5193.6.13 by Vincent Ladeuil
Implement the section generation.
67
        # sections can be embedded and produce different directives depending
68
        # on the depth.
69
        self.section_level = -1
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
70
        # By default paragraghs are separated by newlines, but there are some
71
        # exceptions that set it to '' for some subtrees instead
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
72
        self.paragraph_sep = '\n'
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
73
74
    # The whole document
75
76
    def visit_document(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
77
        if DEBUG:
78
            import sys
79
            sys.stdout.write(node.pformat().encode('utf8'))
80
        set_item_list_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
81
82
    def depart_document(self, node):
5193.6.32 by Vincent Ladeuil
Stop gap fix to make makeinfo a bit more happy.
83
        # FIXME: info requires a Top node for each info file, but unless we
84
        # chose a global layout to divide the overall documentation into a set
85
        # of info files, there is no criteria to decide for a title.
5193.6.34 by Vincent Ladeuil
Make sure we don't get bug reports about the known broken info links.
86
        top_cmd = '''\
87
This file has been converted using a beta rst->texinfo converter. 
88
Most of the info links are currently bogus, don't report bugs about them,
89
this is currently worked on.
90
@node Top
91
@top Placeholder
92
'''
5193.6.32 by Vincent Ladeuil
Stop gap fix to make makeinfo a bit more happy.
93
        self.body = top_cmd + ''.join(node['text'])
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
94
95
    # Layout
96
97
    def visit_section(self, node):
5193.6.13 by Vincent Ladeuil
Implement the section generation.
98
        self.section_level += 1
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
99
        set_item_list_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
100
101
    def depart_section(self, node):
5193.6.31 by Vincent Ladeuil
Create a @node before each sectionning command.
102
        title = node['title']
103
        ids = node.get('ids', [])
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
104
        try:
105
            section_name = self.section_names[self.section_level]
106
        except IndexError:
107
            # Just use @heading, it's not numbered anyway
108
            section_name = 'heading'
5193.6.31 by Vincent Ladeuil
Create a @node before each sectionning command.
109
        if ids:
110
            # There shouldn't be different ids for a section, so until we
111
            # encounter bugs, just take the first one.
112
            node_cmd = '@node %s\n' % (ids[0],)
113
        else:
114
            node_cmd = ''
115
        section_cmd = '@%s %s\n' % (section_name, title)
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
116
        text = ''.join(node['text'])
5193.6.31 by Vincent Ladeuil
Create a @node before each sectionning command.
117
        node.parent.collect_text(node_cmd + section_cmd + text)
5193.6.13 by Vincent Ladeuil
Implement the section generation.
118
        self.section_level -= 1
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
119
120
    def visit_topic(self, node):
121
        pass
122
123
    def depart_topic(self, node):
124
        pass
125
5713.2.2 by Vincent Ladeuil
Further tests with sphinx >= 1.0 reveals that a new kind of node have been introduced for toctrees :-/
126
    def visit_compound(self, node):
127
        # compound is new in sphinx >= 1.0 and just add a optional layer so we
128
        # relay the text to the parent when it occurs. This may requires a
129
        # cleaner approach once we settle on which sphinx versions we want to
130
        # support.
131
        set_item_list_collector(node, 'text')
132
133
    def depart_compound(self, node):
134
        text = ''.join(node['text'])
135
        node.parent.collect_text(text)
136
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
137
    def visit_paragraph(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
138
        set_item_list_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
139
140
    def depart_paragraph(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
141
        # End the paragraph with a new line (or '' depending on the parent) and
142
        # leave a blank line after it.
143
        text = ''.join(node['text']) + self.paragraph_sep * 2
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
144
        node.parent.collect_text(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
145
146
    def visit_compact_paragraph(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
147
        set_item_list_collector(node, 'text')
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
148
        if node.has_key('toctree'):
149
            self.in_toctree = True
150
        elif self.in_toctree:
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
151
            set_item_collector(node, 'reference')
5193.6.7 by Vincent Ladeuil
Implement basic text generation.
152
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
153
    def depart_compact_paragraph(self, node):
5193.6.29 by Vincent Ladeuil
Start implementing references, most of them are @uref ones.
154
        # FIXME: Using a different visitor specific to toctree may be a better
155
        # design and makes code clearer. -- vila 20100708
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
156
        if node.has_key('toctree'):
5713.2.1 by Vincent Ladeuil
The problem wasn't due to sphinx version but to a spurious change in release-notes/bzr-2.2.txt, we still have to add support for topics though.
157
            if node['text']:
158
                node.parent.collect_text('@menu\n')
159
                node.parent.collect_text(''.join(node['text']))
160
                node.parent.collect_text('@end menu\n')
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
161
            self.in_toctree = False
162
        elif self.in_toctree:
163
            # * FIRST-ENTRY-NAME:(FILENAME)NODENAME.     DESCRIPTION
164
            # XXX: the file name should probably be adjusted to the targeted
165
            # info file name
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
166
            node_name, file_name, entry_name = node['reference']
167
            if not node_name:
168
                node_name = entry_name
169
            description = '' # We can't specify a description in rest AFAICS
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
170
            # XXX: What if :maxdepth: is not 1 ?
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
171
            text = '* %s: (%s)%s. %s\n' % (entry_name, file_name,
172
                                           node_name, description)
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
173
            node.parent.collect_text(text)
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
174
        else:
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
175
            # End the paragraph with a new line (or '' depending on the parent)
176
            # and leave a blank line after it.
177
            text = ''.join(node['text']) + self.paragraph_sep * 2
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
178
            node.parent.collect_text(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
179
5193.6.14 by Vincent Ladeuil
Implement literal generation.
180
    def visit_literal_block(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
181
        set_item_collector(node, 'text')
5193.6.14 by Vincent Ladeuil
Implement literal generation.
182
183
    def depart_literal_block(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
184
        text = '@samp{%s}' % ''.join(node['text']) + self.paragraph_sep * 2
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
185
        node.parent.collect_text(text)
5193.6.14 by Vincent Ladeuil
Implement literal generation.
186
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
187
    def visit_block_quote(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
188
        set_item_list_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
189
190
    def depart_block_quote(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
191
        node.parent.collect_text('@example\n')
192
        node.parent.collect_text(''.join(node['text']))
193
        node.parent.collect_text('@end example\n\n')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
194
195
    def depart_warning(self, node):
196
        pass
197
198
    def visit_warning(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
199
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
200
5193.6.30 by Vincent Ladeuil
Implement intra document references.
201
    def visit_note(self, node):
202
        raise nodes.SkipNode # Not implemented yet
203
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
204
    def depart_note(self, node):
205
        pass
206
207
    def visit_footnote(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
208
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
209
210
    def depart_footnote(self, node):
211
        pass
212
213
    def visit_comment(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
214
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
215
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
216
    # Attributes
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
217
218
    def visit_title(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
219
        set_item_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
220
221
    def depart_title(self, node):
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
222
        node.parent['title'] = node['text']
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
223
224
    def visit_label(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
225
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
226
227
    def visit_substitution_definition(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
228
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
229
230
    # Plain text
231
232
    def visit_Text(self, node):
233
        pass
234
235
    def depart_Text(self, node):
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
236
        text = node.astext()
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
237
        if '@' in text:
238
            text = text.replace('@', '@@')
239
        if '{' in text:
240
            text = text.replace('{', '@{')
241
        if '}' in text:
242
            text = text.replace('}', '@}')
243
        node.parent.collect_text(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
244
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
245
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
246
    # Styled text
247
248
    def visit_emphasis(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
249
        set_item_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
250
251
    def depart_emphasis(self, node):
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
252
        text = '@emph{%s}' % node['text']
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
253
        node.parent.collect_text(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
254
255
    def visit_strong(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
256
        set_item_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
257
258
    def depart_strong(self, node):
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
259
        text = '@strong{%s}' % node['text']
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
260
        node.parent.collect_text(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
261
262
    def visit_literal(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
263
        set_item_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
264
265
    def depart_literal(self, node):
5193.6.25 by Vincent Ladeuil
Delete dead code and ensure compatibility with lucid.
266
        text = '@code{%s}' % node['text']
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
267
        node.parent.collect_text(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
268
269
    # Lists
270
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
271
    def _decorate_list(self, item_list, collect, item_fmt='%s',
272
                       head=None, foot=None):
273
        if head is not None:
274
            collect(head)
275
        for item in item_list:
276
            collect(item_fmt % item)
277
        if foot is not None:
278
            collect(foot)
279
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
280
    def visit_bullet_list(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
281
        set_item_list_collector(node, 'list_item')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
282
283
    def depart_bullet_list(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
284
        l = node['list_item']
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
285
        if self.in_toctree:
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
286
            self._decorate_list(node['list_item'], node.parent.collect_text)
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
287
        else:
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
288
            self._decorate_list(node['list_item'], node.parent.collect_text,
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
289
                                '@item\n%s',
5193.6.20 by Vincent Ladeuil
Refactor tests.
290
                                # FIXME: Should respect the 'bullet' attribute
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
291
                                '@itemize @bullet\n', '@end itemize\n')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
292
293
    def visit_enumerated_list(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
294
        set_item_list_collector(node, 'list_item')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
295
296
    def depart_enumerated_list(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
297
        self._decorate_list(node['list_item'], node.parent.collect_text,
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
298
                            '@item\n%s',
299
                            '@enumerate\n', '@end enumerate\n')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
300
301
    def visit_definition_list(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
302
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
303
304
    def depart_definition_list(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
305
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
306
307
    def visit_definition_list_item(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
308
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
309
310
    def depart_definition_list_item(self, node):
311
        pass
312
313
    def visit_term(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
314
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
315
316
    def depart_term(self, node):
317
        pass
318
319
    def visit_definition(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
320
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
321
322
    def depart_definition(self, node):
323
        pass
324
325
    def visit_field_list(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
326
        raise nodes.SkipNode # Not implemented yet
327
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
328
    def depart_field_list(self, node):
329
        pass
330
331
    def visit_field(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
332
        raise nodes.SkipNode # Not implemented yet
333
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
334
    def depart_field(self, node):
335
        pass
336
337
    def visit_field_name(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
338
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
339
340
    def depart_field_name(self, node):
341
        pass
342
343
    def visit_field_body(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
344
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
345
346
    def depart_field_body(self, node):
347
        pass
348
349
    def visit_list_item(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
350
        set_item_list_collector(node, 'text')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
351
352
    def depart_list_item(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
353
        text = ''.join(node['text'])
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
354
        node.parent.collect_list_item(text)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
355
356
    def visit_option_list(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
357
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
358
359
    def depart_option_list(self, node):
360
        pass
361
362
    def visit_option_list_item(self, node):
363
        pass
364
365
    def depart_option_list_item(self, node):
366
        pass
367
368
    def visit_option_group(self, node):
369
        pass
370
371
    def depart_option_group(self, node):
372
        pass
373
374
    def visit_option(self, node):
375
        pass
376
377
    def depart_option(self, node):
378
        pass
379
380
    def visit_option_string(self, node):
381
        pass
382
    def depart_option_string(self, node):
383
        pass
384
385
    def visit_option_argument(self, node):
386
        pass
387
388
    def depart_option_argument(self, node):
389
        pass
390
391
    def visit_description(self, node):
392
        pass
393
    def depart_description(self, node):
394
        pass
395
396
    # Tables
397
    def visit_table(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
398
        set_item_collector(node, 'table')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
399
400
    def depart_table(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
401
        node.parent.collect_text(node['table'])
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
402
403
    def visit_tgroup(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
404
        set_item_list_collector(node, 'colspec')
405
        set_item_collector(node, 'head_entries')
406
        set_item_collector(node, 'body_rows')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
407
408
    def depart_tgroup(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
409
        header = []
410
        # The '@multitable {xxx}{xxx}' line
411
        self._decorate_list(node['colspec'], header.append,
412
                            '{%s}', '@multitable ', '\n')
413
        # The '@headitem xxx @tab yyy...' line
414
        head_entries = node['head_entries']
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
415
        if head_entries is not None:
416
            # Not all tables define titles for the columns... rest parser bug ?
417
            # FIXME: need a test
418
            self._decorate_list(head_entries[1:], header.append,
419
                                ' @tab %s',
420
                                '@headitem %s' % head_entries[0], '\n')
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
421
        header = ''.join(header)
422
        # The '@item xxx\n @tab yyy\n ...' lines
423
        body_rows = node['body_rows']
424
        rows = []
425
        for r in body_rows:
426
            self._decorate_list(r[1:], rows.append,
427
                                '@tab %s\n', '@item %s\n' % r[0])
5193.6.20 by Vincent Ladeuil
Refactor tests.
428
        footer = '@end multitable\n'
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
429
        node.parent.collect_table(header + ''.join(rows) + footer)
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
430
431
    def visit_colspec(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
432
        pass
5193.6.16 by Vincent Ladeuil
Implement the table generation.
433
434
    def depart_colspec(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
435
        node.parent.collect_colspec('x' * node['colwidth'])
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
436
437
    def visit_thead(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
438
        set_item_collector(node, 'row')
5193.6.16 by Vincent Ladeuil
Implement the table generation.
439
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
440
    def depart_thead(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
441
        node.parent.collect_head_entries(node['row'])
5193.6.16 by Vincent Ladeuil
Implement the table generation.
442
443
    def visit_tbody(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
444
        set_item_list_collector(node, 'row')
5193.6.16 by Vincent Ladeuil
Implement the table generation.
445
446
    def depart_tbody(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
447
        node.parent.collect_body_rows(node['row'])
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
448
449
    def visit_row(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
450
        set_item_list_collector(node, 'entry')
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
451
452
    def depart_row(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
453
        node.parent.collect_row(node['entry'])
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
454
455
    def visit_entry(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
456
        set_item_list_collector(node, 'text')
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
457
        node['par_sep_orig'] = self.paragraph_sep
458
        self.paragraph_sep = ''
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
459
460
    def depart_entry(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
461
        node.parent.collect_entry(''.join(node['text']))
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
462
        self.paragraph_sep = node['par_sep_orig']
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
463
464
    # References
465
466
    def visit_reference(self, node):
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
467
        for c in node.children:
468
            if getattr(c, 'parent', None) is None:
469
                # Bug sphinx
470
                node.setup_child(c)
471
        set_item_collector(node, 'text')
5193.6.12 by Vincent Ladeuil
Implement a basic toctree generation.
472
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
473
    def depart_reference(self, node):
5193.6.29 by Vincent Ladeuil
Start implementing references, most of them are @uref ones.
474
        anchorname = node.get('anchorname', None)
475
        refuri = node.get('refuri', None)
476
        refid = node.get('refid', None)
477
        text = ''.join(node['text'])
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
478
        collect = getattr(node.parent, 'collect_reference', None)
479
        if collect is not None:
5193.6.29 by Vincent Ladeuil
Start implementing references, most of them are @uref ones.
480
            if not self.in_toctree:
481
                raise AssertionError('collect_reference is specific to toctree')
482
            if anchorname is None:
483
                anchorname = ''
484
            if refuri is None:
485
                refuri = ''
486
            collect((anchorname, refuri, text))
487
        elif refuri is not None:
5193.6.30 by Vincent Ladeuil
Implement intra document references.
488
            node.parent.collect_text('@uref{%s,%s}' % (refuri, text))
489
        elif refid is not None:
490
            # Info format requires that a reference is followed by some
491
            # punctuation char ('.', ','. ')', etc). Rest is more liberal. To
492
            # accommodate, we use pxref inside parenthesis.
493
            node.parent.collect_text('%s (@pxref{%s})' % (text, refid))
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
494
495
    def visit_footnote_reference(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
496
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
497
498
    def visit_citation_reference(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
499
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
500
501
    def visit_title_reference(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
502
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
503
504
    def depart_title_reference(self, node):
505
        pass
506
507
    def visit_target(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
508
        raise nodes.SkipNode # Not implemented yet
5193.6.5 by Vincent Ladeuil
Implement a bare-bone texinfo translator for sphinx.
509
510
    def depart_target(self, node):
511
        pass
512
513
    def visit_image(self, node):
5193.6.21 by Vincent Ladeuil
Skip all not implemented nodes.
514
        raise nodes.SkipNode # Not implemented yet
5193.6.16 by Vincent Ladeuil
Implement the table generation.
515
5193.6.19 by Vincent Ladeuil
Use a better design to better control the texinfo generation.
516
# Helpers to collect data in parent node
517
518
def set_item_collector(node, name):
519
    node[name] = None
520
    def set_item(item):
521
        node[name] = item
522
    setattr(node, 'collect_' + name, set_item)
523
524
525
def set_item_list_collector(node, name, sep=''):
526
    node[name] = []
527
    node[name + '_sep'] = sep
528
    def append_item(item):
529
        node[name].append(item)
530
    setattr(node, 'collect_' + name, append_item)
531
532