summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2015-07-20 10:43:55 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2015-07-20 10:43:55 (GMT)
commitc41c19ff0b19cbca7c850239bfa11295a674f520 (patch) (side-by-side diff)
tree4336a509242f00cd4910f949fdb45199266566ec
parent63f5f9902f83bd65fd2a37239ab9d6e5876924fd (diff)
downloadwikitrans-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.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--WikiTrans/wiki2texi.py12
-rw-r--r--WikiTrans/wikimarkup.py57
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
@@ -138,24 +138,18 @@ class TexiWikiMarkup (WikiMarkup):
self._print(str(elt))
def str_tag(self, elt):
- if elt['tag'] == 'code':
+ if elt['tag'] in ['code', 'tt']:
save = self._begin_print()
self.nested += 1
self.format(elt['content'])
self.nested -= 1
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)
else:
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':
if 'args' in elt and 'id' in elt['args']:
self._print("@anchor{%s}\n" % elt['args']['id'],
diff --git a/WikiTrans/wikimarkup.py b/WikiTrans/wikimarkup.py
index ba30269..d56c664 100644
--- a/WikiTrans/wikimarkup.py
+++ b/WikiTrans/wikimarkup.py
@@ -137,6 +137,9 @@ class BaseWikiMarkup(object):
def dump_tag(self, node, level, file):
self.print_dump_prefix(level, file)
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:
self.print_dump_prefix(level, file)
file.write("ARGS: %s\n" % node['args'])
@@ -509,7 +512,7 @@ class BaseWikiMarkup(object):
self.dprint(80, "LEAVE parse_ref=None")
return None
elif tok['type'] == 'OTAG':
- list.append(self.parse_til(tok))
+ list.append(self.parse_tag(tok))
else:
seq.append(tok)
@@ -573,7 +576,7 @@ class BaseWikiMarkup(object):
tok = self.peektkn()
if self.newline:
- if re.match("^\s", tok['content']):
+ if 'content' in tok and re.match("^\s", tok['content']):
type = 'PRE'
rx = re.compile("^\S")
else:
@@ -582,7 +585,7 @@ class BaseWikiMarkup(object):
else:
type = 'SEQ'
rx = None
-
+ self.dprint(80, "IN parse_para, type %s", type)
while 1:
tok = self.getkn()
if tok['type'] == 'TEXT':
@@ -601,7 +604,31 @@ class BaseWikiMarkup(object):
textlist.append('\n')
elif tok['type'] == 'NIL':
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()
break
elif tok['type'] == 'DELIM':
@@ -679,7 +706,7 @@ class BaseWikiMarkup(object):
else:
list.append(tok)
elif tok['type'] == 'OTAG':
- list.append(self.parse_til(tok))
+ list.append(self.parse_tag(tok))
else:
list.append(tok)
self.dprint(80, "LEAVE parse_line=(SEQ, %s)", list)
@@ -725,10 +752,12 @@ class BaseWikiMarkup(object):
self.dprint(80, "LEAVE parse_indent=%s", x)
return x
- 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:
t = self.parse0()
if t == None or t['type'] == 'NIL':
@@ -746,7 +775,7 @@ class BaseWikiMarkup(object):
tag['content'] = s
if subtree:
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()
return self.parse0()
@@ -757,11 +786,15 @@ class BaseWikiMarkup(object):
ret = { 'type': 'TAG',
'tag': tag['tag'],
'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
def parse0(self):
+ if self.tokind == 0:
+ self.newline = True
+ return self.parse_para()
tok = self.getkn()
self.dprint(80, "ENTER parse0(%s)", tok)
toktype = tok['type']
@@ -790,7 +823,7 @@ class BaseWikiMarkup(object):
elif toktype == 'NL':
return { 'type': 'TEXT', 'content': '\n' }
elif toktype == 'OTAG':
- return self.parse_til(tok)
+ return self.parse_tag(tok)
else:
return tok
@@ -810,10 +843,6 @@ class BaseWikiMarkup(object):
break
self.tree.append(subtree)
- if self.nested:
- if self.tree[0]['type'] == 'PARA':
- self.tree[0]['type'] = 'SEQ'
-
if self.debug_level >= 70:
print("TREE DUMP BEGIN")
self.dump(self.tree)

Return to:

Send suggestions and report system problems to the System administrator.