File: text_xml.py

package info (click to toggle)
moin 1.9.9-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 76,024 kB
  • sloc: python: 143,896; java: 10,704; php: 2,385; perl: 1,574; xml: 371; makefile: 214; sh: 81; sed: 5
file content (231 lines) | stat: -rw-r--r-- 7,119 bytes parent folder | download | duplicates (7)
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
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - "text/xml" Formatter

    @copyright: 2000-2002 Juergen Hermann <jh@web.de>
    @license: GNU GPL, see COPYING for details.
"""

from xml.sax import saxutils
from MoinMoin.formatter import FormatterBase
from MoinMoin import config
from MoinMoin.Page import Page

class Formatter(FormatterBase):
    """
        Send XML data.
    """

    hardspace = '&nbsp;'

    def __init__(self, request, **kw):
        FormatterBase.__init__(self, request, **kw)
        self._current_depth = 1
        self._base_depth = 0
        self.in_pre = 0

    def _escape(self, text, extra_mapping={"'": "&apos;", '"': "&quot;"}):
        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 '<![CDATA[' + markup.replace(']]>', ']]>]]&gt;<![CDATA[') + ']]>'

    def pagelink(self, on, pagename='', page=None, **kw):
        FormatterBase.pagelink(self, on, pagename, page, **kw)
        if page is None:
            page = Page(self.request, pagename, formatter=self)
        return page.link_to(self.request, on=on, **kw)

    def interwikilink(self, on, interwiki='', pagename='', **kw):
        if on:
            return '<interwiki wiki="%s" pagename="%s">' % (interwiki, pagename)
        else:
            return '</interwiki>'

    def url(self, on, url='', css=None, **kw):
        if css:
            str = ' class="%s"' % css
        else:
            str = ''
        return ('<jump href="%s"%s>' % (self._escape(url), str), '</jump>') [not on]

    def attachment_link(self, on, url=None, **kw):
        if on:
            return '<attachment href="%s">' % (url, )
        else:
            return '</attachment>'

    def attachment_image(self, url, **kw):
        return '<attachmentimage href="%s"></attachmentimage>' % (url, )

    def attachment_drawing(self, url, text, **kw):
        return '<attachmentdrawing href="%s">%s</attachmentdrawing>' % (url, text)

    def text(self, text, **kw):
        if self.in_pre:
            return text.replace(']]>', ']]>]]&gt;<![CDATA[')
        return self._escape(text)

    def rule(self, size=0, **kw):
        return "\n<br/>%s<br/>\n" % ("-" * 78, ) # <hr/> not supported in stylebook
#        if size:
#            return '<hr size="%d"/>\n' % (size, )
#        else:
#            return '<hr/>\n'

    def icon(self, type):
        return '<icon type="%s" />' % type

    def strong(self, on, **kw):
        return ['<strong>', '</strong>'][not on]

    def emphasis(self, on, **kw):
        return ['<em>', '</em>'][not on]

    def highlight(self, on, **kw):
        return ['<strong>', '</strong>'][not on]

    def number_list(self, on, type=None, start=None, **kw):
        result = ''
        if self.in_p:
            result = self.paragraph(0)
        return result + ['<ol>', '</ol>\n'][not on]

    def bullet_list(self, on, **kw):
        result = ''
        if self.in_p:
            result = self.paragraph(0)
        return result + ['<ul>', '</ul>\n'][not on]

    def listitem(self, on, **kw):
        return ['<li>', '</li>\n'][not on]

    def code(self, on, **kw):
        return ['<code>', '</code>'][not on]

    def small(self, on, **kw):
        return ['<small>', '</small>'][not on]

    def big(self, on, **kw):
        return ['<big>', '</big>'][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 ['<strike>', '</strike>'][not on]

    def preformatted(self, on, **kw):
        FormatterBase.preformatted(self, on)
        result = ''
        if self.in_p:
            result = self.paragraph(0)
        return result + ['<source><![CDATA[', ']]></source>'][not on]

    def paragraph(self, on, **kw):
        FormatterBase.paragraph(self, on)
        return ['<p>', '</p>\n'][not on]

    def linebreak(self, preformatted=1):
        return ['\n', '<br/>\n'][not preformatted]

    def heading(self, on, depth, id=None, **kw):
        if not on:
            return '">\n'
        # remember depth of first heading, and adapt current depth accordingly
        if not self._base_depth:
            self._base_depth = depth
        depth = max(depth + (2 - self._base_depth), 2)

        # close open sections
        result = ""
        while self._current_depth >= depth:
            result = result + "</s%d>\n" % self._current_depth
            self._current_depth -= 1
        self._current_depth = depth

        id_text = ''
        if id:
            id_text = ' id="%s"' % id

        return result + '<s%d%s title="' % (depth, id_text)

    def table(self, on, attrs={}, **kw):
        return ['<table>', '</table>'][not on]

    def table_row(self, on, attrs={}, **kw):
        return ['<tr>', '</tr>'][not on]

    def table_cell(self, on, attrs={}, **kw):
        return ['<td>', '</td>'][not on]

    def anchordef(self, id):
        return '<anchor id="%s"/>' % id

    def anchorlink(self, on, name='', **kw):
        id = kw.get('id', None)
        extra = ''
        if id:
            extra = ' id="%s"' % id
        return ('<link anchor="%s"%s>' % (name, extra), '</link>') [not on]

    def underline(self, on, **kw):
        return self.strong(on) # no underline in StyleBook

    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']
        attrs = {'src': src}
        for key, value in kw.items():
            if key in valid_attrs:
                attrs[key] = value
        return FormatterBase.image(self, **attrs) + '</img>'

    def transclusion(self, on, **kw):
        # TODO, see text_html formatter
        return ''

    def transclusion_param(self, **kw):
        # TODO, see text_html formatter
        return ''

    def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1, msg=None):
        return ('<codearea id="%s">' % code_id, '</codearea')[not on]

    def code_line(self, on):
        return ('<codeline>', '</codeline')[not on]

    def code_token(self, on, tok_type):
        return ('<codetoken type="%s">' % tok_type, '</codetoken')[not on]