~ubuntu-branches/ubuntu/jaunty/ubuntu-docs/jaunty-updates

« back to all changes in this revision

Viewing changes to teamstuff/moin2db/xml_docbook.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthew East
  • Date: 2007-10-04 08:17:06 UTC
  • Revision ID: james.westby@ubuntu.com-20071004081706-cfypxumc06nmu6ab
Tags: 7.10.1
* Adding translations to source package
* Don't install libs/pdf directory 
* Restore symlink-dupes script to eliminate duplicate files (LP: 149040)
* Fix debian/rules to take account of new location for pot files (thanks to Martin Pitt)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
"""
3
 
    MoinMoin - docbook Formatter
4
 
 
5
 
    @copyright: 2005 by Mikko Virkkil�<mvirkkil@cc.hut.fi>
6
 
    @license: GNU GPL, see COPYING for details.
7
 
"""
8
 
 
9
 
from MoinMoin.formatter.base import FormatterBase
10
 
from MoinMoin import wikiutil, i18n, config
11
 
from xml.sax import saxutils
12
 
 
13
 
class Formatter(FormatterBase):
14
 
    """
15
 
        Send plain text data.
16
 
    """
17
 
 
18
 
    hardspace = u'###CRAP###'
19
 
 
20
 
    section_should_break = ["abstract",'para', 'emphasis']
21
 
 
22
 
    def __init__(self, request, **kw):
23
 
        '''We should use this for creating the doc'''
24
 
        apply(FormatterBase.__init__, (self, request), kw)
25
 
        from xml.dom import getDOMImplementation
26
 
        dom = getDOMImplementation( "4DOM" ) 
27
 
        self.doc = dom.createDocument(None, "article", dom.createDocumentType("article","-//OASIS//DTD DocBook V4.3//EN","http://www.docbook.org/xml/4.3/docbookx.dtd" ))
28
 
        self.root = self.doc.documentElement
29
 
        self.realdepth=1
30
 
 
31
 
    def startDocument(self, pagename):
32
 
        info = self.doc.createElement("articleinfo")
33
 
        title = self.doc.createElement("title")
34
 
        title.appendChild( self.doc.createTextNode(pagename) )
35
 
        info.appendChild(title)
36
 
        self.root.appendChild(info)
37
 
        return ""
38
 
 
39
 
    def startContent(self, content_id="content", **kwargs):
40
 
        self.cur=self.root
41
 
        return ""
42
 
 
43
 
 
44
 
    def endDocument(self):
45
 
        from xml.dom.ext import PrettyPrint
46
 
        import StringIO
47
 
        txt=""
48
 
        a=StringIO.StringIO(txt)
49
 
        PrettyPrint(self.doc,a)
50
 
        txt=a.getvalue()
51
 
        a.close()
52
 
        return txt
53
 
 
54
 
    def text(self, text):
55
 
        self.cur.appendChild( self.doc.createTextNode(text) )
56
 
        return ""
57
 
 
58
 
 
59
 
 
60
 
    def heading(self, on, depth, **kw):
61
 
        while self.cur.nodeName in self.section_should_break:
62
 
            self.cur=self.cur.parentNode
63
 
               
64
 
        if on:
65
 
            if depth <= self.realdepth:
66
 
                for i in range(depth, self.realdepth):
67
 
                    while(self.cur.nodeName!="section"):
68
 
                        self.cur=self.cur.parentNode
69
 
                    
70
 
                    if len(self.cur.childNodes)<3:
71
 
                        self._addEmptyNode("para")
72
 
 
73
 
                    self.cur=self.cur.parentNode
74
 
                self.realdepth=depth
75
 
 
76
 
            section = self.doc.createElement("section")
77
 
            self.cur.appendChild(section)
78
 
            self.cur=section
79
 
 
80
 
            title=self.doc.createElement("title")
81
 
            self.cur.appendChild(title)
82
 
            self.cur=title
83
 
            self.curdepth=depth
84
 
            self.realdepth+=1
85
 
        else:
