summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-08-16 15:45:00 +0300
committerSergey Poznyakoff <gray@gnu.org>2018-08-17 13:17:11 +0300
commit7186dbab7f1c1227e9229866e086bc417e3e4e52 (patch)
treef29114e9ff7a7b023dd3d611a9bc8808f5cf5bbd
parentd9e26129527ce84f626eb44ff95e4ecfbc5bc92a (diff)
downloadwikitrans-7186dbab7f1c1227e9229866e086bc417e3e4e52.tar.gz
wikitrans-7186dbab7f1c1227e9229866e086bc417e3e4e52.tar.bz2
Fix PEP 8 issues.
-rw-r--r--tests/test_html.py8
-rw-r--r--tests/test_texi.py6
-rw-r--r--tests/test_text.py8
-rw-r--r--tests/wikitest.py (renamed from tests/WikiTest.py)6
-rw-r--r--wikitrans/__init__.py (renamed from WikiTrans/__init__.py)0
-rw-r--r--wikitrans/wiki2html.py (renamed from WikiTrans/wiki2html.py)113
-rw-r--r--wikitrans/wiki2texi.py (renamed from WikiTrans/wiki2texi.py)61
-rw-r--r--wikitrans/wiki2text.py (renamed from WikiTrans/wiki2text.py)68
-rw-r--r--wikitrans/wikidump.py (renamed from WikiTrans/wikidump.py)41
-rw-r--r--wikitrans/wikimarkup.py (renamed from WikiTrans/wikimarkup.py)298
-rw-r--r--wikitrans/wikins.py (renamed from WikiTrans/wikins.py)0
-rw-r--r--wikitrans/wikitoken.py (renamed from WikiTrans/wikitoken.py)136
12 files changed, 545 insertions, 200 deletions
diff --git a/tests/test_html.py b/tests/test_html.py
index 3da57f6..5a15cb8 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -4,9 +4,9 @@ from __future__ import print_function
import unittest
-from WikiTrans.wiki2html import HtmlWiktionaryMarkup
-from WikiTest import populateMethods
+from wikitrans.wiki2html import HtmlWikiMarkup
+from wikitest import populate_methods
-class TestWiktionaryMarkup (unittest.TestCase):
+class TestWikiMarkup (unittest.TestCase):
pass
-populateMethods(TestWiktionaryMarkup, HtmlWiktionaryMarkup, '.html')
+populate_methods(TestWikiMarkup, HtmlWikiMarkup, '.html')
diff --git a/tests/test_texi.py b/tests/test_texi.py
index 75314c9..ddd26c7 100644
--- a/tests/test_texi.py
+++ b/tests/test_texi.py
@@ -4,4 +4,4 @@ from __future__ import print_function
import unittest
-from WikiTrans.wiki2texi import TexiWikiMarkup
-from WikiTest import populateMethods
+from wikitrans.wiki2texi import TexiWikiMarkup
+from wikitest import populate_methods
@@ -10,3 +10,3 @@ class TestTexiWikiMarkup (unittest.TestCase):
-populateMethods(TestTexiWikiMarkup, TexiWikiMarkup, '.texi')
+populate_methods(TestTexiWikiMarkup, TexiWikiMarkup, '.texi')
diff --git a/tests/test_text.py b/tests/test_text.py
index a06f519..b3d0a12 100644
--- a/tests/test_text.py
+++ b/tests/test_text.py
@@ -4,9 +4,9 @@ from __future__ import print_function
import unittest
-from WikiTrans.wiki2text import TextWiktionaryMarkup
-from WikiTest import populateMethods
+from wikitrans.wiki2text import TextWikiMarkup
+from wikitest import populate_methods
-class TestTextWiktionaryMarkup (unittest.TestCase):
+class TestTextWikiMarkup (unittest.TestCase):
pass
-populateMethods(TestTextWiktionaryMarkup, TextWiktionaryMarkup, '.text')
+populate_methods(TestTextWikiMarkup, TextWikiMarkup, '.text')
diff --git a/tests/WikiTest.py b/tests/wikitest.py
index 1429f5e..ff26227 100644
--- a/tests/WikiTest.py
+++ b/tests/wikitest.py
@@ -6,3 +6,3 @@ import os.path
-def MarkupTest(classname, name_in, name_out):
+def wiki_markup_test(classname, name_in, name_out):
fh = open(name_out)
@@ -21,6 +21,6 @@ def MarkupTest(classname, name_in, name_out):
-def populateMethods(cls, wcls, suffix):
+def populate_methods(cls, wcls, suffix):
def settest(self, base, wiki_name, pat_name):
def dyntest(self):
- self.assertTrue(MarkupTest(wcls, wiki_name, pat_name))
+ self.assertTrue(wiki_markup_test(wcls, wiki_name, pat_name))
meth = 'test_' + wcls.__name__ + '_' + base
diff --git a/WikiTrans/__init__.py b/wikitrans/__init__.py
index 5832e38..5832e38 100644
--- a/WikiTrans/__init__.py
+++ b/wikitrans/__init__.py
diff --git a/WikiTrans/wiki2html.py b/wikitrans/wiki2html.py
index 6147642..ce65bae 100644
--- a/WikiTrans/wiki2html.py
+++ b/wikitrans/wiki2html.py
@@ -17,6 +17,17 @@
+"""
+Wiki markup to HTML translator.
+
+Classes:
+
+HtmlWikiMarkup -- Converts Wiki material to HTML.
+HtmlWiktionaryMarkup -- Reserved for future use. Currently does the same as
+ HtmlWikiMarkup.
+
+"""
+
from __future__ import print_function
-from WikiTrans.wikimarkup import *
-from WikiTrans.wikitoken import *
-from WikiTrans.wikins import wiki_ns_re, wiki_ns
+from wikitrans.wikimarkup import *
+from wikitrans.wikitoken import *
+from wikitrans.wikins import wiki_ns_re, wiki_ns
import re
@@ -81,4 +92,3 @@ class HtmlLinkNode(HtmlSeqNode):
return "<a href=\"%s\">%s</a>" % (tgt,
- text if (text and text != '') \
- else arg)
+ text if (text and text != '') else arg)
@@ -88,5 +98,6 @@ class HtmlRefNode(WikiRefNode):
text = self.content.format()
- return "<a href=\"%s\">%s</a>" % (target,
- text if (text and text != '') \
- else target)
+ return "<a href=\"%s\">%s</a>" % (
+ target,
+ text if (text and text != '') else target
+ )
@@ -154,10 +165,10 @@ class HtmlTagNode(WikiTagNode):
n += 1
- s += ('<li id="cite_note-%d">' + \
- '<span class="mw-cite-backlink">' + \
- '<b><a href="#cite_ref-%d">^</a></b>' + \
- '</span>' + \
- '<span class="reference-text">' + \
- ref.content.format() + \
- '</span>' + \
- '</li>\n') % (n,n)
+ s += ('<li id="cite_note-%d">'
+ + '<span class="mw-cite-backlink">'
+ + '<b><a href="#cite_ref-%d">^</a></b>'
+ + '</span>'
+ + '<span class="reference-text">'
+ + ref.content.format()
+ + '</span>'
+ + '</li>\n') % (n,n)
s += '</ol>\n</div>\n'
@@ -190,7 +201,15 @@ class HtmlIndNode(WikiIndNode):
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.
+ """A Wiki markup to HTML translator class.
+
+ Usage:
+
+ x = HtmlWikiMarkup(file="input.wiki")
+ # Parse the input:
+ x.parse()
+ # Print it as HTML:
+ print(str(x))
+
+ Known bugs:
+ * [[official position]]s
+ Final 's' gets after closing </a> tag. Should be before.
"""
@@ -200,2 +219,26 @@ class HtmlWikiMarkup (WikiMarkup):
def __init__(self, *args, **kwargs):
+ """Create a HtmlWikiMarkup object.
+
+ Arguments:
+
+ filename=FILE
+ Read Wiki material from the file named FILE.
+ file=FD
+ Read Wiki material from file object FD.
+ text=STRING
+ Read Wiki material from STRING.
+ lang=CODE
+ Specifies source language. Default is 'en'. This variable can be
+ referred to as '%(lang)s' in the keyword arguments below.
+ html_base=URL
+ Base URL for cross-references. Default is
+ 'http://%(lang)s.wiktionary.org/wiki/'
+ image_base=URL
+ Base URL for images. Default is
+ 'http://upload.wikimedia.org/wikipedia/commons/thumb/a/bf'
+ media_base=URL
+ Base URL for media files. Default is
+ 'http://www.mediawiki.org/xml/export-0.3'
+ """
+
super(HtmlWikiMarkup, self).__init__(*args, **kwargs)
@@ -273,27 +316,5 @@ class HtmlWikiMarkup (WikiMarkup):
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. vast 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.
+ """A class for translating Wiktionary articles into HTML.
+
+ Reserved for future use. Currently does the same as HtmlWikiMarkup.
"""
diff --git a/WikiTrans/wiki2texi.py b/wikitrans/wiki2texi.py
index 7297195..d9e5f52 100644
--- a/WikiTrans/wiki2texi.py
+++ b/wikitrans/wiki2texi.py
@@ -17,5 +17,14 @@
-from WikiTrans.wikimarkup import *
-from WikiTrans.wikitoken import *
-from WikiTrans.wikins import wiki_ns_re, wiki_ns
+"""
+Wiki markup to Texinfo translator.
+
+Classes:
+
+TexiWikiMarkup -- Converts Wiki material to Texinfo.
+
+"""
+
+from wikitrans.wikimarkup import *
+from wikitrans.wikitoken import *
+from wikitrans.wikins import wiki_ns_re, wiki_ns
import re
@@ -254,2 +263,14 @@ class TexiRefNode(WikiRefNode):
class TexiWikiMarkup(WikiMarkup):
+ """Wiki markup to Texinfo translator class.
+
+ Usage:
+
+ x = TexiWikiMarkup(file="input.wiki")
+ # Parse the input:
+ x.parse()
+ # Print it as Texi:
+ print(str(x))
+
+ """
+
nested = 0
@@ -290,2 +311,36 @@ class TexiWikiMarkup (WikiMarkup):
def __init__(self, *args, **keywords):
+ """Create a TexiWikiMarkup object.
+
+ Arguments:
+
+ filename=FILE
+ Read Wiki material from the file named FILE.
+ file=FD
+ Read Wiki material from file object FD.
+ text=STRING
+ Read Wiki material from STRING.
+
+ sectioning_model=MODEL
+ Select the Texinfo sectioning model for the output document. Possible
+ values are:
+
+ 'numbered'
+ Top of document is marked with "@top". Headings ("=", "==",
+ "===", etc) produce "@chapter", "@section", "@subsection", etc.
+ 'unnumbered'
+ Unnumbered sectioning: "@top", "@unnumbered", "@unnumberedsec",
+ "@unnumberedsubsec".
+ 'appendix'
+ Sectioning suitable for appendix entries: "@top", "@appendix",
+ "@appendixsec", "@appendixsubsec", etc.
+ 'heading'
+ Use heading directives to reflect sectioning: "@majorheading",
+ "@chapheading", "@heading", "@subheading", etc.
+ sectioning_start=N
+ Shift resulting heading level by N positions. For example, supposing
+ "sectioning_model='numbered'", "== A ==" normally produces
+ "@section A" on output. Now, if given "sectioning_start=1", this
+ directive will produce "@subsection A" instead.
+ """
+
super(TexiWikiMarkup, self).__init__(*args, **keywords)
diff --git a/WikiTrans/wiki2text.py b/wikitrans/wiki2text.py
index cb3a183..1fbc61b 100644
--- a/WikiTrans/wiki2text.py
+++ b/wikitrans/wiki2text.py
@@ -17,5 +17,16 @@
-from WikiTrans.wikitoken import *
-from WikiTrans.wikimarkup import *
-from WikiTrans.wikins import wiki_ns_re, wiki_ns
+"""
+Wiki markup to plain text translator.
+
+Classes:
+
+TextWikiMarkup -- Converts Wiki material to plain text.
+TextWiktionaryMarkup -- Reserved for future use. Currently does the same as
+ TextWikiMarkup.
+
+"""
+
+from wikitrans.wikitoken import *
+from wikitrans.wikimarkup import *
+from wikitrans.wikins import wiki_ns_re, wiki_ns
import re
@@ -109,5 +120,5 @@ class TextLinkNode(WikiSeqNode):
text = "[%s: %s]" % (qual, text if text else arg)
- tgt = self.image_base + '/' + \
- url_quote(tgt) + \
- '/250px-' + url_quote(tgt)
+ tgt = "%s/%s/250px-%s" % (self.image_base,
+ url_quote(tgt),
+ url_quote(tgt))
elif ns == 'NS_MEDIA':
@@ -143,4 +154,7 @@ class TextHdrNode(WikiHdrNode):
def format(self):
- return "\n" + ("*" * self.level) + " " + \
- self.content.format().lstrip(" ") + "\n\n"
+ return ("\n"
+ + ("*" * self.level)
+ + " "
+ + self.content.format().lstrip(" ")
+ + "\n\n")
@@ -207,4 +221,12 @@ class TextTagNode(WikiTagNode):
class TextWikiMarkup(WikiMarkup):
- """
- A (general-purpose Wiki->Text translator class.
+ """A Wiki markup to plain text translator.
+
+ Usage:
+
+ x = TextWikiMarkup(file="input.wiki")
+ # Parse the input:
+ x.parse()
+ # Print it as plain text:
+ print(str(x))
+
"""
@@ -225,2 +247,21 @@ class TextWikiMarkup (WikiMarkup):
def __init__(self, *args, **keywords):
+ """Create a TextWikiMarkup object.
+
+ Arguments:
+
+ filename=FILE
+ Read Wiki material from the file named FILE.
+ file=FD
+ Read Wiki material from file object FD.
+ text=STRING
+ Read Wiki material from STRING.
+
+ width=N
+ Limit output width to N columns. Default is 78.
+ show_urls=False
+ By default, the link URLs are displayed in parentheses next to the
+ link text. If this argument is given, only the link text will be
+ displayed.
+ """
+
super(TextWikiMarkup,self).__init__(*args, **keywords)
@@ -301,6 +342,7 @@ class TextWikiMarkup (WikiMarkup):
class TextWiktionaryMarkup(TextWikiMarkup):
+ """A class for translating Wiktionary articles into plain text.
+
+ Reserved for future use. Currently does the same as TextWikiMarkup.
"""
- See documentation for HtmlWiktionaryMarkup
- """
- # FIXME: It is supposed to do something about templates
+
diff --git a/WikiTrans/wikidump.py b/wikitrans/wikidump.py
index 7457dfa..d5f651c 100644
--- a/WikiTrans/wikidump.py
+++ b/wikitrans/wikidump.py
@@ -16,6 +16,15 @@
+"""
+Print Wiki parse tree as JSON.
+
+Classes:
+
+DumpWikiMarkup
+
+"""
+
from __future__ import print_function
-from WikiTrans.wikitoken import *
+from wikitrans.wikitoken import *
import json
-from WikiTrans.wikimarkup import WikiMarkup
+from wikitrans.wikimarkup import WikiMarkup
@@ -29,2 +38,14 @@ class DumpReferences(object):
class DumpWikiMarkup(WikiMarkup):
+ """Produce a JSON dump of the Wiki markup parse tree.
+
+ Usage:
+
+ x = DumpWikiMarkup(file="input.wiki")
+ # Parse the input:
+ x.parse()
+ # Print a JSON dump of the parse tree
+ print(str(x))
+
+ """
+
indent = None
@@ -32,2 +53,16 @@ class DumpWikiMarkup(WikiMarkup):
def __init__(self, **kwarg):
+ """Create a DumpWikiMarkup object.
+
+ Arguments:
+
+ filename=FILE
+ Read Wiki material from the file named FILE.
+ file=FD
+ Read Wiki material from file object FD.
+ text=STRING
+ Read Wiki material from STRING.
+ indent=N
+ Basic indent offset for JSON objects.
+ """
+
n = kwarg.pop('indent', None)
@@ -35,3 +70,3 @@ class DumpWikiMarkup(WikiMarkup):
self.indent = int(n)
- WikiMarkup.__init__(self, **kwarg)
+ super(DumpWikiMarkup,self).__init__(self, **kwarg)
def __str__(self):
diff --git a/WikiTrans/wikimarkup.py b/wikitrans/wikimarkup.py
index 6cbf5de..77c3b30 100644
--- a/WikiTrans/wikimarkup.py
+++ b/wikitrans/wikimarkup.py
@@ -17,2 +17,16 @@
+"""
+Wiki markup parser.
+
+This module provides two class:
+
+WikiMarkupParser:
+ An abstract parser class, which serves as a base class for all markup
+ classes in this package.
+
+WikiMarkup
+ A subclass of the above, providing basic input method.
+
+"""
+
from __future__ import print_function
@@ -21,8 +35,8 @@ import re
from types import *
-from WikiTrans.wikitoken import *
+from wikitrans.wikitoken import *
-__all__ = [ "BaseWikiMarkup", "WikiMarkup",
- "TagAttributes", "TagAttributeSyntax" ]
+__all__ = [ "WikiMarkupParser", "WikiMarkup",
+ "TagAttributes", "TagAttributeSyntaxError" ]
-class UnexpectedToken(Exception):
+class UnexpectedTokenError(Exception):
def __init__(self, value):
@@ -30,3 +44,3 @@ class UnexpectedToken(Exception):
-class TagAttributeSyntax(Exception):
+class TagAttributeSyntaxError(Exception):
def __init__(self, value):
@@ -37,2 +51,13 @@ class TagAttributeSyntax(Exception):
class TagAttributes(object):
+ """A dictionary-like collection of tag attributes.
+
+ Example:
+
+ attr = TagAttributes('href="foo" length=2')
+ if 'href' in attr:
+ print(x['href']) # returns "foo"
+ for a in attr:
+ ...
+ """
+
attrstart = re.compile("^(?P<attr>[a-zA-Z0-9_-]+)(?P<eq>=\")?")
@@ -70,3 +95,3 @@ class TagAttributes(object):
else:
- raise TagAttributeSyntax(s)
+ raise TagAttributeSyntaxError(s)
def __len__(self):
@@ -91,3 +116,22 @@ class TagAttributes(object):
-class BaseWikiMarkup(object):
+class WikiMarkupParser(object):
+ """Parser for Wiki markup language.
+
+ Given input in Wiki markup language creates an abstract parse tree for it.
+ This is a base class for actual parsers. The subclasses must provide the
+ input method.
+
+ Public methods:
+
+ parse() -- parse the input.
+
+ Abstract methods (must be overridden by the subclass):
+
+ input() -- returns next physical line from the input material.
+
+ Public attributes:
+
+ tree -- constructed parse tree (a subclass of WikiNode)
+
+ """
@@ -121,2 +165,5 @@ class BaseWikiMarkup(object):
def dprint(self, lev, fmt, *argv):
+ """If current debug level is greater than or equal to lev, print *argv
+ according to format.
+ """
if self.debug_level >= lev:
@@ -150,3 +197,3 @@ class BaseWikiMarkup(object):
- def __createWikiNode(self,**kwarg):
+ def _new_node(self,**kwarg):
return self.token_class[kwarg['type']](self, **kwarg)
@@ -154,2 +201,3 @@ class BaseWikiMarkup(object):
def tokread(self):
+ """Read next token from the input. Return it as a subclass of WikiNode."""
line = None
@@ -165,3 +213,3 @@ class BaseWikiMarkup(object):
if not line or line == "":
- yield(self.__createWikiNode(type='NIL'))
+ yield(self._new_node(type='NIL'))
break
@@ -169,3 +217,3 @@ class BaseWikiMarkup(object):
if line == '\n':
- yield(self.__createWikiNode(type='NL'))
+ yield(self._new_node(type='NL'))
line = None
@@ -178,3 +226,3 @@ class BaseWikiMarkup(object):
if (pos < m.start(0)):
- yield(self.__createWikiNode(type='TEXT',
+ yield(self._new_node(type='TEXT',
content=line[pos:m.start(0)]))
@@ -193,3 +241,3 @@ class BaseWikiMarkup(object):
if m and m.group('tag') == 'nowiki':
- yield(self.__createWikiNode(type='TEXT',
+ yield(self._new_node(type='TEXT',
content=line[pos:m.start(0)] ))
@@ -198,3 +246,3 @@ class BaseWikiMarkup(object):
- yield(self.__createWikiNode(type='TEXT',
+ yield(self._new_node(type='TEXT',
content=line[pos:]))
@@ -208,3 +256,3 @@ class BaseWikiMarkup(object):
try:
- yield(self.__createWikiNode(type='OTAG',
+ yield(self._new_node(type='OTAG',
tag=m.group('tag'),
@@ -213,9 +261,10 @@ class BaseWikiMarkup(object):
if m.group('closed'):
- yield(self.__createWikiNode(type='CTAG',
+ yield(self._new_node(type='CTAG',
tag=m.group('tag')))
- except TagAttributeSyntax:
- yield(self.__createWikiNode(type='TEXT',content=m.group(0)))
+ except TagAttributeSyntaxError:
+ yield(self._new_node(type='TEXT',
+ content=m.group(0)))
continue
else:
- yield(self.__createWikiNode(type='TEXT',content=m.group(0)))
+ yield(self._new_node(type='TEXT',content=m.group(0)))
continue
@@ -225,3 +274,3 @@ class BaseWikiMarkup(object):
if m.group('tag') in self.tags:
- yield(self.__createWikiNode(type='CTAG',
+ yield(self._new_node(type='CTAG',
tag=m.group('tag')))
@@ -230,3 +279,3 @@ class BaseWikiMarkup(object):
else:
- yield(self.__createWikiNode(type='TEXT',
+ yield(self._new_node(type='TEXT',
content=line[pos:pos+1]))
@@ -238,3 +287,3 @@ class BaseWikiMarkup(object):
if content[0] in self.envtypes:
- node = self.__createWikiNode(type='DELIM',
+ node = self._new_node(type='DELIM',
content=content,
@@ -251,3 +300,3 @@ class BaseWikiMarkup(object):
else:
- yield(self.__createWikiNode(type='DELIM',
+ yield(self._new_node(type='DELIM',
isblock=(content.strip() not in self.inline_delims),
@@ -259,6 +308,6 @@ class BaseWikiMarkup(object):
if line[pos:-1] != '':
- yield(self.__createWikiNode(type='TEXT',content=line[pos:-1]))
- yield(self.__createWikiNode(type='NL'))
+ yield(self._new_node(type='TEXT',content=line[pos:-1]))
+ yield(self._new_node(type='NL'))
else:
- yield(self.__createWikiNode(type='TEXT',content=line[pos:]))
+ yield(self._new_node(type='TEXT',content=line[pos:]))
line = None
@@ -267,2 +316,6 @@ class BaseWikiMarkup(object):
def input(self):
+ """Return next physical line from the input.
+
+ This method must be overridden by the subclass.
+ """
return None
@@ -270,2 +323,3 @@ class BaseWikiMarkup(object):
def swaptkn(self, i, j):
+ """Swap tokens at indices i and j in toklist."""
self.dprint(80, "SWAPPING %s <-> %s", i, j)
@@ -276,2 +330,7 @@ class BaseWikiMarkup(object):
def tokenize(self):
+ """Tokenize the input.
+
+ Read tokens from the input (supplied by the input() method). Place the
+ obtained tokens in the toklist array.
+ """
self.toklist = []
@@ -293,5 +352,5 @@ class BaseWikiMarkup(object):
for i in range(0,len(self.toklist)):
- if self.toklist[i].type == 'DELIM' \
- and (self.toklist[i].content == "''" \
- or self.toklist[i].content == "'''"):
+ if (self.toklist[i].type == 'DELIM'
+ and (self.toklist[i].content == "''"
+ or self.toklist[i].content == "'''")):
if len(stack) > 0:
@@ -305,5 +364,6 @@ class BaseWikiMarkup(object):
stack.pop()
- elif i < len(self.toklist) \
- and self.toklist[i+1].type == 'DELIM' \
- and self.toklist[stack[-1]].content == self.toklist[i+1].content:
+ elif (i < len(self.toklist)
+ and self.toklist[i+1].type == 'DELIM'
+ and self.toklist[stack[-1]].content
+ == self.toklist[i+1].content):
# Case 3: swap current and next tokens
@@ -325,2 +385,3 @@ class BaseWikiMarkup(object):
def push_mark(self):
+ """Save the current token index on stack."""
self.mark.append(self.tokind)
@@ -328,2 +389,3 @@ class BaseWikiMarkup(object):
def pop_mark(self):
+ """Restore the token index from top of stack."""
self.tokind = self.mark.pop()
@@ -331,2 +393,3 @@ class BaseWikiMarkup(object):
def clear_mark(self):
+ """Forget the last mark."""
self.mark.pop()
@@ -334,2 +397,3 @@ class BaseWikiMarkup(object):
def lookahead(self, off=0):
+ """Peek a token at index (tokind+off)."""
tok = self.toklist[self.tokind+off]
@@ -339,2 +403,3 @@ class BaseWikiMarkup(object):
def setkn(self,val):
+ """Store token val at the current token index."""
self.toklist[self.tokind] = val
@@ -342,5 +407,6 @@ class BaseWikiMarkup(object):
def getkn(self):
+ """Get next token from the toklist. Advance tokind."""
self.newline = self.tokind == 0 or self.toklist[self.tokind-1].type == 'NL'
if self.tokind == len(self.toklist):
- return self.__createWikiNode(type='NIL')
+ return self._new_node(type='NIL')
tok = self.toklist[self.tokind]
@@ -351,2 +417,8 @@ class BaseWikiMarkup(object):
def ungetkn(self, tok=None):
+ """Unget the last read token.
+
+ Decrease the tokind by one, so the last read token will be read again.
+ If optional argument is supplied and is not None, store it in the toklist
+ in place of the current token.
+ """
self.tokind = self.tokind - 1
@@ -359,4 +431,5 @@ class BaseWikiMarkup(object):
def fixuptkn(self, tok):
+ """Replace the recently read token by tok."""
if self.tokind == 0:
- raise IndexError('wikimarkup.fixuptkn called at start of input')
+ raise IndexError('WikiMarkupParser.fixuptkn called at start of input')
self.toklist[self.tokind-1] = tok
@@ -365,2 +438,3 @@ class BaseWikiMarkup(object):
def dump(self, tree, file=sys.stdout):
+ """Dump the tree to file, node by node."""
for node in tree:
@@ -370,2 +444,3 @@ class BaseWikiMarkup(object):
def is_block_end(self, tok):
+ """Return True if tok ends a block environment."""
if tok.type == 'NIL':
@@ -385,2 +460,3 @@ class BaseWikiMarkup(object):
def parse_para(self, tok):
+ """Read paragraph starting at tok."""
self.dprint(80, "ENTER parse_para: %s", tok)
@@ -392,3 +468,3 @@ class BaseWikiMarkup(object):
if acc['textlist']:
- acc['seq'].append(self.__createWikiNode(type='TEXT',
+ acc['seq'].append(self._new_node(type='TEXT',
content=''.join(acc['textlist'])))
@@ -396,5 +472,5 @@ class BaseWikiMarkup(object):
- if isinstance(tok, WikiContentNode) \
- and isinstance(tok.content,str) \
- and re.match("^[ \t]", tok.content):
+ if (isinstance(tok, WikiContentNode)
+ and isinstance(tok.content,str)
+ and re.match("^[ \t]", tok.content)):
type = 'PRE'
@@ -420,3 +496,3 @@ class BaseWikiMarkup(object):
else:
- raise UnexpectedToken(tok)
+ raise UnexpectedTokenError(tok)
tok = self.getkn()
@@ -424,3 +500,3 @@ class BaseWikiMarkup(object):
if acc['seq']:
- tok = self.__createWikiNode(type=type, content=acc['seq'])
+ tok = self._new_node(type=type, content=acc['seq'])
else:
@@ -431,2 +507,3 @@ class BaseWikiMarkup(object):
def parse_block_delim(self, tok):
+ """Parse block environment starting at tok."""
self.dprint(80, "ENTER parse_block_delim")
@@ -434,3 +511,3 @@ class BaseWikiMarkup(object):
if tok.content == "----":
- node = self.__createWikiNode(type = 'BAR')
+ node = self._new_node(type = 'BAR')
elif tok.content[0:2] == "==":
@@ -438,3 +515,3 @@ class BaseWikiMarkup(object):
if not node:
- tok = self.ungetkn(self.__createWikiNode(type='TEXT',
+ tok = self.ungetkn(self._new_node(type='TEXT',
content=tok.content))
@@ -455,2 +532,3 @@ class BaseWikiMarkup(object):
def parse_line(self):
+ """Parse the input line."""
self.dprint(80, "ENTER parse_line")
@@ -465,4 +543,3 @@ class BaseWikiMarkup(object):
if tok.isblock:
- tok = self.__createWikiNode(type = 'TEXT',
- content = tok.content)
+ tok = self._new_node(type = 'TEXT', content = tok.content)
self.fixuptkn(tok)
@@ -478,3 +555,4 @@ class BaseWikiMarkup(object):
else:
- list.append(self.fixuptkn(self.__createWikiNode(type = 'TEXT', content = tok.content)))
+ list.append(self.fixuptkn(self._new_node(type = 'TEXT',
+ content = tok.content)))
elif tok.type == 'OTAG':
@@ -486,3 +564,3 @@ class BaseWikiMarkup(object):
list.append(tok)
- ret = self.__createWikiNode(type='SEQ', content=list)
+ ret = self._new_node(type='SEQ', content=list)
self.dprint(80, "LEAVE parse_line=%s", ret)
@@ -491,5 +569,6 @@ class BaseWikiMarkup(object):
def parse_indent(self, tok):
+ """Parse indented block starting at tok."""
lev = len(tok.content)
self.dprint(80, "ENTER parse_indent(%s)", lev)
- x = self.__createWikiNode(type='IND', level=lev, content=self.parse_line())
+ x = self._new_node(type='IND', level=lev, content=self.parse_line())
self.dprint(80, "LEAVE parse_indent=%s", x)
@@ -498,2 +577,9 @@ class BaseWikiMarkup(object):
def parse_fontmod(self,delim,what):
+ """Parse font modification directive (bold or italics).
+
+ Arguments:
+
+ delim -- starting delimiter ("''" or "'''")
+ what -- 'IT' or 'BOLD'
+ """
self.dprint(80, "ENTER parse_fontmod(%s,%s), tok %s",
@@ -515,3 +601,3 @@ class BaseWikiMarkup(object):
if text:
- seq.append(self.__createWikiNode(type='TEXT', content=text))
+ seq.append(self._new_node(type='TEXT', content=text))
text = ''
@@ -524,3 +610,3 @@ class BaseWikiMarkup(object):
elif tok.type == 'NL':
- seq.append(self.__createWikiNode(type='TEXT', content='\n'))
+ seq.append(self._new_node(type='TEXT', content='\n'))
else:
@@ -529,4 +615,4 @@ class BaseWikiMarkup(object):
if text:
- seq.append(self.__createWikiNode(type='TEXT', content=text))
- res = self.__createWikiNode(type=what, content=seq)
+ seq.append(self._new_node(type='TEXT', content=text))
+ res = self._new_node(type=what, content=seq)
self.dprint(80, "LEAVE parse_fontmod=%s", res)
@@ -535,2 +621,3 @@ class BaseWikiMarkup(object):
def parse_ref(self):
+ """Parse a reference block ([...])"""
self.dprint(80, "ENTER parse_ref")
@@ -544,3 +631,3 @@ class BaseWikiMarkup(object):
if text:
- seq.insert(0, self.__createWikiNode(type='TEXT', content=text))
+ seq.insert(0, self._new_node(type='TEXT', content=text))
@@ -569,5 +656,4 @@ class BaseWikiMarkup(object):
- ret = self.__createWikiNode(type='REF',
- ref=ref,
- content=self.__createWikiNode(type='SEQ', content=seq))
+ ret = self._new_node(type='REF', ref=ref,
+ content=self._new_node(type='SEQ', content=seq))
self.dprint(80, "LEAVE parse_ref= %s", ret)
@@ -576,2 +662,12 @@ class BaseWikiMarkup(object):
def parse_link(self, type, delim):
+ """Parse an external link ([[...]]).
+
+ In this implementation, it is also used to parse template
+ references ({{...}}).
+
+ Arguments:
+
+ type -- 'LINK' or 'TMPL'
+ delim -- expected closing delimiter.
+ """
self.dprint(80, "ENTER parse_link(%s,%s)", type, delim)
@@ -587,3 +683,3 @@ class BaseWikiMarkup(object):
if list:
- subtree.append(self.__createWikiNode(type='SEQ',
+ subtree.append(self._new_node(type='SEQ',
content=list))
@@ -592,3 +688,3 @@ class BaseWikiMarkup(object):
if len(list) > 1:
- subtree.append(self.__createWikiNode(type='SEQ',
+ subtree.append(self._new_node(type='SEQ',
content=list))
@@ -609,3 +705,3 @@ class BaseWikiMarkup(object):
return None
- ret = self.__createWikiNode(type=type, content=subtree)
+ ret = self._new_node(type=type, content=subtree)
self.dprint(80, "LEAVE parse_link=%s", ret)
@@ -614,2 +710,3 @@ class BaseWikiMarkup(object):
def parse_inline_delim(self, tok):
+ """Parse an inline block."""
self.dprint(80, "ENTER parse_inline_delim")
@@ -635,4 +732,3 @@ class BaseWikiMarkup(object):</