summaryrefslogtreecommitdiffabout
path: root/wiki2html.py
blob: 4e0e78a93418309cb380684e4d4ac048f685e0c3 (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
#!/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
from wikins import wiki_ns_re, wiki_ns
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.
    """

    ST_INIT = 0
    ST_PARA = 1
    ST_OPEN = 2
    
    state = []

    def opara(self):
        if self.state[-1] == self.ST_PARA:
            self.state[-1] = self.ST_OPEN
            return "<p>"
        else:
            return ""
        
    def cpara(self):
        state = self.state.pop();
        self.state.append(self.ST_INIT)
        if state == self.ST_OPEN:
            return "</p>"
        else:
            return ""
    

    def wiki_ns_name(self, str):
        if str in wiki_ns[self.lang]:
            return wiki_ns[self.lang][str]
        elif str in wiki_ns_re[self.lang]:
            for elt in wiki_ns_re[self.lang][str]:
                if str.beginswith(elt[0]) and str.endswith(elt[1]):
                    return elt[2]
        return None            
        

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

    def mktgt(self, tgt):
        return self.html_base % { 'lang' : self.lang } + '/' + urllib.quote(tgt)
    
    def link(self, tok, env, istmpl):
        arg = self.fmtok(tok[1], env)
        text = self.fmtok(tok[2], env)
        (qual,sep,tgt) = arg.partition(':')
        if tgt != '':
            ns = self.wiki_ns_name(qual)
            if ns:
                if ns == 'NS_IMAGE':
                    return "<img src=\"%s\" alt=\"[ %s ]\" />" % \
                            (self.image_base + '/' + \
                                 urllib.quote(tgt) + \
                                 '/250px-' + urllib.quote(tgt),
                             text if text else arg)
                elif ns == 'NS_MEDIA':
                    tgt = self.media_base + '/' + tgt
                else:
                    tgt = self.mktgt(tgt)
            elif not istmpl and qual in self.langtab:
                tgt = self.html_base % { 'lang' : qual } + \
                      '/' + urllib.quote(tgt)
                if not text or text == '':
                    text = self.langtab[qual]
            else:
                tgt = self.mktgt(tgt)
        else:
            tgt = self.mktgt(arg)
        return "<a href=\"%s\">%s</a>" % (tgt,
                                          text if (text and text != '') \
                                               else arg)
                
        
    def str_link(self, tok, env):
        return self.link(tok, env, False)

    def str_tmpl(self, tok, env):
        return self.link(tok, env, True)

    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_para(self, tok, env):
        s = self.cpara()
        self.state.append(self.ST_PARA)
        return s

    def fmtok(self, tok, env):
        if type(tok) != TupleType:
            return ""
        if tok[0] in [ self.ENV, self.HDR ]:
            s = self.cpara()
        elif tok[0] == self.BAR:
            s = self.str_para(tok, env)
        elif tok[0] in [ self.NIL, self.SEQ ]:
            s = ""
        else:
            s = self.opara()
        s1 = WikiMarkup.fmtok(self, tok, env)
        if s1:
            s += s1
        return s
    
    def __str__(self):
        self.state = [ self.ST_PARA ]
        return WikiMarkup.__str__(self) + self.cpara()



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 enough 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):
            str = ""
            self.seq_pos=0
            for t in tok[1:]:
                s = self.fmtok(t, env)
                if s:
                    str += s
                    self.seq_pos += 1
            return str
    
    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.