86
 
            self.cur=self.cur.parentNode
87
 
 
88
 
 
89
 
        return ""
90
 
            
91
 
 
92
 
 
93
 
    def paragraph(self, on):
94
 
        FormatterBase.paragraph(self, on)
95
 
        if on:
96
 
            para=self.doc.createElement("para")
97
 
            self.cur.appendChild(para)
98
 
            self.cur=para
99
 
        else:
100
 
            self.cur=self.cur.parentNode
101
 
        return ""
102
 
 
103
 
    def linebreak(self, preformatted=1):
104
 
        if preformatted:
105
 
            self.text('\\n')
106
 
        else:
107
 
            self.text('CRAP')
108
 
        return ""
109
 
 
110
 
 
111
 
    def _handleNode(self, name, on, attributes=()):
112
 
        if on:
113
 
            node=self.doc.createElement(name)
114
 
            self.cur.appendChild(node)
115
 
            if len(attributes)>0:
116
 
                for (name,value) in attributes:
117
 
                    node.setAttribute(name,value)
118
 
            self.cur=node
119
 
        else:
120
 
            self.cur=self.cur.parentNode
121
 
        return ""
122
 
 
123
 
    def _addEmptyNode(self,name, attributes=()):
124
 
        node=self.doc.createElement(name)
125
 
        self.cur.appendChild(node)
126
 
        if len(attributes)>0:
127
 
            for (name,value) in attributes:
128
 
                node.setAttribute(name,value)
129
 
 
130
 
### Inline ##########################################################
131
 
 
132
 
    def _handleFormatting(self,name, on, attributes=()):
133
 
        #We add all the elements we create to the list of elements that should not contain a section        
134
 
        if name not in self.section_should_break:
135
 
            self.section_should_break.append(name)
136
 
 
137
 
        return self._handleNode(name,on,attributes)
138
 
 
139
 
    def strong(self, on):
140
 
        return self._handleFormatting("emphasis",on , [['role','bold']] )
141
 
 
142
 
    def emphasis(self, on):
143
 
        return self._handleFormatting("emphasis",on)
144
 
 
145
 
    def underline(self, on):
146
 
        return self._handleFormatting("emphasis",on , [['role','underline']] )
147
 
 
148
 
    def highlight(self, on):
149
 
        return self._handleFormatting("emphasis",on , [['role','highlight']] )
150
 
 
151
 
    def sup(self, on):
152
 
        return self._handleFormatting("superscript",on)
153
 
 
154
 
    def sub(self, on):
155
 
        return self._handleFormatting("subscript",on)
156
 
 
157
 
    def code(self, on):
158
 
        return self._handleFormatting("code",on)
159
 
 
160
 
    def preformatted(self, on):
161
 
        return self._handleFormatting("screen",on)
162
 
 
163
 
 
164
 
### Lists ###########################################################
165
 
 
166
 
    def number_list(self, on, type=None, start=None):
167
 
        docbook_ol_types={  '1':"arabic", 
168
 
                            'a':"loweralpha", 
169
 
                            'A':"upperalpha",
170
 
                            'i':"lowerroman",
171
 
                            'I':"upperroman" }
172
 
 
173
 
        if type and docbook_ol_types.has_key(type):
174
 
            attrs=[("numeration", docbook_ol_types[type])]
175
 
        else:
176
 
            attrs=[]
177
 
 
178
 
        return self._handleNode('orderedlist',on,attrs)
179
 
 
180
 
 
181
 
    def bullet_list(self, on):
182
 
        return self._handleNode("itemizedlist",on)
183
 
 
184
 
    def definition_list(self, on):
185
 
        return self._handleNode("glosslist",on)        
186
 
 
187
 
 
188
 
    '''When on is false, we back out just on level. This is
189
 
       ok because we know definition_desc gets called, and we
190
 
       back out two levels there'''
191
 
    def definition_term(self, on, compact=0):
192
 
        if on:
193
 
            entry=self.doc.createElement('glossentry')
194
 
            term=self.doc.createElement('glossterm')
