Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 398 lines (309 sloc) 11.095 kB
61b460d @etrepum initial import
authored
1 #!/usr/bin/env python
2 import cStringIO, cgi, sys, urllib, os
3 import docutils.core, docutils.io
4 from docutils import writers, nodes, languages
5 from reportlab.tools.pythonpoint import pythonpoint
6
7 def _hackhackhack():
8 styles = pythonpoint.getStyles()
9 codeStyle = styles['Code']
10 codeStyle.leftIndent = 72
11 codeStyle.leading = 36
12 codeStyle.spaceBefore = 24
13 styles['Code'] = codeStyle
14 pythonpoint.setStyles(styles)
15 #_hackhackhack()
16
17 class Writer(writers.Writer):
18
19 settings_spec = ()
20 settings_default_overrides = {}
21 output = None
22
23 def translate(self):
24 visitor = DumbPythonPointFormatter(self.document)
25 self.document.walkabout(visitor)
26 self.output = visitor.astext()
27 #self.head_prefix = visitor.head_prefix
28 #self.head = visitor.head
29 #self.body_prefix = visitor.body_prefix
30 #self.body = visitor.body
31 #self.body_suffix = visitor.body_suffix
32
33 class DumbPythonPointFormatter(nodes.NodeVisitor):
34
35 def __init__(self, document):
36 nodes.NodeVisitor.__init__(self, document)
37 self.section = 0
38 self.closers = []
39 self.slidenum = 0
40 self.body = []
41 self.w = self.body.append
42 self.suppress_para = 0
43
44 def astext(self):
45 return ''.join(self.body)
46
47 def visit_reference(self, node):
48 pass
49
50 def depart_reference(self, node):
51 pass
52
53 def visit_document(self, node):
54 self.w('<presentation>\n'
55 '<stylesheet module="modern" function="getParagraphStyles"/>\n'
56 '<section name="Main">\n'
57 '<fixedimage filename="../sidebar.jpg"'
58 ' height="600" width="150" x="0" y="0" />\n'
59 )
60
61 def depart_document(self, node):
62 self.w('</section>\n'
63 '</presentation>\n')
64
65 def visit_section(self, node):
66 #if node.attributes.has_key('dupname'):
67 # name = node.attributes['dupname']
68 #else:
69 # name = node.attributes['name']
70
71 # HACK HACK HACK
72 name = node.astext().split('\n', 1)[0]
73 self.slidenum += 1
74 self.w('<slide id="Slide%03d" title="%s">\n'
75 '<frame x="110" y="30" width="690" height="520" leftmargin="12"'
76 ' rightmargin="0">\n'%(self.slidenum, name))
77
78 def depart_section(self, node):
79 self.w('</frame>\n</slide>\n')
80
81 def visit_title(self, node):
82 self.w('<para style="Heading1">')
83 self.suppress_para = 1
84
85 def depart_title(self, node):
86 self.suppress_para = 0
87 self.w('</para>\n')
88
89 def visit_paragraph(self, node):
90 if not self.suppress_para: self.w('<para style="BodyText">')
91
92 def depart_paragraph(self, node):
93 if not self.suppress_para: self.w('</para>\n')
94
95 # Simple lists
96 def visit_bullet_list(self, node):
97 pass
98
99 def depart_bullet_list(self, node):
100 pass
101
102 def visit_enumerated_list(self, node):
103 pass
104
105 def depart_enumerated_list(self, node):
106 pass
107
108 def visit_list_item(self, node):
109 self.w('<para style="Bullet">')
110 self.suppress_para = 1
111
112 def depart_list_item(self, node):
113 self.suppress_para = 0
114 self.w('</para>\n')
115
116 # Definition List
117 def visit_definition_list(self, node):
118 pass
119
120 def depart_definition_list(self, node):
121 pass
122
123 def visit_definition_list_item(self, node):
124 pass
125
126 def depart_definition_list_item(self, node):
127 pass
128
129 def visit_term(self, node):
130 self.w('<para><b>')
131
132 def depart_term(self, node):
133 self.w('</b>')
134
135 def visit_classifier(self, node):
136 self.w('<i>')
137
138 def depart_classifier(self, node):
139 self.w('</i>')
140
141 def visit_definition(self, node):
142 self.w('</para>\n<para style="Definition">')
143 self.suppress_para = 1
144
145 def depart_definition(self, node):
146 self.suppress_para = 0
147 self.w('</para>\n')
148
149 def visit_field(self, node):
150 self.w('<para>')
151
152 def depart_field(self, node):
153 self.w('</para>\n')
154
155 def visit_field_body(self, node):
156 self.suppress_para = 1
157 return
158
159 def depart_field_body(self, node):
160 self.suppress_para = 0
161
162 def visit_field_list(self, node):
163 return
164
165 def depart_field_list(self, node):
166 return
167
168 def visit_field_name(self, node):
169 self.w('<b>')
170
171 def depart_field_name(self, node):
172 self.w(':')
173 self.w('</b>')
174
175 # Literal Block
176 def visit_literal_block(self, node):
177 self.w('<para style="BodyText"></para><pycode style="Code">')
178 self.suppress_para = 1
179
180 def depart_literal_block(self, node):
181 self.suppress_para = 0
182 self.w('</pycode>\n')
183
184 # Block Quote
185 def visit_block_quote(self, node):
186 self.w('<para style="Indent">')
187
188 def depart_block_quote(self, node):
189 self.w('</para>\n')
190
191 def visit_image(self, node):
192 ''' EMPTY
193 uri CDATA #REQUIRED
194 alt CDATA #IMPLIED
195 height NMTOKEN #IMPLIED
196 width NMTOKEN #IMPLIED
197 scale NMTOKEN #IMPLIED
198 '''
199 attrs = node.attributes
200 l = ['src="%(uri)s"'%attrs]
201 # TODO: scale
202 self.w('<image filename="%s"/>'%node.attributes['uri'])
203
204 def depart_image(self, node):
205 pass
206
207 #
208 # Tables:
209 # NOT IN DOM YET
210 #
211 def visit_table(self, node):
212 '''
213 +------------------------+------------+----------+----------+
214 | Header row, column 1 | Header 2 | Header 3 | Header 4 |
215 | (header rows optional) | | | |
216 +========================+============+==========+==========+
217 | body row 1, column 1 | column 2 | column 3 | column 4 |
218 +------------------------+------------+----------+----------+
219 | body row 2 | Cells may span columns. |
220 +------------------------+------------+---------------------+
221 | body row 3 | Cells may | - Table cells |
222 +------------------------+ span rows. | - contain |
223 | body row 4 | | - body elements. |
224 +------------------------+------------+---------------------+
225 '''
226 self.w('<table border=1>\n')
227
228 def depart_table(self, node):
229 self.w('</table>\n')
230
231 def visit_tgroup(self, node):
232 pass
233
234 def depart_tgroup(self, node):
235 pass
236
237 def visit_colspec(self, node):
238 pass
239
240 def depart_colspec(self, node):
241 pass
242
243 def visit_row(self, node):
244 self.body.append(self.starttag(node, 'tr', ''))
245
246 def depart_row(self, node):
247 self.body.append('</tr>\n')
248
249 def visit_thead(self, node):
250 self.thead = 1
251
252 def depart_thead(self, node):
253 self.thead = 0
254
255 def visit_tbody(self, node):
256 self.thead = 1
257
258 def depart_tbody(self, node):
259 self.thead = 0
260
261 def visit_entry(self, node):
262 if self.thead:
263 s = 'th '
264 else:
265 s = 'td '
266 attrs = node.attributes
267 if attrs.has_key('morecols'):
268 s = s + 'colspan=%d '%(attrs['morecols']+1)
269 if attrs.has_key('morerows'):
270 s = s + 'rowspan=%d '%(attrs['morerows']+1)
271 self.w('<%svalign="top" align="left">'%s)
272
273 def depart_entry(self, node):
274 if self.thead:
275 self.w('</th>\n')
276 else:
277 self.w('</td>\n')
278
279 def visit_emphasis(self, node):
280 self.w('<i>')
281
282 def depart_emphasis(self, node):
283 self.w('</i>')
284
285 def visit_strong(self, node):
286 self.w('<b>')
287
288 def depart_strong(self, node):
289 self.w('</b>')
290
291 def visit_interpreted(self, node):
292 pass #raise NotImplementedError, node
293
294 def depart_interpreted(self, node):
295 pass #raise NotImplementedError, node
296
297 def visit_literal(self, node):
298 self.w('<div style="Code">')
299
300 def depart_literal(self, node):
301 self.w('</div>')
302
303 def visit_reference(self, node):
304 attrs = node.attributes
305 doc = self.document
306 ok = 1
307 if attrs.has_key('refuri'):
308 self.w('<a href="%s">'%attrs['refuri'])
309 elif doc.explicit_targets.has_key(attrs['refname']):
310 # an external reference has been defined
311 ref = doc.explicit_targets[attrs['refname']]
312 if ref.attributes.has_key('refuri'):
313 self.w('<a href="%s">'%ref.attributes['refuri'])
314 else:
315 self.w('<a href="#%s">'%attrs['refname'])
316 elif doc.implicit_targets.has_key(attrs['refname']):
317 # internal reference
318 name = attrs['refname']
319 self.w('<a href="#%s">'%urllib.quote(name))
320 else:
321 self.w('<span class="formatter_error">target "%s" '
322 'undefined</span>'%attrs['refname'])
323
324 def depart_reference(self, node):
325 self.w('</a>')
326
327 def visit_footnote_reference(self, node):
328 raise NotImplementedError, node
329
330 def visit_substitution_reference(self, node):
331 pass
332
333 def depart_substitution_reference(self, node):
334 pass
335
336 def visit_problematic(self, node):
337 raise NotImplementedError, node
338
339 def depart_problematic(self, node):
340 raise NotImplementedError, node
341
342 def visit_system_message(self, node):
343 print >>sys.stderr, '%s: %s'%(node.attributes['type'], node[0][0].data)
344
345 def depart_system_message(self, node):
346 pass
347
348 def visit_comment(self, node):
349 pass
350
351 def depart_comment(self, node):
352 pass
353
354 def visit_Text(self, node):
355 self.w(cgi.escape(node.data))
356
357 def depart_Text(self, node):
358 pass
359
360 def makexml(filename, outfile=sys.stdout, debug=0):
361 pub = docutils.core.Publisher()
362 pub.set_reader('standalone', None, 'restructuredtext')
363 pub.writer = Writer()
364 pub.get_settings()
365 pub.settings._destination = ''
366 pub.source = docutils.io.StringInput(source=open(filename).read(),
367 encoding='latin-1')
368 pub.destination = docutils.io.StringOutput(encoding='latin-1')
369 document = pub.reader.read(pub.source, pub.parser, pub.settings)
370 pub.apply_transforms(document)
371
372 if debug == 1:
373 print >>outfile, document.pformat()
374 else:
375 print >>outfile, pub.writer.write(document, pub.destination)
376
377 def ns_open(*args):
378 OPEN = '/usr/bin/open'
379 return os.spawnl(os.P_WAIT, OPEN, OPEN, *args)
380
381 def main(filename='slides.txt'):
382 try:
383 os.makedirs('build')
384 except:
385 pass
386 basefile = os.path.realpath(os.path.join('build', os.path.splitext(os.path.basename(filename))[0]))
387 # sio = cStringIO.StringIO()
388 sio = file(basefile+'.xml', 'w')
389 makexml(filename, outfile=sio)
390 # sio.seek(0)
391 sio = file(basefile+'.xml', 'r')
392 datafilename = basefile+'.pdf'
393 pythonpoint.process(sio, datafilename=datafilename)
394 ns_open(datafilename)
395
396 if __name__ == '__main__':
397 main(*sys.argv[1:])
Something went wrong with that request. Please try again.