summaryrefslogtreecommitdiffabout
path: root/wiki2html.py
blob: 907e3b16274ccaba1eef53f3438b11e5cb60a5d0 (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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008 Sergey Poznyakoff
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from wikimarkup import *
from types import TupleType
import urllib

class HtmlWikiMarkup (WikiMarkup):
    """
    A (hopefully) general-purpose Wiki->HTML translator class.
    FIXME: 1. See WikiMarkup for a list
           2. [[official position]]s : final 's' gets after closing </a> tag.
           Should be before.
    """
        
    def target(self, t):
        (qual,sep,tgt) = t.partition(':')
        r = None
        if tgt != '':
            if qual in ('Image', 'Grafika'):
                t = self.image_base + urllib.quote(tgt) + '/250px-' + urllib.quote(tgt)
            elif qual == "Media":
                t = self.media_base + '/' + tgt
            elif qual in self.langtab:
                t = self.html_base % { 'lang' : qual } + '/' + urllib.quote(tgt)
                r = self.langtab[qual]
            else:
                t = self.html_base % { 'lang' : self.lang } + '/' + urllib.quote(t)
        else:
            t = self.html_base % { 'lang' : self.lang } + '/' + urllib.quote(t)
        return t, r

    envhdr = [ "ul", "ol", "dl" ]    
    envel = [ "li", "li", "dd" ]

    def str_link(self, tok, env):
        arg = self.fmtok(tok[1], env)
        (target, r) = self.target(arg)
        text = self.fmtok(tok[2], env)
        return "<a href=\"%s\">%s</a>" % (target,
                                          text if (text and text != '') \
                                               else \
                                              r if r else arg)

    def str_tmpl(self, tok, env):
        arg = self.fmtok(tok[1], env)
        (target, r) = self.target(arg)
        text = self.fmtok(tok[2], env)
        return "<a href=\"%s\">%s</a>" % (target,
                                          text if (text and text != '') \
                                               else arg)

    def str_ref(self, tok, env):
        target = self.fmtok(tok[1], env)
        text = self.fmtok(tok[2], env)
        return "<a href=\"%s\">%s</a>" % (target,
                                          text if (text and text != '') \
                                                   else target)
    def str_it(self, tok, env):
        return "<i>" + self.fmtok(tok[1], env) + "</i>"
                                          
    def str_bold(self, tok, env):
        return "<b>" + self.fmtok(tok[1], env) + "</b>"
                                              
    def str_hdr(self, tok, env):
        level = tok[1]
        if level > 4:
            level = 4
        return "<h%s>%s</h%s>" % (level, self.fmtok(tok[2], env), level)
    
    def str_bar(self, tok, env):
        return "<hr/>"
    
    def str_env(self, tok, env):
        t = tok[1]
        return "<" + self.envhdr[t] + ">" + \
            self.fmtok(tok[3], tok) + \
            "</" + self.envhdr[t] + ">"
    
    def str_item(self, tok, env):
        return "<%s>%s</%s>" % (self.envel[env[1]],
                        self.fmtok(tok[1], env),
                        self.envel[env[1]])

    def str_seq(self, tok, env):
            s = ""
            for t in tok[1:]:
                s += self.fmtok(t, env)
            return s



class HtmlWiktionaryMarkup (HtmlWikiMarkup):
    """
 A class for translating Wiktionary articles into HTML.
 This version does not do much, except that it tries to correctly
 format templates. But "tries" does not mean "does". The heuristics
 used here is clearly not enogh to cope with it.

 1. FIXME:    
 The right solution would be to have a database of templates with their
 semantics and to decide on their rendering depending on that. E.g.
 {{term}} in en.wiktionary means "replace this with the search term".
 This, however, does not work in other wiktionaries. There are
 also more complex templates, e.g.: {{t+|bg|врата|n|p|tr=vrata|sc=Cyrl}}
 I don't know what it means. Couldn't find any documentation either.
 Again, this template does not work in other dictionaries.

 2. Capitulation notice:    
 Given the:
   1. waste amount of wiktionaries available,
   2. abundance of various templates for each wictionary,
   3. apparent lack of documentation thereof,
   4. the lack of standardized language-independent templates,
 I dont see any way to cope with the template-rendering task within a
 reasonable amount of time.
 
 Faeci quod potui, faciant meliora potentes.    
    """
    seq_pos = 0
    
    def str_seq(self, tok, env):
            s = ""
            self.seq_pos=0
            for t in tok[1:]:
                s += self.fmtok(t, env)
                self.seq_pos += 1
            return s
    
    def str_tmpl(self, tok, env):
        arg = self.fmtok(tok[1], env)
        if self.seq_pos > 0:
            return " <b>" + arg + "</b>"
        else:
            return "<br/><b>" + arg + ":</b><br/>"
                

Return to:

Send suggestions and report system problems to the System administrator.