#!/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 .
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 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 "
"
else:
return ""
def cpara(self):
state = self.state.pop();
self.state.append(self.ST_INIT)
if state == self.ST_OPEN:
return "
"
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 "" % \
(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 "%s" % (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 "%s" % (target,
text if (text and text != '') \
else target)
def str_it(self, tok, env):
return "" + self.fmtok(tok[1], env) + ""
def str_bold(self, tok, env):
return "" + self.fmtok(tok[1], env) + ""
def str_hdr(self, tok, env):
level = tok[1]
if level > 4:
level = 4
return "%s" % (level, self.fmtok(tok[2], env), level)
def str_bar(self, tok, env):
return "
"
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 " " + arg + ""
else:
return "
" + arg + ":
"