1
##############################################################################
3
# Copyright (c) 2001 Zope Foundation and Contributors.
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
12
##############################################################################
13
""" Render STX document as docbook.
19
""" Structured text document renderer for Docbook.
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',
41
def dispatch(self, doc, level, output):
42
getattr(self, self.element_types[doc.getNodeName()]
45
def __call__(self, doc, level=1):
47
self.dispatch(doc, level-1, r.append)
50
def _text(self, doc, level, output):
51
if doc.getNodeName() == 'StructuredTextLiteral':
52
output(doc.getNodeValue())
54
output(doc.getNodeValue().lstrip())
56
def document(self, doc, level, output):
57
output('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n')
59
children=doc.getChildNodes()
61
children[0].getNodeName() == 'StructuredTextSection'):
62
output('<title>%s</title>'
63
% children[0].getChildNodes()[0].getNodeValue())
65
getattr(self, self.element_types[c.getNodeName()]
69
def section(self, doc, level, output):
70
output('\n<section>\n')
71
children=doc.getChildNodes()
73
getattr(self, self.element_types[c.getNodeName()]
75
output('\n</section>\n')
77
def sectionTitle(self, doc, level, output):
79
for c in doc.getChildNodes():
81
getattr(self, self.element_types[c.getNodeName()]
84
print "failed", c.getNodeName(), c
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()]
94
n=doc.getNextSibling()
95
if n is None or n.getNodeName() is not doc.getNodeName():
96
output('</variablelist>\n')
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()]
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()]
110
output('</para></listitem>\n')
111
output('</varlistentry>\n')
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')
119
for c in doc.getChildNodes():
120
getattr(self, self.element_types[c.getNodeName()]
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')
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()]
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')
140
def example(self, doc, level, output):
142
for c in doc.getChildNodes():
144
output('<programlisting>\n<![CDATA[\n')
146
## eek. A ']]>' in your body will break this...
148
output(prestrip(c.getNodeValue()))
149
output('\n]]></programlisting>\n')
151
getattr(self, self.element_types[c.getNodeName()])(
154
def paragraph(self, doc, level, output):
156
for c in doc.getChildNodes():
157
getattr(self, self.element_types[c.getNodeName()])(
159
output('</para>\n\n')
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()]
168
def emphasis(self, doc, level, output):
170
for c in doc.getChildNodes():
171
getattr(self, self.element_types[c.getNodeName()]
173
output('</emphasis> ')
175
def literal(self, doc, level, output):
177
for c in doc.getChildNodes():
178
output(c.getNodeValue())
181
def strong(self, doc, level, output):
183
for c in doc.getChildNodes():
184
getattr(self, self.element_types[c.getNodeName()]
186
output('</emphasis>')
188
def xref(self, doc, level, output):
189
output('<xref linkend="%s"/>' % doc.getNodeValue())
191
def sgml(self, doc, level, output):
192
output(doc.getNodeValue())
196
v=v.replace( '\r\n', '\n')
197
v=v.replace( '\r', '\n')
198
v=v.replace( '\t', ' ')
202
if not len(line): continue
203
i=len(line)-len(line.lstrip())
208
nlines.append(line[indent:])
209
return '\n'.join(nlines)
211
class DocBookChapter(DocBook):
213
def document(self, doc, level, output):
214
output('<chapter>\n')
215
children=doc.getChildNodes()
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()]
223
output('</chapter>\n')
225
class DocBookChapterWithFigures(DocBookChapter):
227
element_types = DocBook.element_types
228
element_types.update({'StructuredTextImage': 'image'})
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()) )
235
output('<figure><title>%s</title>\n' % doc.getNodeValue())
236
output('<graphic fileref="%s"></graphic>\n</figure>\n' % doc.href)
238
class DocBookArticle(DocBook):
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()
245
children[0].getNodeName() == 'StructuredTextSection'):
246
output('<articleinfo>\n<title>%s</title>\n</articleinfo>\n' %
247
children[0].getChildNodes()[0].getNodeValue())
249
getattr(self, self.element_types[c.getNodeName()]
251
output('</article>\n')
256
def __init__(self, title=''):
260
def addChapter(self, chapter):
261
self.chapters.append(chapter)
264
out = ('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n'
266
out = out + '<title>%s</title>\n' % self.title
267
for chapter in self.chapters:
268
out = out + chapter + '\n</book>\n'