Attachment 'emitter.py'
Download 1 import re, StringIO
2 from MoinMoin import config, wikimacro, wikiutil
3
4 class DocEmitter:
5 """
6 Generate the output for the document tree consisting of DocNodes.
7 """
8
9 def __init__(self, root, formatter, request):
10 self.root = root
11 self.formatter = formatter
12 self.request = request
13 self.form = request.form
14 self.macro = None
15
16 def get_image(self, addr, text=''):
17 """Return markup for image depending on the address."""
18 url = wikiutil.url_unquote(addr, want_unicode=True)
19 if addr.startswith('http:'):
20 return self.formatter.image(src=url,alt=text, html_class='external_image')
21 else:
22 return self.formatter.attachment_image(url, alt=text, html_class='image')
23
24 def get_text(self, node):
25 """Try to emit whatever text is in the node."""
26 try:
27 return node.children[0].content or ''
28 except:
29 return node.content or ''
30
31 # *_emit methods for emitting nodes of the document
32
33 def document_emit(self, node):
34 return self.emit_children(node)
35
36 def text_emit(self, node):
37 return self.formatter.text(node.content or '')
38
39 def rule_emit(self, node):
40 return self.formatter.rule()
41
42 def paragraph_emit(self, node):
43 return ''.join([
44 self.formatter.paragraph(1),
45 self.emit_children(node),
46 self.formatter.paragraph(0),
47 ])
48
49 def bullet_list_emit(self, node):
50 return ''.join([
51 self.formatter.bullet_list(1),
52 self.emit_children(node),
53 self.formatter.bullet_list(0),
54 ])
55
56 def number_list_emit(self, node):
57 return ''.join([
58 self.formatter.number_list(1),
59 self.emit_children(node),
60 self.formatter.number_list(0),
61 ])
62
63 def list_item_emit(self, node):
64 return ''.join([
65 self.formatter.listitem(1),
66 self.emit_children(node),
67 self.formatter.listitem(0),
68 ])
69
70 def definition_list_emit(self, node):
71 return ''.join([
72 self.formatter.definition_list(1),
73 self.emit_children(node),
74 self.formatter.definition_list(0),
75 ])
76
77 def term_emit(self, node):
78 return ''.join([
79 self.formatter.definition_term(1),
80 self.emit_children(node),
81 self.formatter.definition_term(0),
82 ])
83
84 def definition_emit(self, node):
85 return ''.join([
86 self.formatter.definition_desc(1),
87 self.emit_children(node),
88 self.formatter.definition_desc(0),
89 ])
90
91 def table_emit(self, node):
92 return ''.join([
93 self.formatter.table(1, attrs=node.attrs),
94 self.emit_children(node),
95 self.formatter.table(0),
96 ])
97
98 def table_row_emit(self, node):
99 return ''.join([
100 self.formatter.table_row(1, attrs=node.attrs),
101 self.emit_children(node),
102 self.formatter.table_row(0),
103 ])
104
105 def table_cell_emit(self, node):
106 return ''.join([
107 self.formatter.table_cell(1, attrs=node.attrs),
108 self.emit_children(node),
109 self.formatter.table_cell(0),
110 ])
111
112
113
114 def emphasis_emit(self, node):
115 return ''.join([
116 self.formatter.emphasis(1),
117 self.emit_children(node),
118 self.formatter.emphasis(0),
119 ])
120
121 def quote_emit(self, node):
122 return ''.join([
123 self.formatter.rawHTML('<q>'),
124 self.emit_children(node),
125 self.formatter.rawHTML('</q>'),
126 ])
127
128 def strong_emit(self, node):
129 return ''.join([
130 self.formatter.strong(1),
131 self.emit_children(node),
132 self.formatter.strong(0)
133 ])
134
135 def smiley_emit(self, node):
136 return self.formatter.smiley(node.content)
137
138 def header_emit(self, node):
139 import sha
140 pntt = self.formatter.page.page_name + self.get_text(node)
141 ident ="head-"+sha.new(pntt.encode(config.charset)).hexdigest()
142 return ''.join([
143 self.formatter.heading(1, node.level, id=ident),
144 self.formatter.text(node.content or ''),
145 self.formatter.heading(0, node.level),
146 ])
147
148 def code_emit(self, node):
149 return ''.join([
150 self.formatter.code(1),
151 self.formatter.text(node.content or ''),
152 self.formatter.code(0),
153 ])
154
155 def abbr_emit(self, node):
156 return ''.join([
157 self.formatter.rawHTML('<abbr title="%s">'%node.title),
158 self.formatter.text(node.content or ''),
159 self.formatter.rawHTML('</abbr>'),
160 ])
161
162 def page_link_emit(self, node):
163 word = node.content
164 # handle relative links
165 if word.startswith(wikiutil.CHILD_PREFIX):
166 word = self.formatter.page.page_name + '/' + word[wikiutil.CHILD_PREFIX_LEN:]
167 # handle anchors
168 parts = word.split("#", 1)
169 anchor = ""
170 if len(parts)==2:
171 word, anchor = parts
172 return ''.join([
173 self.formatter.pagelink(1, word, anchor=anchor),
174 self.emit_children(node),
175 self.formatter.pagelink(0, word),
176 ])
177
178 def external_link_emit(self, node):
179 return ''.join([
180 self.formatter.url(1, node.content, css='www %s'%node.proto),
181 self.emit_children(node),
182 self.formatter.url(0),
183 ])
184
185 def anchor_link_emit(self, node):
186 return ''.join([
187 self.formatter.url(1, node.content, css='anchor'),
188 self.emit_children(node),
189 self.formatter.url(0),
190 ])
191
192 def interwiki_link_emit(self, node):
193 word = node.content
194 ( wikitag,
195 wikiurl,
196 wikitail,
197 wikitag_bad ) = wikiutil.resolve_wiki(self.request, word)
198 href = wikiutil.join_wiki(wikiurl, wikitail)
199 return ''.join([
200 self.formatter.interwikilink(1, wikitag, wikitail),
201 self.emit_children(node),
202 self.formatter.interwikilink(0),
203 ])
204
205 def attachment_emit(self, node):
206 url = wikiutil.url_unquote(node.content, want_unicode=True)
207 text = self.get_text(node)
208 return self.formatter.attachment_link(url, text)
209
210 def inlined_attachment_emit(self, node):
211 url = wikiutil.url_unquote(node.content, want_unicode=True)
212 text = self.get_text(node)
213 return self.formatter.attachment_inlined(url, text)
214
215 def image_emit(self, node):
216 text = self.get_text(node)
217 return self.get_image(node.content, text)
218
219 def drawing_emit(self, node):
220 url = wikiutil.url_unquote(node.content, want_unicode=True)
221 text = self.get_text(node)
222 return self.formatter.attachment_drawing(url, text)
223
224 def figure_emit(self, node):
225 text = self.get_text(node)
226 url = wikiutil.url_unquote(node.content, want_unicode=True)
227 return ''.join([
228 self.formatter.rawHTML('<div class="figure">'),
229 self.get_image(url, text), self.emit_children(node),
230 self.formatter.rawHTML('</div>')
231 ])
232
233 def bad_link_emit(self, node):
234 return self.formatter.text(''.join([
235 '[[',
236 node.content or '',
237 ']]',
238 ]))
239
240 def macro_emit(self, node):
241 macro_name = node.content
242 args = node.args
243 if self.macro is None:
244 self.macro = wikimacro.Macro(self)
245 try:
246 return self.formatter.macro(self.macro, macro_name, args)
247 except:
248 return self.formatter.text(self.request.getText('macro error'))
249
250 def section_emit(self, node):
251 return ''.join([
252 self.formatter.rawHTML(
253 '<div class="%s" style="%s">'%(node.sect, node.style)
254 ),
255 self.emit_children(node),
256 self.formatter.rawHTML('</div>'),
257 ])
258
259 def blockquote_emit(self, node):
260 return ''.join([
261 self.formatter.rawHTML('<blockquote>'),
262 self.emit_children(node),
263 self.formatter.rawHTML('</blockquote>'),
264 ])
265
266 def preformatted_emit(self, node):
267 content = node.content
268 self.processor_name = node.sect
269 self.processor = None
270 if self.processor_name:
271 self._setProcessor(self.processor_name)
272 if self.processor is None:
273 return ''.join([
274 self.formatter.preformatted(1),
275 self.formatter.text(content),
276 self.formatter.preformatted(0),
277 ])
278 else:
279 buff = StringIO.StringIO()
280 self.request.redirect(buff)
281 try:
282 self.formatter.processor(
283 self.processor_name,
284 content.split('\n'),
285 self.processor_is_parser
286 )
287 finally:
288 self.request.redirect()
289 return buff.getvalue()
290
291 def default_emit(self, node):
292 return ''.join([
293 self.formatter.preformatted(1),
294 self.formatter.text('<%s>\n'%node.kind),
295 self.emit_children(node),
296 self.formatter.preformatted(0)
297 ])
298
299 ##
300 def emit_children(self, node):
301 """Emit all the children of a node."""
302 return ''.join([self.emit_node(child) for child in node.children])
303
304 def emit_node(self, node):
305 try:
306 emit = getattr(self, '%s_emit'%node.kind)
307 except:
308 emit = self.default_emit
309 return emit(node)
310
311 def emit(self):
312 return '\n'.join([
313 self.emit_node(self.root),
314 ])
315
316 # From the wiki.py parser
317 def _setProcessor(self, name):
318 """ Set processer to either processor or parser named 'name' """
319 cfg = self.request.cfg
320 try:
321 self.processor = wikiutil.importPlugin(
322 cfg,
323 "processor",
324 name,
325 "process"
326 )
327 self.processor_is_parser = 0
328 except wikiutil.PluginMissingError:
329 try:
330 self.processor = wikiutil.importPlugin(
331 cfg,
332 "parser",
333 name,
334 "Parser"
335 )
336 self.processor_is_parser = 1
337 except wikiutil.PluginMissingError:
338 self.processor = None
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.