195
 
            entry.appendChild(term)
196
 
            self.cur.appendChild(entry)
197
 
 
198
 
            self.cur=term
199
 
        else:
200
 
            self.cur=self.cur.parentNode
201
 
        return ""
202
 
   
203
 
    '''We backout two levels when 'on' is false, to leave the glossentry stuff'''
204
 
    def definition_desc(self, on):
205
 
        if on:
206
 
            return self._handleNode("glossdef",on)
207
 
        else:
208
 
            self.cur=self.cur.parentNode.parentNode
209
 
            return ""
210
 
 
211
 
    def listitem(self, on, **kw):
212
 
        if on:
213
 
            node=self.doc.createElement("listitem")
214
 
            self.cur.appendChild(node)
215
 
 
216
 
            self.cur=node
217
 
        else:
218
 
            self.cur=self.cur.parentNode
219
 
        return ""
220
 
 
221
 
 
222
 
### Links ###########################################################
223
 
 
224
 
    #FIXME: This is quite crappy
225
 
    def pagelink(self, on, pagename='', page=None, **kw):
226
 
        apply(FormatterBase.pagelink, (self, on, pagename, page), kw)
227
 
 
228
 
        return self.interwikilink(on,'self',pagename) #FIXME
229
 
 
230
 
    #FIXME: This is even more crappy
231
 
    def interwikilink(self, on, interwiki='', pagename='', **kw):
232
 
        if not on:
233
 
            self.url(on,kw)
234
 
            return ""
235
 
 
236
 
        wikitag, wikiurl, wikitail, wikitag_bad = wikiutil.resolve_wiki(self.request, '%s:%s' % (interwiki, pagename))
237
 
        wikiurl = wikiutil.mapURL(self.request, wikiurl)
238
 
        href = wikiutil.join_wiki(wikiurl, wikitail)
239
 
        self.url(on, href)
240
 
 
241
 
        #return self._handleNode("link",on,[['linkend',interwiki+"/"+pagename]]) #FIXME
242
 
        return ""
243
 
            
244
 
    def url(self, on, url=None, css=None, **kw):
245
 
        return self._handleNode("ulink",on,[['url',url]])
246
 
 
247
 
    def anchordef(self, name):
248
 
        self._handleNode("anchor",True,[['id',name]])
249
 
        self._handleNode("ulink",False)
250
 
        return ""
251
 
 
252
 
    def anchorlink(self, on, name='', id=None):
253
 
        attrs=[]
254
 
        if name!='':
255
 
            attrs.append( ('endterm',name) )
256
 
        if id!=None:
257
 
            attrs.append( ('linkend',id) )
258
 
        elif name!='':
259
 
            attrs.append( ('linkend',name) )
260
 
 
261
 
        return self._handleNode("link",on,attrs)
262
 
 
263
 
 
264
 
### Images and Smileys ##############################################
265
 
    def image(self, **kw):
266
 
        media=self.doc.createElement('inlinemediaobject')
267
 
 
268
 
        imagewrap=self.doc.createElement('imageobject')
269
 
        media.appendChild(imagewrap)
270
 
 
271
 
        image=self.doc.createElement('imagedata')
272
 
        if kw.has_key('src'):
273
 
            image.setAttribute('fileref',kw['src'])
274
 
        if kw.has_key('width'):
275
 
            image.setAttribute('width',kw['width'])
276
 
        if kw.has_key('height'):
277
 
            image.setAttribute('depth',kw['height'])
278
 
        imagewrap.appendChild(image)
279
 
        
280
 
        if kw.has_key('alt'):
281
 
            txtcontainer=self.doc.createElement('textobject')
282
 
            media.appendChild(txtcontainer)        
283
 
            txtphrase=self.doc.createElement('phrase')
284
 
            txtphrase.appendChild( self.doc.createTextNode(kw['alt']) )
285
 
            txtcontainer.appendChild(txtphrase)        
286
 
        
287
 
        self.cur.appendChild(media)
288
 
        return ""        
289
 
 
290
 
    def smiley(self, text):
