summaryrefslogtreecommitdiffabout
path: root/wikitrans/wiki2html.py
Side-by-side diff
Diffstat (limited to 'wikitrans/wiki2html.py') (more/less context) (ignore whitespace changes)
-rw-r--r--wikitrans/wiki2html.py53
1 files changed, 33 insertions, 20 deletions
diff --git a/wikitrans/wiki2html.py b/wikitrans/wiki2html.py
index fc6b142..0696dce 100644
--- a/wikitrans/wiki2html.py
+++ b/wikitrans/wiki2html.py
@@ -1,17 +1,17 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2018 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/>.
@@ -40,16 +40,18 @@ try:
from html import escape as html_escape
except ImportError:
from cgi import escape as html_escape
-
+
__all__ = [ "HtmlWikiMarkup", "HtmlWiktionaryMarkup" ]
+
class HtmlSeqNode(WikiSeqNode):
def format(self):
s = ''
for x in self.content:
s += x.format()
return s
-
+
+
class HtmlLinkNode(HtmlSeqNode):
def format(self):
arg = self.content[0].format()
@@ -70,8 +72,8 @@ class HtmlLinkNode(HtmlSeqNode):
elif s[0] == "proto":
text = self.parser.tmpl_proto(s)
return text
-
- (qual,sep,tgt) = arg.partition(':')
+
+ (qual, sep, tgt) = arg.partition(':')
if tgt != '':
ns = self.parser.wiki_ns_name(qual)
if ns:
@@ -92,6 +94,7 @@ class HtmlLinkNode(HtmlSeqNode):
return "<a href=\"%s\">%s</a>" % (tgt,
text if (text and text != '') else arg)
+
class HtmlRefNode(WikiRefNode):
def format(self):
target = self.ref
@@ -101,6 +104,7 @@ class HtmlRefNode(WikiRefNode):
text if (text and text != '') else target
)
+
class HtmlFontNode(HtmlSeqNode):
def format(self):
comm = { 'IT': 'i',
@@ -111,14 +115,16 @@ class HtmlFontNode(HtmlSeqNode):
s += '</%s>' % comm[self.type]
return s
+
class HtmlTextNode(HtmlSeqNode):
def format(self):
- if isinstance(self.content,list):
+ if isinstance(self.content, list):
s = ''.join(self.content)
else:
s = html_escape(self.content, quote=False)
return s
+
class HtmlHdrNode(WikiHdrNode):
def format(self):
level = self.level
@@ -126,10 +132,12 @@ class HtmlHdrNode(WikiHdrNode):
level = 6
return "<h%s>%s</h%s>\n\n" % (level, self.content.format(), level)
+
class HtmlBarNode(WikiNode):
def format(self):
return "<hr/>\n"
+
class HtmlEnvNode(WikiEnvNode):
def format(self):
type = self.envtype
@@ -146,7 +154,8 @@ class HtmlEnvNode(WikiEnvNode):
string,
self.parser.envt[type]["hdr"])
return string
-
+
+
class HtmlTagNode(WikiTagNode):
def format(self):
if self.tag == 'code':
@@ -156,13 +165,13 @@ class HtmlTagNode(WikiTagNode):
return '<pre><code>' + s + '</code></pre>' #FIXME
elif self.tag == 'ref':
n = self.idx+1
- return '<sup id="cite_ref-%d" class="reference"><a name="cite_ref-%d" href=#cite_note-%d">%d</a></sup>' % (n,n,n,n)
+ return '<sup id="cite_ref-%d" class="reference"><a name="cite_ref-%d" href=#cite_note-%d">%d</a></sup>' % (n, n, n, n)
elif self.tag == 'references':
s = '<div class="references">\n'
s += '<ol class="references">\n'
n = 0
for ref in self.parser.references:
- n += 1
+ n += 1
s += ('<li id="cite_note-%d">'
+ '<span class="mw-cite-backlink">'
+ '<b><a href="#cite_ref-%d">^</a></b>'
@@ -170,7 +179,7 @@ class HtmlTagNode(WikiTagNode):
+ '<span class="reference-text">'
+ ref.content.format()
+ '</span>'
- + '</li>\n') % (n,n)
+ + '</li>\n') % (n, n)
s += '</ol>\n</div>\n'
return s
else:
@@ -180,11 +189,13 @@ class HtmlTagNode(WikiTagNode):
s += '>'
s += self.content.format()
return s + '</' + self.tag + '>'
-
+
+
class HtmlParaNode(HtmlSeqNode):
def format(self):
return "<p>" + super(HtmlParaNode, self).format() + "</p>\n"
+
class HtmlPreNode(HtmlSeqNode):
def format(self):
s = super(HtmlPreNode, self).format()
@@ -193,10 +204,11 @@ class HtmlPreNode(HtmlSeqNode):
else:
return '<pre>' + s + '</pre>'
+
class HtmlIndNode(WikiIndNode):
def format(self):
return ("<dl><dd>" * self.level) + self.content.format() + "</dd></dl>" * self.level
-
+
class HtmlWikiMarkup(WikiMarkup):
"""A Wiki markup to HTML translator class.
@@ -223,9 +235,9 @@ class HtmlWikiMarkup(WikiMarkup):
[html_base=URL],[image_base=URL],[media_base=URL])
The arguments have the same meaning as in the WikiMarkup constructor.
-
+
"""
-
+
super(HtmlWikiMarkup, self).__init__(*args, **kwargs)
self.token_class['LINK'] = HtmlLinkNode
self.token_class['TMPL'] = HtmlLinkNode
@@ -249,14 +261,14 @@ class HtmlWikiMarkup(WikiMarkup):
for elt in wiki_ns_re[self.lang][str]:
if str.beginswith(elt[0]) and str.endswith(elt[1]):
return elt[2]
- return None
-
+ return None
+
envt = { "unnumbered": { "hdr": "ul",
"elt": ["li"] },
"numbered": { "hdr": "ol",
"elt": ["li"] },
"defn": { "hdr": "dl",
- "elt": ["dt","dd"] } }
+ "elt": ["dt","dd"] } }
def mktgt(self, tgt, lang = None):
if not lang:
@@ -291,13 +303,14 @@ class HtmlWikiMarkup(WikiMarkup):
text += ' <span class="proto">' + x + '</span>'
text += ' <span class="meaning">(' + s[-2] + ')</span>'
return text
-
+
def __str__(self):
str = ""
for elt in self.tree:
str += elt.format()
return str
+
class HtmlWiktionaryMarkup(HtmlWikiMarkup):
"""A class for translating Wiktionary articles into HTML.

Return to:

Send suggestions and report system problems to the System administrator.