~landscape/zope3/ztk-1.1.3

« back to all changes in this revision

Viewing changes to src/zope/structuredtext/docbook.py

  • Committer: Sidnei da Silva
  • Date: 2010-07-05 21:07:01 UTC
  • Revision ID: sidnei.da.silva@canonical.com-20100705210701-zmqhqrbzad1mhzsl
- Reduce deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##############################################################################
2
 
#
3
 
# Copyright (c) 2001 Zope Foundation and Contributors.
4
 
#
5
 
# This software is subject to the provisions of the Zope Public License,
6
 
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
 
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
 
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
 
# FOR A PARTICULAR PURPOSE
11
 
#
12
 
##############################################################################
13
 
""" Render STX document as docbook.
14
 
"""
15
 
 
16
 
__metaclass__ = type
17
 
 
18
 
class DocBook:
19
 
    """ Structured text document renderer for Docbook.
20
 
    """
21
 
    element_types = {
22
 
       '#text': '_text',
23
 
       'StructuredTextDocument': 'document',
24
 
       'StructuredTextParagraph': 'paragraph',
25
 
       'StructuredTextExample': 'example',
26
 
       'StructuredTextBullet': 'bullet',
27
 
       'StructuredTextNumbered': 'numbered',
28
 
       'StructuredTextDescription': 'description',
29
 
       'StructuredTextDescriptionTitle': 'descriptionTitle',
30
 
       'StructuredTextDescriptionBody': 'descriptionBody',
31
 
       'StructuredTextSection': 'section',
32
 
       'StructuredTextSectionTitle': 'sectionTitle',
33
 
       'StructuredTextLiteral': 'literal',
34
 
       'StructuredTextEmphasis': 'emphasis',
35
 
       'StructuredTextStrong': 'strong',
36
 
       'StructuredTextLink': 'link',
37
 
       'StructuredTextXref': 'xref',
38
 
       'StructuredTextSGML': 'sgml',
39
 
       }
40
 
 
41
 
    def dispatch(self, doc, level, output):
42
 
        getattr(self, self.element_types[doc.getNodeName()]
43
 
               )(doc, level, output)
44
 
 
45
 
    def __call__(self, doc, level=1):
46
 
        r=[]
47
 
        self.dispatch(doc, level-1, r.append)
48
 
        return ''.join(r)
49
 
 
50
 
    def _text(self, doc, level, output):
51
 
        if doc.getNodeName() == 'StructuredTextLiteral':
52
 
            output(doc.getNodeValue())
53
 
        else:
54
 
            output(doc.getNodeValue().lstrip())
55
 
 
56
 
    def document(self, doc, level, output):
57
 
        output('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n')
58
 
        output('<book>\n')
59
 
        children=doc.getChildNodes()
60
 
        if (children and
61
 
            children[0].getNodeName() == 'StructuredTextSection'):
62
 
            output('<title>%s</title>'
63
 
                % children[0].getChildNodes()[0].getNodeValue())
64
 
        for c in children:
65
 
            getattr(self, self.element_types[c.getNodeName()]
66
 
                   )(c, level, output)
67
 
        output('</book>\n')
68
 
 
69
 
    def section(self, doc, level, output):
70
 
        output('\n<section>\n')
71
 
        children=doc.getChildNodes()
72
 
        for c in children:
73
 
            getattr(self, self.element_types[c.getNodeName()]
74
 
                   )(c, level+1, output)
75
 
        output('\n</section>\n')
76
 
 
77
 
    def sectionTitle(self, doc, level, output):
78
 
        output('<title>')
79
 
        for c in doc.getChildNodes():
80
 
            try:
81
 
                getattr(self, self.element_types[c.getNodeName()]
82
 
                       )(c, level, output)
83
 
            except:
84
 
                print "failed", c.getNodeName(), c
85
 
        output('</title>\n')
86
 
 
87
 
    def description(self, doc, level, output):
88
 
        p=doc.getPreviousSibling()
