summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-09-01 19:10:01 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-09-01 19:24:02 (GMT)
commit5320bea15e388200e613e6a2bdac3c1449030986 (patch) (side-by-side diff)
tree7d8571c63f72cc690cea8323e43be09c18527c45
parent0aae19835045bac0be0f22ecd0e84527cdaee21c (diff)
downloadwikitrans-5320bea15e388200e613e6a2bdac3c1449030986.tar.gz
wikitrans-5320bea15e388200e613e6a2bdac3c1449030986.tar.bz2
Bugfixes
* README.rst: Describe new options. * bin/wikitrans: Change handling of the --debug option. * wikitrans/wikimarkup.py (WikiMarkupParser): New attribute - strict. (parse_para): Don't throw UnexpectedTokenError if self.strict is False, instead ignore invalid token. (WikiMarkup): Fix Python 3 compatibility * wikitrans/wikitoken.py: Fix Python 3 compatibility
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--README.rst6
-rwxr-xr-xbin/wikitrans13
-rw-r--r--wikitrans/wikimarkup.py27
-rw-r--r--wikitrans/wikitoken.py2
4 files changed, 42 insertions, 6 deletions
diff --git a/README.rst b/README.rst
index 7838fa1..7edeb04 100644
--- a/README.rst
+++ b/README.rst
@@ -37,6 +37,12 @@ media_base = *url*
Base URL for media files. Default is
``http://www.mediawiki.org/xml/export-0.3``
+debug_level = *int*
+ Debug verbosity level (0 - no debug info, 100 - excessively
+ copious debug messages). Default is 0.
+
+strict = *bool*
+ Strict parsing mode. Throw exceptions on syntax errors. Default is False.
class ``TextWikiMarkup``
------------------------
diff --git a/bin/wikitrans b/bin/wikitrans
index 4a0fc06..09ba0b3 100755
--- a/bin/wikitrans
+++ b/bin/wikitrans
@@ -71,6 +71,11 @@ def setkw(option, opt, value, parser):
if val:
parser.values.kwdict[kw] = val
+def setdebug(option, opt, value, parser):
+ if not parser.values.kwdict:
+ parser.values.kwdict = {}
+ parser.values.kwdict['debug_level'] = value
+
def getwiki(url, options):
tmp = tempfile.NamedTemporaryFile()
if sys.version_info[0] > 2:
@@ -135,8 +140,8 @@ Otherwise, ARG is name of the file to read wiki material from.
default={},
help='set keyword option for the parser class constructor')
parser.add_option('-d', '--debug',
- action='store', type='int', dest='debug',
- default=0,
+ action='callback', callback=setdebug,
+ type='int', dest='kwdict',
help='set debug level (0..100)')
parser.add_option('-D', '--dump',
action='store_const', const='dump',
@@ -150,7 +155,8 @@ Otherwise, ARG is name of the file to read wiki material from.
(options, args) = parser.parse_args()
if len(args) == 1:
if options.base_url:
- getwiki(options.base_url + '/wiki/Special:Export/' + args[0], options)
+ getwiki(options.base_url + '/wiki/Special:Export/' + args[0],
+ options)
elif args[0] == '-':
options.kwdict['file'] = sys.stdin
elif re.match('^(http|ftp)s?://',args[0]):
@@ -167,7 +173,6 @@ Otherwise, ARG is name of the file to read wiki material from.
if options.otype in handlers:
if options.itype in handlers[options.otype]:
markup = handlers[options.otype][options.itype](**options.kwdict)
- markup.debug_level = options.debug
markup.parse()
print("%s" % str(markup))
exit(0)
diff --git a/wikitrans/wikimarkup.py b/wikitrans/wikimarkup.py
index 19f69e6..1e2429f 100644
--- a/wikitrans/wikimarkup.py
+++ b/wikitrans/wikimarkup.py
@@ -145,6 +145,13 @@ class WikiMarkupParser(object):
Public attributes:
+ Input:
+ debug_level -- debug verbosity level (0 - no debug info, 100 - excessively
+ copious debug messages). Default is 0.
+ strict -- if True, parser will throw exception upon encountering
+ invalid markup tag (mostly for future use)
+
+ Output:
tree -- constructed parse tree (a subclass of WikiNode)
"""
@@ -175,6 +182,7 @@ class WikiMarkupParser(object):
tags = [ 'code', 'nowiki', 'tt', 'div', 'ref', 'references' ]
debug_level = 0
+ strict = False
def dprint(self, lev, fmt, *argv):
"""If current debug level is greater than or equal to lev, print *argv
@@ -374,7 +382,7 @@ class WikiMarkupParser(object):
stack.append(i)
# Redefine all non-matched tokens as TEXT
for i in stack:
- # FIXME
+ # FIXME: How to convert node to TEXT?
self.toklist[i] = self._new_node(type='TEXT',
content=str(self.toklist[i]))
@@ -492,7 +500,10 @@ class WikiMarkupParser(object):
flush()
acc['seq'].append(self.parse_inline_delim(tok))
else:
+ if self.strict:
raise UnexpectedTokenError(tok)
+ # FIXME: Another possible variant of handling this case is to
+ # convert tok to TEXT node and append it to acc['seq']
tok = self.getkn()
flush()
if acc['seq']:
@@ -976,6 +987,13 @@ class WikiMarkup(WikiMarkupParser):
media_base=URL
Base URL for media files. Default is
'http://www.mediawiki.org/xml/export-0.3'
+
+ debug_level=INT
+ debug verbosity level (0 - no debug info, 100 - excessively
+ copious debug messages). Default is 0.
+ strict=BOOL
+ Strict parsing mode. Throw exceptions on syntax errors. Default
+ is False.
"""
self.token_class = {
'NIL': WikiNode,
@@ -1006,6 +1024,9 @@ class WikiMarkup(WikiMarkupParser):
elif kw == 'filename':
self.file = open(keywords[kw])
elif kw == 'text':
+ if sys.version_info[0] > 2:
+ self.text = keywords[kw].decode('utf-8').split("\n")
+ else:
self.text = keywords[kw].split("\n")
elif kw == 'lang':
self.lang = keywords[kw]
@@ -1015,6 +1036,10 @@ class WikiMarkup(WikiMarkupParser):
self.image_base = keywords[kw]
elif kw == 'media_base':
self.media_base = keywords[kw]
+ elif kw == 'strict':
+ self.strict = keywords[kw]
+ elif kw == 'debug_level':
+ self.debug_level = keywords[kw]
def __del__(self):
if self.file:
diff --git a/wikitrans/wikitoken.py b/wikitrans/wikitoken.py
index deedea8..1f81092 100644
--- a/wikitrans/wikitoken.py
+++ b/wikitrans/wikitoken.py
@@ -311,7 +311,7 @@ class WikiEnvNode(WikiContentNode):
return {
'envtype': self.envtype,
'level': self.level,
- 'content': map(lambda x: x.json_encode(), self.content)
+ 'content': [x for x in map(lambda x: x.json_encode(), self.content)]
}

Return to:

Send suggestions and report system problems to the System administrator.