291
 
        w, h, b, img = config.smileys[text.strip()]
292
 
        href = img
293
 
        if not href.startswith('/'):
294
 
            href = self.request.theme.img_url(img)
295
 
        return self.image(src=href, alt=text, width=str(w), height=str(h))
296
 
 
297
 
    def icon(self, type):
298
 
        return ''#self.request.theme.make_icon(type)
299
 
 
300
 
### Tables ##########################################################
301
 
 
302
 
    #FIXME: We should copy code from text_html.py for attr handling
303
 
    
304
 
 
305
 
    def table(self, on, attrs=None):
306
 
        sanitized_attrs=[]
307
 
        if attrs and attrs.has_key('id'):
308
 
            sanitized_attrs[id]=attrs['id']
309
 
 
310
 
        self._handleNode("table",on,sanitized_attrs)
311
 
        if on:
312
 
            self._addEmptyNode("caption") #dtd for table requires caption
313
 
 
314
 
        return ""
315
 
    
316
 
    def table_row(self, on, attrs=None):
317
 
        sanitized_attrs=[]
318
 
        if attrs and attrs.has_key('id'):
319
 
            sanitized_attrs[id]=attrs['id']
320
 
        return self._handleNode("tr",on,sanitized_attrs)
321
 
    
322
 
    def table_cell(self, on, attrs=None):
323
 
        sanitized_attrs=[]
324
 
        if attrs and attrs.has_key('id'):
325
 
            sanitized_attrs[id]=attrs['id']
326
 
        return self._handleNode("td",on,sanitized_attrs)
327
 
 
328
 
 
329
 
### Code ############################################################
330
 
    def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1):
331
 
        if show:
332
 
            show = 'numbered'
333
 
        else:
334
 
            show = 'unnumbered'
335
 
        if start<1:
336
 
            start=1
337
 
        
338
 
        attrs=[ ['id',code_id],
339
 
                ['linenumbering',show],
340
 
                ['startinglinenumber',str(start)],
341
 
                ['language',code_type],
342
 
                ['format','linespecific'] 
343
 
                ]
344
 
        return self._handleFormatting("screen", on, attrs )
345
 
 
346
 
    def code_line(self, on):
347
 
        return '' #No clue why something should be done here
348
 
    
349
 
 
350
 
 
351
 
 
352
 
    def code_token(self, on, tok_type):
353
 
        toks_map={  'ID':'methodname',
354
 
                    'Operator':'',
355
 
                    'Char':'',
356
 
                    'Comment':'lineannotation',
357
 
                    'Number':'',
358
 
                    'String':'phrase',
359
 
                    'SPChar':'',
360
 
                    'ResWord':'token',
361
 
                    'ConsWord':'symbol',
362
 
                    'Error':'errortext',
363
 
                    'ResWord2':'',
364
 
                    'Special':'',
365
 
                    'Preprc':'',
366
 
                    'Text':''}
367
 
        if toks_map.has_key(tok_type) and toks_map[tok_type] != '':
368
 
            return self._handleFormatting(toks_map[tok_type],on)
369
 
        else:
370
 
            return ""
371
 
 
372
 
 
373
 
### Not supported ###################################################
374
 
    def rule(self, size=0):
375
 
        return ""
376
 
 
377
 
    def small(self, on):
378
 
        return ""
379
 
 
380
 
    def big(self, on):
381
 
        return ""
382
 
 
383
 
    def rawHTML(self, markup):
384
 
        """ This allows emitting pre-formatted HTML markup, and should be
385
 
            used wisely (i.e. very seldom).
386
 
 
387
 
            Using this event while generating content results in unwanted
388
 
            effects, like loss of markup or insertion of CDATA sections
389
 
            when output goes to XML formats.
390
 
        """
391
 
        return ''
392
 
 
393
 
    def escapedText(self, on):
394
 
        """ This allows emitting text as-is, anything special will
395
 
            be escaped (at least in HTML, some text output format
396
 
            would possibly do nothing here)
397
 
        """
398
 
        return ''