This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
text_dokuwiki.py
346 lines (277 loc) · 8.06 KB
/
text_dokuwiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# -*- coding: utf-8 -*-
# Setup VIM: ex: noet ts=2 sw=2 :
"""
MoinMoin - Dokuwiki Formatter
@copyright: 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
@copyright: 2011-2012 Elan Ruusamäe <glen@delfi.ee>
@license: GNU GPL, see COPYING for details.
"""
from xml.sax import saxutils
from MoinMoin.formatter.base import FormatterBase
from MoinMoin import config
from MoinMoin.Page import Page
from types import *
# TODO: let base class MoinMoin/formatter/base.py handle not implemented methods
class Formatter(FormatterBase):
"""
Send Dokuwiki formatted data.
"""
hardspace = ' '
# hardspace = ' '
def __init__(self, request, **kw):
apply(FormatterBase.__init__, (self, request), kw)
self._current_depth = 1
self._base_depth = 0
self.in_pre = 0
self.in_table = 0
self._text = None # XXX does not work with links in headings!!!!!
self.list_depth = 0
self.list_type = ' '
def _escape(self, text, extra_mapping={"'": "'", '"': """}):
return saxutils.escape(text, extra_mapping)
def startDocument(self, pagename):
encoding = config.charset
return '<?xml version="1.0" encoding="%s"?>\n<s1 title="%s">' % (
encoding, self._escape(pagename))
def endDocument(self):
result = ""
while self._current_depth > 1:
result += "</s%d>" % self._current_depth
self._current_depth -= 1
return result + '</s1>'
def lang(self, on, lang_name):
return ('<div lang="">' % lang_name, '</div>')[not on]
def sysmsg(self, on, **kw):
return ('<sysmsg>', '</sysmsg>')[not on]
def rawHTML(self, markup):
return '<html>' + markup + '</html>'
def pagelink(self, on, pagename='', page=None, **kw):
if on:
return '[[:' + ":".join(pagename.split("/")) + "|"
else:
return ']]'
def interwikilink(self, on, interwiki='', pagename='', **kw):
if on:
if interwiki == 'Self':
return self.pagelink(on, pagename, **kw)
return '[[%s>%s|' % (interwiki, pagename)
else:
return ']]'
def url(self, on, url='', css=None, **kw):
return ('[[%s|' % (self._escape(url)), ']]')[not on]
def attachment_link(self, url, text, **kw):
return '{{%s|%s}}' % (url, text)
def attachment_image(self, url, **kw):
return '{{%s|}}' % (url,)
def attachment_drawing(self, url, text, **kw):
return '{{%s|%s}}' % (url, text)
def text(self, text, **kw):
self._did_para = 0
if self._text is not None:
self._text.append(text)
return text
def rule(self, size=0, **kw):
# size not supported
if size >= 4:
return '----\n'
else:
return '-' * size + '\n'
def icon(self, type):
return '<icon type="%s" />' % type
def strong(self, on, **kw):
return ['**', '**'][not on]
def emphasis(self, on, **kw):
return ['//', '//'][not on]
def highlight(self, on, **kw):
return ['**', '**'][not on]
def number_list(self, on, type=None, start=None, **kw):
# list type not supported
if on:
self.list_depth += 1
self.list_type = '-'
else:
self.list_depth -= 1
self.list_type = ' '
return ['', '\n'][on]
def bullet_list(self, on, **kw):
if on:
self.list_depth += 1
self.list_type = '*'
else:
self.list_depth -= 1
self.list_type = ' '
return ['', '\n'][on]
def listitem(self, on, **kw):
# somewhy blockquote uses "listitem" call
return [(' ' * self.list_depth * 2) + self.list_type + ' ', '\n'][not on]
def code(self, on, **kw):
""" `typewriter` or {{{typerwriter}}, for code blocks i hope codeblock works """
return ["''", "''"][not on]
def sup(self, on, **kw):
return ['<sup>', '</sup>'][not on]
def sub(self, on, **kw):
return ['<sub>', '</sub>'][not on]
def strike(self, on, **kw):
return ['<del>', '</del>'][not on]
def preformatted(self, on, **kw):
FormatterBase.preformatted(self, on)
result = ''
if self.in_p:
result = self.paragraph(0)
return result + ['<file>', '</file>\n'][not on]
def paragraph(self, on, **kw):
FormatterBase.paragraph(self, on)
if self.in_table or self.list_depth:
return ''
return ['', '\n\n'][not on]
def linebreak(self, preformatted=1):
return ['\n', '\\\n'][not preformatted]
def heading(self, on, depth, **kw):
# heading depth reversed in dokuwiki
heading_depth = 7 - depth
if on:
return u'%s ' % (u'=' * heading_depth)
else:
return u' %s\n' % (u'=' * heading_depth)
def table(self, on, attrs={}, **kw):
if on:
self.in_table = 1
else:
self.in_table = 0
return ''
def table_row(self, on, attrs={}, **kw):
return ['\n', '|'][not on]
def table_cell(self, on, attrs={}, **kw):
return ['|', ''][not on]
def anchordef(self, id):
# not supported
return ''
def anchorlink(self, on, name='', **kw):
# kw.id not supported, we hope the anchor matches existing heading on page
return ('[[#', ']]') [not on]
def underline(self, on, **kw):
return ['__', '__'][not on]
def definition_list(self, on, **kw):
result = ''
if self.in_p:
result = self.paragraph(0)
return result + ['<gloss>', '</gloss>'][not on]
def definition_term(self, on, compact=0, **kw):
return ['<label>', '</label>'][not on]
def definition_desc(self, on, **kw):
return ['<item>', '</item>'][not on]
def image(self, src=None, **kw):
valid_attrs = ['src', 'width', 'height', 'alt', 'title']
url = src
if '?' in url:
url += '&'
else:
url += '?'
attrs = {}
for key, value in kw.items():
if key in valid_attrs:
attrs[key] = value
# TODO: finish this
if attrs.has_key('width'):
url += attrs['width']
return '{{' + url + '}}'
def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1):
syntax = ''
# switch for Python: http://simonwillison.net/2004/may/7/switch/
try:
syntax = {
'ColorizedPython': 'python',
'ColorizedPascal': 'pascal',
'ColorizedJava': 'java',
'ColorizedCPlusPlus': 'cpp',
}[code_type]
except KeyError:
pass
return ('<code %s>' % syntax , '</code>')[not on]
def code_line(self, on):
return ('', '\n')[on]
def code_token(self, on, tok_type):
# not supported
return ''
def comment(self, text):
# real comments (lines with two hash marks)
if text[0:2] == '##':
return "/* %s */\n" % text[2:].strip()
# Some kind of Processing Instruction
# http://moinmo.in/HelpOnProcessingInstructions
tokens = text.lstrip('#').split(None, 1)
if tokens[0] in ('language', 'format', 'refresh'):
return ''
if tokens[0] == 'acl':
# TODO: fill acl.auth.php
return ''
if tokens[0] == 'deprecated':
return '<note warning>This page is deprecated</note>\n'
if tokens[0] == 'redirect':
return text + "\n"
if tokens[0] == 'pragma':
# TODO: can do 'description' via 'meta' dokuwiki plugin
return "/* pragma: %s */\n" % " ".join(tokens[1:])
return "/* %s */\n" % text.lstrip('#')
def macro(self, macro_obj, name, args):
def email(args):
mail = args.replace(' AT ', '@')
mail = mail.replace(' DOT ', '.')
return '[[%s|%s]]' % (mail, args)
# function which will just do what parent class would
def inherit(args):
return apply(FormatterBase.macro, (self, macro_obj, name, args))
try:
lookup = {
'BR' : '\\\\',
'MailTo' : email,
'GetText' : args,
'ShowSmileys' : inherit,
}[name]
except KeyError:
lookup = '/* UndefinedMacro: %s(%s) */' % (name, args)
if type(lookup) == FunctionType:
text = lookup(args)
else:
text = lookup
return text
def smiley(self, text):
try:
# https://www.dokuwiki.org/devel:smileys.conf
return {
# note: reverse sorted so that longer smileys get matched first
'X-(' : ':-X',
'{X}' : ':!:',
'{*}' : '<ubu>',
'(./)' : u'✓',
':))' : ':-P',
':-))' : ':-P',
':-?' : ':-P',
':o' : ':-o',
'{OK}' : ':!:',
'{o}' : '<circ>',
'{i}' : ':!:',
':D' : ':-D',
'B)' : '8-)',
'B-)' : '8-)',
'{3}' : '<3>',
'{2}' : '<2>',
'{1}' : '<1>',
'(!)' : ':!:',
'/!\\' : ':!:',
':\\' : ':-\\',
':))' : ':-)',
':)' : ':-)',
':(' : ':-(',
':-))' : ':-)',
':-)' : ':-)',
':-(' : ':-(',
';)' : ';-)',
'|)' : ':-|',
'|-)' : ':-|',
'>:>' : '^_^',
'<!>' : ':!:',
'<:(' : ':-?',
}[text]
except KeyError:
return text