Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mwlib/htmlwriter.py
blob: d169482eb1cf73afc4ce638d583f013e8b5582d9 (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2007-2008 PediaPress GmbH
# See README.txt for additional licensing information.

import os
from mwlib import parser, rendermath, timeline

import urllib
import cgi

#from PIL import Image

from mwlib.log import Log

log = Log("htmlwriter")

class HTMLWriter(object):
    imglevel = 0
    namedLinkCount = 1
    def __init__(self, out, images=None, math_renderer=None):
        self.out = out
        self.level = 0
        self.images = images
        # self.images = imgdb.ImageDB(os.path.expanduser("~/images"))
        self.references = []
        if math_renderer is None:
            self.math_renderer = rendermath.Renderer()
        else:
            self.math_renderer = math_renderer
    
    def _write(self, s):
        self.out.write(cgi.escape(s).encode('utf8'))

    def getCategoryList(self, obj):
        categories = list(set(c.target for c in obj.find(parser.CategoryLink)))
        categories.sort()
        return categories
                    
    def write(self, obj):
        m = "write" + obj.__class__.__name__
        m=getattr(self, m)
        m(obj)

    def ignore(self, obj):
        pass

    def serializeVList(self,vlist):
        args = []
        styleArgs = []
        gotClass = 0
        gotExtraClass = 0
        for (key,value) in vlist.items():
            if isinstance(value, (basestring, int)):
                if key=="class":
                    args.append('%s="%s"' % (key, value))
                    gotClass = 1
                else:
                    args.append('%s="%s"' % (key, value))
            if isinstance(value, dict) and key=="style":
                for (_key,_value) in value.items():
                    styleArgs.append("%s:%s" % (_key, _value))
                args.append(' style="%s"' % ';'.join(styleArgs))
                gotExtraClass = 1
        return ' '.join(args)


    def writeMagic(self, m):
        if m.values.get('html'):
            for x in m.children:
                self.write(x)

    def writeCaption(self, obj):
        # todo- A table contained a Caption node, causing an exception in write.
        # Not sure what the HTML should be, if any.
        pass
    
    def writeSection(self, obj):
        header = "h%s" % (obj.level)
        self.out.write("<%s>" % header)
        self.write(obj.children[0])
        self.out.write("</%s>" % header)
                
        self.level += 1
        for x in obj.children[1:]:
            self.write(x)
        self.level -= 1

    def writePreFormatted(self, n):
        self.out.write("<pre>")
        for x in n:
            self.write(x)
        self.out.write("</pre>")
        
    def writeNode(self, n):
        for x in n:
            self.write(x)

    def writeCell(self, cell):
        svl = ""
        if cell.vlist:
            svl = self.serializeVList(cell.vlist)
            
        self.out.write('<td %s>' % svl)
        for x in cell:
            self.write(x)
        self.out.write("</td>")

    def writeTagNode(self, t):
        if t.caption == 'ref':
            self.references.append(t)
            self.out.write("<sup>%s</sup>" % len(self.references))
            return
        elif t.caption == 'references':
            if not self.references:
                return

            self.out.write("<ol>")
            for r in self.references:
                self.out.write("<li>")
                for x in r:                    
                    self.write(x)
                self.out.write("</li>")
            self.out.write("</ol>")
                           
            self.references = []            
            return
        elif t.caption=='imagemap':
            # FIXME. this is not complete. t.imagemap.entries should also be handled.
            print "WRITEIMAGEMAP:", t.imagemap
            if t.imagemap.imagelink:
                self.write(t.imagemap.imagelink)
            return

        
        self.out.write(t.starttext.encode('utf8'))
        for x in t:
            self.write(x)
        self.out.write(t.endtext.encode('utf8'))
            
    def writeRow(self, row):
        self.out.write('<tr>')
        for x in row:
            self.write(x)
            
        self.out.write('</tr>')

    def writeTable(self, t):           
        svl = ""
        if t.vlist:
            svl = self.serializeVList(t.vlist)

        
            
        self.out.write("<table %s>" % svl)
        if t.caption:
            self.out.write("<caption>")
            self.write(t.caption)
            self.out.write("<caption>")
        for x in t:
            self.write(x)
        self.out.write("</table>")

    def writeMath(self, obj):
        latex = obj.caption
        p = self.math_renderer.render(latex)
        self.out.write(p)

    def writeURL(self, obj):
        self.out.write('<a href="%s" class="offsite" ttid="externallink">' % obj.caption)
        if obj.children:
            for x in obj.children:
                self.write(x)
        else:
            self.out.write(obj.caption)
            
        self.out.write('&nbsp;<img src="/static/outgoing_link.gif" /></a>')

    def writeNamedURL(self, obj):
        self.out.write('<a href="%s" class="offsite" ttid="externallink">' % obj.caption)
        if obj.children:
            for x in obj.children:
                self.write(x)
        else:
            name = "[%s]" % self.namedLinkCount
            self.namedLinkCount += 1
            self.out.write(name)
                        
        self.out.write('&nbsp;<img src="/static/outgoing_link.gif" /></a>')

        
    def writeParagraph(self, obj):
        self.out.write("\n<p>")
        for x in obj:
            self.write(x)
        self.out.write("</p>\n")

    def getHREF(self, obj):
        parts = obj.target.encode('utf-8').split('#')
        parts[0] = parts[0].replace(" ", "_")
        

        return '../%s/' % ("#".join([urllib.quote(x) for x in parts]))

    writeLangLink = ignore

    def writeLink(self, obj):
        if obj.target is None:
            return

        href = self.getHREF(obj)
        if href is not None:
            self.out.write('<a href="%s" class="normallink">' % (href,))
        else:
            self.out.write('<a class="deadlink">')
        if obj.children:
            for x in obj.children:
                self.write(x)
        else:
            self._write(obj.target)
            
        self.out.write("</a>")

    def writeSpecialLink(self, obj):
        if obj.children:
            for x in obj.children:
                self.write(x)
        else:
            self._write(obj.target)

    def writeCategoryLink(self, obj):
        if obj.colon:
            if obj.children:
                for x in obj.children:
                    self.write(x)
            else:
                self._write(obj.target)

    def writeTimeline(self, obj):
        #img = timeline.drawTimeline(obj.caption)
        img = None
        if img is None:
            return
        
        target = "/timeline/"+os.path.basename(img)
        width, height = Image.open(img).size
        
        self.out.write('<img src="%s" width="%s" height="%s" />' % (target, width, height))
        
    def writeImageLink(self, obj):
        """
        <span class='image'>
          <span class='left'>
            <img src='bla' />
            <span class='imagecaption'>bla bla</span>
          <span/>
        <span/>
        """
        
        if self.images is None:
            return

        width = obj.width
        height = obj.height

        #if not width:
        #    width = 400  # what could be a sensible default if no width is given? maybe better 0?

        if width:
            path = self.images.getPath(obj.target, size=max(width, height))
            url = self.images.getURL(obj.target, size=max(width, height))
        else:
            path = self.images.getPath(obj.target)
            url = self.images.getURL(obj.target)
            
        if url is None:
            return

        if isinstance(path, str):
            path = unicode(path, 'utf8')

        if self.imglevel==0:
            self.imglevel += 1

            # WTB: Added the ability to not specify width & height since images may not be found locally.
            # This may have to be redone eventually, perhaps we need a database of image dimensions,
            # but I doubt it.  Besides, more hardcoded pathnames in 'getimg'?
            try:
                def getimg():
                    return Image.open(path)
                img = None
                
                if not width:
                    if not img:
                        img = getimg()
                    size = img.size
                    width = min(400, size[0])

                if not height:
                    if not img:
                        img = getimg()
                    size = img.size
                    height = size[1]*width/size[0]
            except IOError, err:
                log.warn("Image.open failed:", err, "path=", repr(path))
                # WTB: Removed following return as images will not always be found locally.
                #self.imglevel -= 1
                #return

            attr = ''
            attr_css = ''
            
            if width:
                attr += "width='%d' " % width
                attr_css += "width:%dpx " % width
                
            if height:
                attr += "height='%d' " % height
                # WTB: Note- height not applied to CSS.

            if obj.isInline():
                self.out.write('<img src="%s" %s/>' % (url.encode("utf8"), attr.encode("utf8")))
            else:
                # WTB: This looked like a mistake to me, it was modifying obj.align instead of align.
                # This function should not modify obj at all.
                align = obj.align
                if obj.thumb == True and not align:
                    align = "clear right"
                self.out.write('''<div  class="bbotstyle image %s" style="%s">'''% (align, attr_css))
                self.out.write('<img src="%s" %s/>' % (url.encode("utf8"), attr.encode("utf8")))
                
                self.out.write('<span class="imagecaption">')
                for x in obj.children:
                    self.write(x)
                self.out.write('</span></div>')
            self.imglevel -= 1
        else:
            self.out.write('<a href="%s">' % url)
            for x in obj.children:
                self.write(x)
            self.out.write('</a>')

    def writeText(self, t):
        #self.out.write(cgi.escape(t.caption).encode('ascii', 'xmlcharrefreplace'))
        self._write(t.caption)
        
    writeControl = writeText

    def writeArticle(self, a):
            
        for x in a:
            self.write(x)

        self.out.write("\n<br/>")
        
    def writeStyle(self, s):
        if s.caption == "''": 
            tag = 'em'
        elif s.caption=="'''''":
            self.out.write("<strong><em>")
            for x in s:
                self.write(x)
            self.out.write("</em></strong>")
            return
        elif s.caption == "'''":
            tag = 'strong'
        elif s.caption == ";":
            self.out.write("<div><strong>")
            for x in s:
                self.write(x)
            self.out.write("</strong></div>")
            return
        
        elif s.caption.startswith(":"):
            self.out.write("<blockquote>"*len(s.caption))
            for x in s:
                self.write(x)
            self.out.write("</blockquote>"*len(s.caption))
            return
        elif s.caption == "overline":
            self.out.write('<u style="text-decoration: overline;">')
            for x in s:
                self.write(x)
            self.out.write('</u>')
            return
        else:
            tag = s.caption
    

        self.out.write("<%s>" % tag)
        for x in s:
            self.write(x)
        self.out.write("</%s>" % tag)

    def writeItem(self, item):
        self.out.write("<li>")
        for x in item:
            self.write(x)
        self.out.write("</li>\n")

    def writeItemList(self, lst):
        if lst.numbered:
            tag = "ol"
        else:
            tag = "ul"
            
        self.out.write("<%s>" % tag)
            
        for x in lst:
            self.write(x)
            self.out.write("\n")

        self.out.write("</%s>" % tag)


class NoLinksWriter(HTMLWriter):
    """Subclass that ignores (non-outgoing) links"""
    
    def writeLink(self, obj):
        if obj.target is None:
            return

        if obj.children:
            for x in obj.children:
                self.write(x)
        else:
            self._write(obj.target)