summaryrefslogtreecommitdiffabout
path: root/wiki2text.py
authorSergey Poznyakoff <gray@gnu.org.ua>2008-11-29 13:16:12 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2008-11-29 13:16:12 (GMT)
commite3710f6c32aa9a9e6b737c4ebc64af0df2ea872b (patch) (side-by-side diff)
tree441aa7476fd40685be4d8287bfa90d8a169a4b6c /wiki2text.py
parent88befee77fdaacc2e0dc92beffd1c6a68f86a8b7 (diff)
downloadwikitrans-e3710f6c32aa9a9e6b737c4ebc64af0df2ea872b.tar.gz
wikitrans-e3710f6c32aa9a9e6b737c4ebc64af0df2ea872b.tar.bz2
Improve text output generation
* wiki2html.py (mktgt): Add 3rd argument. All callers updated. * wiki2text.py (wiki_ns_name,mktgt): New functions (str_link,str_tmpl): Rewrite * wikicvt.py: New argument --input-text (not used yet)
Diffstat (limited to 'wiki2text.py') (more/less context) (ignore whitespace changes)
-rw-r--r--wiki2text.py92
1 files changed, 52 insertions, 40 deletions
diff --git a/wiki2text.py b/wiki2text.py
index 51a7853..564809f 100644
--- a/wiki2text.py
+++ b/wiki2text.py
@@ -17,6 +17,7 @@
from wikimarkup import *
from types import TupleType
+from wikins import wiki_ns_re, wiki_ns
import urllib
class TextWikiMarkup (WikiMarkup):
@@ -42,23 +43,6 @@ class TextWikiMarkup (WikiMarkup):
self.references = keywords['refs']
if 'markup' in keywords:
self.markup = keywords['markup']
-
- def target(self, t):
- (qual,sep,tgt) = t.partition(':')
- r = None
- if tgt != '':
- if qual == "Image":
- t = self.image_base + '/' + 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
def xref(self, text, target):
if text:
@@ -66,30 +50,55 @@ class TextWikiMarkup (WikiMarkup):
else:
return "see " + target
- def str_link(self, tok, env):
+ 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
+ def mktgt(self, tgt, lang = None):
+ if not lang:
+ lang = self.lang
+ return self.html_base % { 'lang' : lang } + urllib.quote(tgt)
+
+ def link(self, tok, env, istmpl):
arg = self.fmtok(tok[1], env)
text = self.fmtok(tok[2], env)
- if self.references:
- (target, r) = self.target(arg)
- return self.xref(text if text else r, target)
+ (qual,sep,tgt) = arg.partition(':')
+ if tgt != '':
+ ns = self.wiki_ns_name(qual)
+ if ns:
+ if ns == 'NS_IMAGE':
+ text = "[%s: %s]" % (qual, text if text else arg)
+ tgt = self.image_base + '/' + \
+ urllib.quote(tgt) + \
+ '/250px-' + urllib.quote(tgt)
+ elif ns == 'NS_MEDIA':
+ text = "[%s]" % (qual)
+ else:
+ tgt = self.mktgt(tgt)
+ elif not istmpl and qual in self.langtab:
+ text = self.langtab[qual] + ": " + tgt
+ tgt = self.mktgt(tgt, qual)
+ else:
+ tgt = self.mktgt(tgt)
else:
- (qual,sep,tgt) = arg.partition(':')
- if sep != '':
- return ""
- elif text:
- return text
+ tgt = self.mktgt(arg)
+ if self.references:
+ return "%s (see %s) " % (text, tgt)
+ elif not text or text == '':
return arg
+ else:
+ return text
+
+ def str_link(self, tok, env):
+ return self.link(tok, env, False)
def str_tmpl(self, tok, env):
- arg = self.fmtok(tok[1], env)
- (target, r) = self.target(arg)
- text = self.fmtok(tok[2], env)
- if not text and r:
- text = r
- if self.references:
- return self.xref(text, target)
- return text
-
+ return self.link(tok, env, True)
+
def str_ref(self, tok, env):
return self.xref(self.fmtok(tok[2], env), self.fmtok(tok[1], env))
@@ -165,14 +174,17 @@ class TextWiktionaryMarkup (TextWikiMarkup):
s = ""
self.seq_pos=0
for t in tok[1:]:
- s += self.fmtok(t, env)
+ x = self.fmtok(t, env)
+ if x:
+ s += x
self.seq_pos += 1
return s
def str_tmpl(self, tok, env):
arg = self.fmtok(tok[1], env)
- if self.seq_pos > 0:
- return arg
- else:
- return "\n" + arg + ":\n"
+ if arg and arg != '':
+ if self.seq_pos > 0:
+ return arg
+ else:
+ return "\n" + arg + ":\n"

Return to:

Send suggestions and report system problems to the System administrator.