diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-07-20 13:43:55 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-07-20 13:43:55 +0300 |
commit | c41c19ff0b19cbca7c850239bfa11295a674f520 (patch) | |
tree | 4336a509242f00cd4910f949fdb45199266566ec | |
parent | 63f5f9902f83bd65fd2a37239ab9d6e5876924fd (diff) | |
download | wikitrans-c41c19ff0b19cbca7c850239bfa11295a674f520.tar.gz wikitrans-c41c19ff0b19cbca7c850239bfa11295a674f520.tar.bz2 |
Improve tag handling.
* WikiTrans/wiki2texi.py (str_tag): Use the 'isblock' attribute
to decide how to render the block.
* WikiTrans/wikimarkup.py (parse_para): Parse tags.
(parse_til): Rename to parse_tag. All callers changed.
Set the 'isblock' attribute
(parse0): Call parse_para if at the beginning of the input.
-rw-r--r-- | WikiTrans/wiki2texi.py | 12 | ||||
-rw-r--r-- | WikiTrans/wikimarkup.py | 57 |
2 files changed, 46 insertions, 23 deletions
diff --git a/WikiTrans/wiki2texi.py b/WikiTrans/wiki2texi.py index 06e854a..f36c0a1 100644 --- a/WikiTrans/wiki2texi.py +++ b/WikiTrans/wiki2texi.py @@ -140,3 +140,3 @@ class TexiWikiMarkup (WikiMarkup): def str_tag(self, elt): - if elt['tag'] == 'code': + if elt['tag'] in ['code', 'tt']: save = self._begin_print() @@ -146,5 +146,5 @@ class TexiWikiMarkup (WikiMarkup): s = self._end_print(save) - if s.startswith('\n'): + if elt['isblock']: self._print('@example', nl=True, escape=False) - self._print(s) + self._print(s, escape=False) self._print('@end example\n', nl=True, escape=False) @@ -152,8 +152,2 @@ class TexiWikiMarkup (WikiMarkup): self._print('@code{%s}' % s, escape=False) - elif elt['tag'] == 'tt': - self._print('@code{', escape=False) - self.nested += 1 - s = self.format(elt['content']) - self.nested -= 1 - self._print('}', escape=False) elif elt['tag'] == 'div': diff --git a/WikiTrans/wikimarkup.py b/WikiTrans/wikimarkup.py index ba30269..d56c664 100644 --- a/WikiTrans/wikimarkup.py +++ b/WikiTrans/wikimarkup.py @@ -139,2 +139,5 @@ class BaseWikiMarkup(object): file.write("TAG: %s\n" % node['tag']) + if 'isblock' in node: + self.print_dump_prefix(level, file) + file.write("PLACEMENT: %s\n" % ('BLOCK' if node['isblock'] else 'INLINE')) if 'args' in node: @@ -511,3 +514,3 @@ class BaseWikiMarkup(object): elif tok['type'] == 'OTAG': - list.append(self.parse_til(tok)) + list.append(self.parse_tag(tok)) else: @@ -575,3 +578,3 @@ class BaseWikiMarkup(object): if self.newline: - if re.match("^\s", tok['content']): + if 'content' in tok and re.match("^\s", tok['content']): type = 'PRE' @@ -584,3 +587,3 @@ class BaseWikiMarkup(object): rx = None - + self.dprint(80, "IN parse_para, type %s", type) while 1: @@ -603,3 +606,27 @@ class BaseWikiMarkup(object): break - elif tok['type'] == 'OTAG' or tok['type'] == 'CTAG' or tok['type'] == 'TAG': + elif tok['type'] == 'OTAG': + save = (self.tokind,self.newline) + t = self.parse_tag(tok) + if t['type'] == 'TAG' and t['isblock']: + del self.toklist[save[0]:self.tokind] + self.tokind = save[0] + self.toklist[self.tokind] = t + self.newline = save[1] + break + else: + if textlist: + seq.append({ 'type': 'TEXT', + 'content': ''.join(textlist) }) + textlist = [] + seq.append(t) + elif tok['type'] == 'TAG': + if tok['isblock']: + break + else: + if textlist: + seq.append({ 'type': 'TEXT', + 'content': ''.join(textlist) }) + textlist = [] + seq.append(tok) + elif tok['type'] == 'CTAG': self.ungetkn() @@ -681,3 +708,3 @@ class BaseWikiMarkup(object): elif tok['type'] == 'OTAG': - list.append(self.parse_til(tok)) + list.append(self.parse_tag(tok)) else: @@ -727,6 +754,8 @@ class BaseWikiMarkup(object): - def parse_til(self, tag): - self.dprint(80, "ENTER parse_til(%s)", tag) + def parse_tag(self, tag): + self.dprint(80, "ENTER parse_tag(%s)", tag) seq = [] save = self.tokind + t = self.peektkn() + isblock = t['type'] == 'NL' while 1: @@ -748,3 +777,3 @@ class BaseWikiMarkup(object): self.tree[self.tokind:self.tokind] = subtree - self.dprint(80, "LEAVE parse_til = %s (tree modified)", tag) + self.dprint(80, "LEAVE parse_tag = %s (tree modified)", tag) self.ungetkn() @@ -759,4 +788,5 @@ class BaseWikiMarkup(object): 'args': tag['args'], + 'isblock': isblock, 'content': { 'type': 'SEQ', 'content': seq } } - self.dprint(80, "LEAVE parse_til = %s", ret) + self.dprint(80, "LEAVE parse_tag = %s", ret) return ret @@ -764,2 +794,5 @@ class BaseWikiMarkup(object): def parse0(self): + if self.tokind == 0: + self.newline = True + return self.parse_para() tok = self.getkn() @@ -792,3 +825,3 @@ class BaseWikiMarkup(object): elif toktype == 'OTAG': - return self.parse_til(tok) + return self.parse_tag(tok) else: @@ -812,6 +845,2 @@ class BaseWikiMarkup(object): - if self.nested: - if self.tree[0]['type'] == 'PARA': - self.tree[0]['type'] = 'SEQ' - if self.debug_level >= 70: |