diff options
-rw-r--r-- | wiki2html.py | 10 | ||||
-rw-r--r-- | wiki2texi.py | 9 | ||||
-rw-r--r-- | wiki2text.py | 3 | ||||
-rwxr-xr-x | wikicvt.py | 22 | ||||
-rw-r--r-- | wikimarkup.py | 5 |
5 files changed, 27 insertions, 22 deletions
diff --git a/wiki2html.py b/wiki2html.py index 0330b92..05d4642 100644 --- a/wiki2html.py +++ b/wiki2html.py @@ -7,28 +7,30 @@ # 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 re -import urllib +try: + from urllib import quote as url_quote +except ImportError: + from urllib.parse import quote as url_quote 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 wiki_ns_name(self, str): if str in wiki_ns[self.lang]: return wiki_ns[self.lang][str] @@ -39,25 +41,25 @@ class HtmlWikiMarkup (WikiMarkup): return None envt = { "unnumbered": { "hdr": "ul", "elt": ["li"] }, "numbered": { "hdr": "ol", "elt": ["li"] }, "defn": { "hdr": "dl", "elt": ["dt","dd"] } } def mktgt(self, tgt, lang = None): if not lang: lang = self.lang - return self.html_base % { 'lang' : lang } + urllib.quote(tgt) + return self.html_base % { 'lang' : lang } + url_quote(tgt) def tmpl_term(self, s): if len(s) == 2: return s[1] text = None trans = None for x in s[1:]: m = re.match('(\w+)=', x) if m: if m.group(1) == "tr": trans = x[m.end(1)+1:] elif not text: @@ -75,25 +77,25 @@ class HtmlWikiMarkup (WikiMarkup): if n > 0: text += ',' n += 1 text += ' <span class="proto">' + x + '</span>' text += ' <span class="meaning">(' + s[-2] + ')</span>' return text def fmtlink(self, elt, istmpl): arg = self.format(elt['content'][0]) text = None if len(elt['content']) > 1: - s = map(self.format, elt['content']) + s = [x for x in map(self.format, elt['content'])] if s[0] == 'disambigR' or s[0] == 'wikiquote': return "" elif len(s) > 1 and s[1] == 'thumb': return "" text = '<span class="template">' + s[1] + '</span>' if istmpl: if re.match("t[+-]$", s[0]): if len(s) > 2: text = s[2] elif s[0] == "term": text = self.tmpl_term(s) elif s[0] == "proto": diff --git a/wiki2texi.py b/wiki2texi.py index a7b5e92..6e32c56 100644 --- a/wiki2texi.py +++ b/wiki2texi.py @@ -7,25 +7,24 @@ # 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 re import urllib class TexiWikiMarkup (WikiMarkup): sectcomm = { 'numbered': [ '@top', '@chapter', '@section', '@subsection', '@subsubsection' @@ -49,31 +48,31 @@ class TexiWikiMarkup (WikiMarkup): '@chapheading', '@heading', '@subheading', '@subsubheading' ] } sectioning_model = 'numbered' sectioning_start = 0 def __init__(self, *args, **keywords): super(TexiWikiMarkup, self).__init__(*args, **keywords) - if keywords.has_key("sectioning-model"): + if "sectioning-model" in keywords: val = keywords["sectioning-model"] - if self.sectcomm.has_key(val): + if val in self.sectcomm: self.sectioning_model = val else: raise ValueError("Invalid value for sectioning model: %s" % val) - if keywords.has_key("sectioning-start"): + if "sectioning-start" in keywords: val = keywords["sectioning-start"] if val < 0 or val > 4: raise ValueError("Invalid value for sectioning start: %s" % val) else: self.sectioning_start = val def __str__(self): str = "" for elt in self.tree: str += self.format(elt) return str @@ -208,25 +207,25 @@ class TexiWikiMarkup (WikiMarkup): for s in elt['content']: if s['subtype'] == 0: string += "@item " + self.format(s['content']) + '\n' else: string += self.format(s['content']) + '\n' string += '@end table\n' return string def str_link(self, elt): # FIXME: A very crude version arg = self.format(elt['content'][0]) if len(elt['content']) > 1: - s = map(self.format, elt['content']) + s = [x for x in map(self.format, elt['content'])] text = s[1] else: s = None text = None if s: if s[0] == 'disambigR' or s[0] == 'wikiquote': return "" if len(s) > 1 and s[1] == 'thumb': return "" (qual,sep,tgt) = arg.partition(':') diff --git a/wiki2text.py b/wiki2text.py index 6308da1..916391e 100644 --- a/wiki2text.py +++ b/wiki2text.py @@ -7,25 +7,24 @@ # 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 re import urllib class TextWikiMarkup (WikiMarkup): """ A (general-purpose Wiki->Text translator class. """ # Output width width = 78 # Do not show references. @@ -59,25 +58,25 @@ class TextWikiMarkup (WikiMarkup): 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 fmtlink(self, elt, istmpl): arg = self.format(elt['content'][0]) if len(elt['content']) > 1: - s = map(self.format, elt['content']) + s = [x for x in map(self.format, elt['content'])] text = s[1] else: s = None text = None if s: if s[0] == 'disambigR' or s[0] == 'wikiquote': return "" if len(s) > 1 and s[1] == 'thumb': return "" (qual,sep,tgt) = arg.partition(':') if tgt != '': @@ -6,45 +6,49 @@ # 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 __future__ import print_function import sys import getopt -import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO from wiki2html import * from wiki2text import * from wiki2texi import * class DumpWikiMarkup (WikiMarkup): def __str__(self): if self.tree: - s = StringIO.StringIO() + s = StringIO() self.dump(self.tree, 0, s) return s.getvalue() else: return "" def usage(code=0): - print """ -usage: %s [-hvt] [-I INTYPE] [-l lang] [-o kw=val] [--lang=lang] [--option kw=val] - [--input-type=INTYPE] [--type=OUTTYPE] [--help] [--verbose] file -""" % (sys.argv[0]) + print("""usage: %s [-hvt] [-I INTYPE] [-l lang] [-o kw=val] [--lang=lang] + [--option kw=val] [--input-type=INTYPE] [--type=OUTTYPE] [--help] + [--verbose] file +""" % sys.argv[0]) sys.exit(code) handlers = { 'dump': { 'default': DumpWikiMarkup }, 'html': { 'default': HtmlWikiMarkup, 'wiktionary': HtmlWiktionaryMarkup }, 'text': { 'default': TextWikiMarkup, @@ -100,22 +104,22 @@ def main(): else: kwdict['filename'] = args[0] else: usage(1) kwdict['lang']=lang if otype in handlers: if itype in handlers[otype]: markup = handlers[otype][itype](**kwdict) markup.debug_level = debug markup.parse() - print str(markup) + print("%s" % str(markup)) exit(0) else: - print "unsupported input type: %s" % (itype) + print("unsupported input type: %s" % itype) else: - print "unsupported output type: %s" % (otype) + print("unsupported output type: %s" % otype) exit(1) if __name__ == '__main__': main() diff --git a/wikimarkup.py b/wikimarkup.py index adaa1a2..2ef6be1 100644 --- a/wikimarkup.py +++ b/wikimarkup.py @@ -6,24 +6,25 @@ # 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 __future__ import print_function import sys import re from types import * __all__ = [ "BaseWikiMarkup", "WikiMarkup", "TagAttributes", "TagAttributeSyntax" ] class TagAttributeSyntax(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) @@ -106,25 +107,25 @@ class BaseWikiMarkup(object): toklist = None tokind = 0 newline = 0 tree = None tags = [ 'code', 'nowiki', 'tt', 'div' ] nested = 0 debug_level = 0 def dprint(self, lev, fmt, *argv): if self.debug_level >= lev: - print "[DEBUG]", fmt % argv + print("[DEBUG]", fmt % argv) def print_dump_prefix(self, level, file): file.write("[DUMP]" + ' ' * (2*level + 1)) def dump_nil(self, node, level, file): pass def dump_text(self, node, level, file): self.print_dump_prefix(level, file) file.write("CONTENT: \"%s\"\n" % node['content']) def dump_delim(self, node, level, file): @@ -984,25 +985,25 @@ class WikiMarkup (BaseWikiMarkup): "fj": "Na Vosa Vakaviti",# Fijian; Fiji "fo": "Føroyskt" , # Faroese "fr": "Français" , # French "frp": "Arpitan", "fur": "Furlan", "fy": "Frysk", # Frisian "ga": "Gaeilge", # Irish "gan": "贛語 (Gànyŭ)", "gd": "Gàidhlig", # Scots; Gaelic "gl": "Gallego" , # Gallegan; Galician "glk": "گیلکی", - "got": "𐌲𐌹𐌺 ", + "got": "𐌲𐌿𐍄𐌹𐍃𐌺𐍉𐍂𐌰𐌶𐌳𐌰", "gn": "Avañe'ẽ", # Guarani "g": "ગુજરાતી", # Gujarati "gv": "Gaelg", # Manx "ha": "هَوُسَ", # Hausa "hak": "Hak-kâ-fa / 客家話", "haw": "Hawai`i", "he": "עברית" , # Hebrew (formerly iw) "hi": "हिन्दी" , # Hindi "hif": "Fiji Hindi", "ho": "Hiri Mot", # Hiri Motu "hr": "Hrvatski" , # Croatian |