89
 
        if p is None or  p.getNodeName() is not doc.getNodeName():
90
 
            output('<variablelist>\n')
91
 
        for c in doc.getChildNodes():
92
 
            getattr(self, self.element_types[c.getNodeName()]
93
 
                   )(c, level, output)
94
 
        n=doc.getNextSibling()
95
 
        if n is None or n.getNodeName() is not doc.getNodeName():
96
 
            output('</variablelist>\n')
97
 
 
98
 
    def descriptionTitle(self, doc, level, output):
99
 
        output('<varlistentry><term>\n')
100
 
        for c in doc.getChildNodes():
101
 
            getattr(self, self.element_types[c.getNodeName()]
102
 
                   )(c, level, output)
103
 
        output('</term>\n')
104
 
 
105
 
    def descriptionBody(self, doc, level, output):
106
 
        output('<listitem><para>\n')
107
 
        for c in doc.getChildNodes():
108
 
            getattr(self, self.element_types[c.getNodeName()]
109
 
                   )(c, level, output)
110
 
        output('</para></listitem>\n')
111
 
        output('</varlistentry>\n')
112
 
 
113
 
    def bullet(self, doc, level, output):
114
 
        p=doc.getPreviousSibling()
115
 
        if p is None or p.getNodeName() is not doc.getNodeName():
116
 
            output('<itemizedlist>\n')
117
 
        output('<listitem><para>\n')
118
 
 
119
 
        for c in doc.getChildNodes():
120
 
            getattr(self, self.element_types[c.getNodeName()]
121
 
                   )(c, level, output)
122
 
        n=doc.getNextSibling()
123
 
        output('</para></listitem>\n')
124
 
        if n is None or n.getNodeName() is not doc.getNodeName():
125
 
            output('</itemizedlist>\n')
126
 
 
127
 
    def numbered(self, doc, level, output):
128
 
        p=doc.getPreviousSibling()
129
 
        if p is None or p.getNodeName() is not doc.getNodeName():
130
 
            output('<orderedlist>\n')
131
 
        output('<listitem><para>\n')
132
 
        for c in doc.getChildNodes():
133
 
            getattr(self, self.element_types[c.getNodeName()]
134
 
                   )(c, level, output)
135
 
        n=doc.getNextSibling()
136
 
        output('</para></listitem>\n')
137
 
        if n is None or n.getNodeName() is not doc.getNodeName():
138
 
            output('</orderedlist>\n')
139
 
 
140
 
    def example(self, doc, level, output):
141
 
        i=0
142
 
        for c in doc.getChildNodes():
143
 
            if i==0:
144
 
                output('<programlisting>\n<![CDATA[\n')
145
 
                ##
146
 
                ## eek.  A ']]>' in your body will break this...
147
 
                ##
148
 
                output(prestrip(c.getNodeValue()))
149
 
                output('\n]]></programlisting>\n')
150
 
            else:
151
 
                getattr(self, self.element_types[c.getNodeName()])(
152
 
                   c, level, output)
153
 
 
154
 
    def paragraph(self, doc, level, output):
155
 
        output('<para>\n\n')
156
 
        for c in doc.getChildNodes():
157
 
            getattr(self, self.element_types[c.getNodeName()])(
158
 
               c, level, output)
159
 
        output('</para>\n\n')
160
 
 
161
 
    def link(self, doc, level, output):
162
 
        output('<ulink url="%s">' % doc.href)
163
 
        for c in doc.getChildNodes():
164
 
            getattr(self, self.element_types[c.getNodeName()]
165
 
                   )(c, level, output)
166
 
        output('</ulink>')
167
 
 
168
 
    def emphasis(self, doc, level, output):
169
 
        output('<emphasis>')
170
 
        for c in doc.getChildNodes():
171
 
            getattr(self, self.element_types[c.getNodeName()]
172
 
                   )(c, level, output)
173
 
        output('</emphasis> ')
174
 
 
175
 
    def literal(self, doc, level, output):
