summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-08-04 17:01:33 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-08-04 17:06:38 (GMT)
commit4c0ff8e5b4f7000ca8db70bd8555a7e6a85875d0 (patch) (side-by-side diff)
treeed08c5d6b902f31f2945c2eee2a262861b43a1b1
parent338745d7852d660658dff572c86068a03b8a0b12 (diff)
downloadwikitrans-4c0ff8e5b4f7000ca8db70bd8555a7e6a85875d0.tar.gz
wikitrans-4c0ff8e5b4f7000ca8db70bd8555a7e6a85875d0.tar.bz2
Improve paragraph handling in texi translator
* WikiTrans/wiki2texi.py: Reimplement accumulator as a separate class.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--WikiTrans/wiki2texi.py69
1 files changed, 64 insertions, 5 deletions
diff --git a/WikiTrans/wiki2texi.py b/WikiTrans/wiki2texi.py
index 616fccb..f087c65 100644
--- a/WikiTrans/wiki2texi.py
+++ b/WikiTrans/wiki2texi.py
@@ -21,6 +21,59 @@ from WikiTrans.wikins import wiki_ns_re, wiki_ns
import re
import urllib
+class Acc(list):
+ def prepend(self,x):
+ self.insert(0,x)
+
+ def is_empty(self):
+ return len(self) == 0
+
+ def clear(self):
+ self = []
+
+ def tail(self, n = 1):
+ s = Acc()
+ i = len(self)
+ while i > 0 and n > 0:
+ elt = self[i-1]
+ l = len(elt)
+ if l == 0:
+ continue
+ elif l > n:
+ l = n
+ s.prepend(elt[-n:])
+ n -= l
+ i -= 1
+ return str(s)
+
+ def trim(self, n):
+ while len(self) and n > 0:
+ elt = self.pop()
+ l = len(elt)
+ if l == 0:
+ continue
+ elif l > n:
+ self += elt[0:-n]
+ break
+ n -= l
+
+ def trimnl(self):
+ if self.endswith('\n'):
+ self.trim(1)
+
+ def trimpara(self):
+ if self.endswith('\n\n'):
+ self.trim(2)
+
+ def endswith(self, x):
+ return self.tail(len(x)) == x
+
+ def in_new_para(self):
+ return self.is_empty() or self.endswith('\n\n')
+
+ def __str__(self):
+ return ''.join(self)
+
class TexiTextNode(WikiTextNode):
def format(self):
parser = self.parser
@@ -61,11 +114,11 @@ class TexiTagNode(WikiTagNode):
class TexiParaNode(WikiSeqNode):
def format(self):
parser = self.parser
- if not parser.acc.endswith('\n\n'):
+ if not parser.acc.in_new_para():
parser._print('\n', nl=True)
for x in self.content:
x.format()
- if not parser.acc.endswith('\n\n'):
+ if not parser.acc.in_new_para():
parser._print('\n', nl=True)
class TexiPreNode(WikiSeqNode):
@@ -258,7 +311,7 @@ class TexiWikiMarkup (WikiMarkup):
self.sectioning_start = val
replchars = re.compile(r'([@{}])')
- acc = None
+ acc = Acc()
def _print(self, text, **kw):
nl = kw.pop('nl', False)
@@ -271,20 +324,26 @@ class TexiWikiMarkup (WikiMarkup):
else:
self.acc += text
- def _begin_print(self, val = ''):
+ def swapacc(self, val = Acc()):
s = self.acc
self.acc = val
return s
+
+ def _begin_print(self):
+ s = self.acc
+ self.acc = Acc()
+ return s
def _end_print(self, val = None):
s = self.acc
self.acc = val
- return s
+ return str(s)
def __str__(self):
self._begin_print()
for elt in self.tree:
elt.format()
+ self.acc.trimpara()
return self._end_print()

Return to:

Send suggestions and report system problems to the System administrator.