diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2015-07-05 14:13:45 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2015-07-05 14:18:07 +0300 |
commit | 729f5a4e4ad71ff62d56cae2336738d65dc574f9 (patch) | |
tree | 2e66ad09adf70c299d64eba158f68c8ef43e0268 | |
parent | a8d15328a95bc16c3d9f4ca06c0c69767899f678 (diff) | |
download | wikitrans-729f5a4e4ad71ff62d56cae2336738d65dc574f9.tar.gz wikitrans-729f5a4e4ad71ff62d56cae2336738d65dc574f9.tar.bz2 |
Fix most testcases.
* wikimarkup.py (envtypes): Redo as a dictionary.
(ELT): New item type
(BaseWikiMarkup): Fix list parsing.
(parse): Comment out changes introduced by 9c42879.
Must be rewritten.
* wiki2html.py (HtmlWikiMarkup): Change handling of lists.
* wiki2text.py: Likewise.
* testdata/deflist.wiki: New testcase.
* testdata/deflist.html: Likewise.
* testdata/colon.html: Update.
* testdata/headings.html: Update.
* testdata/hz.html: Update.
* testdata/numlist.html: Update.
* testdata/unlist.html: Update.
-rw-r--r-- | test.py | 4 | ||||
-rw-r--r-- | testdata/colon.html | 9 | ||||
-rw-r--r-- | testdata/deflist.html | 2 | ||||
-rw-r--r-- | testdata/deflist.wiki | 5 | ||||
-rw-r--r-- | testdata/headings.html | 20 | ||||
-rw-r--r-- | testdata/hz.html | 10 | ||||
-rw-r--r-- | testdata/numlist.html | 7 | ||||
-rw-r--r-- | testdata/unlist.html | 10 | ||||
-rw-r--r-- | wiki2html.py | 22 | ||||
-rw-r--r-- | wiki2text.py | 12 | ||||
-rw-r--r-- | wikimarkup.py | 50 |
11 files changed, 77 insertions, 74 deletions
@@ -42,2 +42,6 @@ class TestMarkupParserBasic (unittest.TestCase): + def test_deflist(self): + self.assert_(self.__test('unlist')) + pass + def test_door(self): diff --git a/testdata/colon.html b/testdata/colon.html index 9721b93..b9e35ed 100644 --- a/testdata/colon.html +++ b/testdata/colon.html @@ -1,9 +1,4 @@ -<dl><dd> A colon (:) indents a line or paragraph. -</dd></dl>A newline starts a new paragraph. +<dl><dd> A colon (:) indents a line or paragraph.</dd></dl><p>A newline starts a new paragraph. Should only be used on talk pages. -For articles, you probably want the blockquote tag. -<dl><dd> We use 1 colon to indent once. -</dd><dl><dd> We use 2 colons to indent twice. -</dd><dl><dd> 3 colons to indent 3 times, and so on. -</dd></dl></dl></dl> +For articles, you probably want the blockquote tag.</p><dl><dd> We use 1 colon to indent once.<dl><dd> We use 2 colons to indent twice.<dl><dd> 3 colons to indent 3 times, and so on.</dd></dl></dd></dl></dd></dl> diff --git a/testdata/deflist.html b/testdata/deflist.html new file mode 100644 index 0000000..8986533 --- /dev/null +++ b/testdata/deflist.html @@ -0,0 +1,2 @@ +<dl><dt>item 1</dt><dd> definition 1</dd><dt>item 2</dt><dd> definition 2-1</dd><dd> definition 2-2</dd></dl> + diff --git a/testdata/deflist.wiki b/testdata/deflist.wiki new file mode 100644 index 0000000..19cc7c6 --- /dev/null +++ b/testdata/deflist.wiki @@ -0,0 +1,5 @@ +;item 1 +: definition 1 +;item 2 +: definition 2-1 +: definition 2-2 diff --git a/testdata/headings.html b/testdata/headings.html index 8b247a3..0ec26ac 100644 --- a/testdata/headings.html +++ b/testdata/headings.html @@ -1,15 +1,11 @@ -<h1> Section headings </h1> -<i>Headings</i> organize your writing into -sections. The Wiki software can automatically -generate a <a href="http://pl.wiktionary.org/table%20of%20contents">table of contents</a> from them. - -<h2> Subsection </h2>Using more "equals" (=) signs creates a subsection. +<h2> Section headings </h2> -<h3> A smaller subsection </h3> -Don't skip levels, -like from two to four equals signs. +<p><i>Headings</i> organize your writing into +sections. The Wiki software can automatically +generate a <a href="http://pl.wiktionary.org/wiki/table%20of%20contents">table of contents</a> from them.</p><h3> Subsection </h3> +<p>Using more "equals" (=) signs creates a subsection.</p><h4> A smaller subsection </h4> -Start with 2 equals signs not 1 +<p>Don't skip levels, +like from two to four equals signs.</p><p>Start with 2 equals signs not 1 because 1 creates H1 tags -which should be reserved for page title. - +which should be reserved for page title.</p> diff --git a/testdata/hz.html b/testdata/hz.html index 569d1fe..507a730 100644 --- a/testdata/hz.html +++ b/testdata/hz.html @@ -1,6 +1,4 @@ -You can make horizontal dividing lines (----) -to separate text. -<hr/>But you should usually use sections instead, -so that they go in the table of contents. - - +<p>You can make horizontal dividing lines (----) +to separate text.</p><hr/> +<p>But you should usually use sections instead, +so that they go in the table of contents.</p> diff --git a/testdata/numlist.html b/testdata/numlist.html index c65d0a2..ce9dd2d 100644 --- a/testdata/numlist.html +++ b/testdata/numlist.html @@ -1,7 +1,2 @@ -<ol><li> <i>Numbered lists</i> are: -</li><ol><li> Very organized -</li><li> Easy to follow -</li></ol></ol>A newline marks the end of the list. -<ol><li> New numbering starts with 1. -</li></ol> +<ol><li> <i>Numbered lists</i> are:<ol><li> Very organized</li><li> Easy to follow</li></ol></li></ol><p>A newline marks the end of the list.</p><ol><li> New numbering starts with 1.</li></ol> diff --git a/testdata/unlist.html b/testdata/unlist.html index 8d3eab7..87be30f 100644 --- a/testdata/unlist.html +++ b/testdata/unlist.html @@ -1,10 +1,2 @@ -<ul><li> <i>Unordered lists</i> are easy to do: -</li><ul><li> Start every line with a star. -</li><ul><li> More stars indicate a deeper level. -</li></ul><li> Previous item continues. -</li><li> A newline -</li></ul><li> in a list -</li></ul>marks the end of the list. -<ul><li>Of course you can start again. -</li></ul> +<ul><li> <i>Unordered lists</i> are easy to do:<ul><li> Start every line with a star.<ul><li> More stars indicate a deeper level.</li></ul></li></ul> Previous item continues.<ul><li> A newline</li></ul></li><li> in a list </li></ul><p>marks the end of the list.</p><ul><li>Of course you can start again.</li></ul> diff --git a/wiki2html.py b/wiki2html.py index 81ada65..77cb97a 100644 --- a/wiki2html.py +++ b/wiki2html.py @@ -40,5 +40,8 @@ class HtmlWikiMarkup (WikiMarkup): - - envhdr = [ "ul", "ol", "dl" ] - envel = [ "li", "li", "dd" ] + envt = { "unnumbered": { "hdr": "ul", + "elt": ["li"] }, + "numbered": { "hdr": "ol", + "elt": ["li"] }, + "defn": { "hdr": "dl", + "elt": ["dt","dd"] } } @@ -162,9 +165,10 @@ class HtmlWikiMarkup (WikiMarkup): for s in elt[3]: - x = self.format(s) - string += "<%s>%s</%s>" % (self.envel[type], - self.format(s), - self.envel[type]) - return "<%s>%s</%s>" % (self.envhdr[type], + n = s[1]; + string += "<%s>%s</%s>" % (self.envt[type]["elt"][n], + self.format(s[2]), + self.envt[type]["elt"][n]) + return "<%s>%s</%s>" % (self.envt[type]["hdr"], string, - self.envhdr[type]) + self.envt[type]["hdr"]) + return string diff --git a/wiki2text.py b/wiki2text.py index 0f8dd5f..005e551 100644 --- a/wiki2text.py +++ b/wiki2text.py @@ -209,8 +209,14 @@ class TextWikiMarkup (WikiMarkup): string += "\n" - x = self.format(s) - if type == ENVUNNUM: + x = self.format(s[2]) + if type == "unnumbered": string += self.fmtpara(self.indent(lev, "- " + x.lstrip(" "))) - elif type == ENVNUM: + elif type == "numbered": string += self.fmtpara(self.indent(lev, "%d. %s" % (n, x))) n += 1 + elif type == "defn": + if s[1] == 0: + string += self.indent(lev-1, x) + else: + string += self.indent(lev+3, x) + if not string.endswith("\n"): diff --git a/wikimarkup.py b/wikimarkup.py index fa60c80..2b6348e 100644 --- a/wikimarkup.py +++ b/wikimarkup.py @@ -27,3 +27,3 @@ __all__ = [ "BaseWikiMarkup", "WikiMarkup", -delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^:+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)") +delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^[;:]+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)") @@ -45,2 +45,3 @@ IND = 13 SEQ = 14 +ELT = 15 @@ -51,3 +52,7 @@ ENVUNNUM = 0 ENVNUM = 1 -envtypes = [ "*", "#" ] +envtypes = { "*": [ "unnumbered", 0 ], + "#": [ "numbered", 0 ], + ";": [ "defn", 0 ], + ":": [ "defn", 1 ] + } @@ -95,3 +100,4 @@ class BaseWikiMarkup: pos = m.end(0) - if m.group(0)[0] in envtypes and line[pos] == ":": + if envtypes.has_key(m.group(0)[0]) and line[pos] == ":": + # FIXME? self.dprint(100, "YIELD: DELIM %s, True", m.group(0)) @@ -383,3 +389,3 @@ class BaseWikiMarkup: tok = self.getkn() - if tok[0] == DELIM and tok[1][0] in envtypes and type == envtypes.index(tok[1][0]): + if tok[0] == DELIM and envtypes.has_key(tok[1][0]) and type == envtypes[tok[1][0]][0]: if len(tok[1]) < lev: @@ -393,9 +399,9 @@ class BaseWikiMarkup: if len(tok) == 2: - list.append(elt) + list.append((ELT, envtypes[tok[1][0]][1], elt)) continue - if list[-1][0] != SEQ: - x = list[-1] - list[-1] = (SEQ, [x]) - list[-1][1].append(elt) + if list[-1][2][0] != SEQ: + x = list[-1][2][1] + list[-1][2] = (SEQ, [x]) + list[-1][2][1].append(elt) else: @@ -425,4 +431,4 @@ class BaseWikiMarkup: return self.parse_header(tok[1]) - elif tok[1][0] in envtypes: - type = envtypes.index(tok[1][0]) + elif envtypes.has_key(tok[1][0]): + type = envtypes[tok[1][0]][0] lev = len(tok[1]) @@ -544,13 +550,13 @@ class WikiMarkup (BaseWikiMarkup): BaseWikiMarkup.parse(self) - # Remove everything before the first header - for i in range(0, len(self.tree)): - if self.tree[i][0] == HDR: - self.tree = self.tree[i:] - break - # Remove trailing links - for i in range(len(self.tree)-1, 0, -1): - if self.tree[i][0] == PARA \ - and not self.is_empty_para(self.tree[i][1]): - self.tree = self.tree[0:i+1] - break + # # Remove everything before the first header + # for i in range(0, len(self.tree)): + # if self.tree[i][0] == HDR: + # self.tree = self.tree[i:] + # break + # # Remove trailing links + # for i in range(len(self.tree)-1, 0, -1): + # if self.tree[i][0] == PARA \ + # and not self.is_empty_para(self.tree[i][1]): + # self.tree = self.tree[0:i+1] + # break |