176
 
        output('<literal>')
177
 
        for c in doc.getChildNodes():
178
 
            output(c.getNodeValue())
179
 
        output('</literal>')
180
 
 
181
 
    def strong(self, doc, level, output):
182
 
        output('<emphasis>')
183
 
        for c in doc.getChildNodes():
184
 
            getattr(self, self.element_types[c.getNodeName()]
185
 
                   )(c, level, output)
186
 
        output('</emphasis>')
187
 
 
188
 
    def xref(self, doc, level, output):
189
 
        output('<xref linkend="%s"/>' % doc.getNodeValue())
190
 
 
191
 
    def sgml(self, doc, level, output):
192
 
        output(doc.getNodeValue())
193
 
 
194
 
 
195
 
def prestrip(v):
196
 
    v=v.replace( '\r\n', '\n')
197
 
    v=v.replace( '\r', '\n')
198
 
    v=v.replace( '\t', '        ')
199
 
    lines=v.split('\n')
200
 
    indent=len(lines[0])
201
 
    for line in lines:
202
 
        if not len(line): continue
203
 
        i=len(line)-len(line.lstrip())
204
 
        if i < indent:
205
 
            indent=i
206
 
    nlines=[]
207
 
    for line in lines:
208
 
        nlines.append(line[indent:])
209
 
    return '\n'.join(nlines)
210
 
 
211
 
class DocBookChapter(DocBook):
212
 
 
213
 
    def document(self, doc, level, output):
214
 
        output('<chapter>\n')
215
 
        children=doc.getChildNodes()
216
 
        if (children and
217
 
            children[0].getNodeName() == 'StructuredTextSection'):
218
 
            output('<title>%s</title>'
219
 
                    % children[0].getChildNodes()[0].getNodeValue())
220
 
        for c in children[0].getChildNodes()[1:]:
221
 
            getattr(self, self.element_types[c.getNodeName()]
222
 
                   )(c, level, output)
223
 
        output('</chapter>\n')
224
 
 
225
 
class DocBookChapterWithFigures(DocBookChapter):
226
 
 
227
 
    element_types = DocBook.element_types
228
 
    element_types.update({'StructuredTextImage': 'image'})
229
 
 
230
 
    def image(self, doc, level, output):
231
 
        if hasattr(doc, 'key'):
232
 
            output('<figure id="%s"><title>%s</title>\n'
233
 
                        % (doc.key, doc.getNodeValue()) )
234
 
        else:
235
 
            output('<figure><title>%s</title>\n' % doc.getNodeValue())
236
 
        output('<graphic fileref="%s"></graphic>\n</figure>\n' % doc.href)
237
 
 
238
 
class DocBookArticle(DocBook):
239
 
 
240
 
    def document(self, doc, level, output):
241
 
        output('<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n')
242
 
        output('<article>\n')
243
 
        children=doc.getChildNodes()
244
 
        if (children and
245
 
            children[0].getNodeName() == 'StructuredTextSection'):
246
 
            output('<articleinfo>\n<title>%s</title>\n</articleinfo>\n' %
247
 
                   children[0].getChildNodes()[0].getNodeValue())
248
 
        for c in children:
249
 
            getattr(self, self.element_types[c.getNodeName()]
250
 
                   )(c, level, output)
251
 
        output('</article>\n')
252
 
 
253
 
 
254
 
class DocBookBook:
255
 
 
256
 
    def __init__(self, title=''):
257
 
        self.title = title
258
 
        self.chapters = []
259
 
 
260
 
    def addChapter(self, chapter):
261
 
        self.chapters.append(chapter)
262
 
 
263
 
    def read(self):
264
 
        out = ('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n'
265
 
               '<book>\n')
266
 
        out = out + '<title>%s</title>\n' % self.title
267
 
        for chapter in self.chapters:
268
 
            out = out + chapter + '\n</book>\n'
269
 
 
270
 
        return out
271
 
 
272
 
    def __str__(self):
273
 
        return self.read()