summaryrefslogtreecommitdiffabout
path: root/wikitrans/wiki2texi.py
Side-by-side diff
Diffstat (limited to 'wikitrans/wiki2texi.py') (more/less context) (ignore whitespace changes)
-rw-r--r--wikitrans/wiki2texi.py82
1 files changed, 44 insertions, 38 deletions
diff --git a/wikitrans/wiki2texi.py b/wikitrans/wiki2texi.py
index 55dffe2..936a133 100644
--- a/wikitrans/wiki2texi.py
+++ b/wikitrans/wiki2texi.py
@@ -1,17 +1,17 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2015-2018 Sergey Poznyakoff
-#
+#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
-#
+#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
-#
+#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -30,16 +30,17 @@ 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 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)
@@ -65,33 +66,35 @@ class Acc(list):
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
- if isinstance(self.content,list):
+ if isinstance(self.content, list):
for s in self.content:
parser._print(s)
else:
parser._print(self.content)
+
class TexiTagNode(WikiTagNode):
def format(self):
parser = self.parser
@@ -125,9 +128,10 @@ class TexiTagNode(WikiTagNode):
parser._print('>');
self.content.format()
parser._print('</' + self.tag + '>')
-
+
+
class TexiParaNode(WikiSeqNode):
- def format(self):
+ def format(self):
parser = self.parser
if not parser.acc.in_new_para():
parser._print('\n', nl=True)
@@ -135,7 +139,8 @@ class TexiParaNode(WikiSeqNode):
x.format()
if not parser.acc.in_new_para():
parser._print('\n', nl=True)
-
+
+
class TexiPreNode(WikiSeqNode):
def format(self):
parser = self.parser
@@ -146,6 +151,7 @@ class TexiPreNode(WikiSeqNode):
if not parser.nested:
parser._print('@end example\n', nl=True, escape=False)
+
class TexiFontNode(WikiSeqNode):
def format(self):
parser = self.parser
@@ -156,6 +162,7 @@ class TexiFontNode(WikiSeqNode):
x.format()
parser._print('}', escape=False)
+
class TexiHdrNode(WikiHdrNode):
def format(self):
parser = self.parser
@@ -174,10 +181,12 @@ class TexiHdrNode(WikiHdrNode):
parser._print('\n')
parser._print(None, nl=True)
+
class TexiBarNode(WikiNode):
def format(self):
self.parser._print("\n-----\n")
+
class TexiIndNode(WikiIndNode):
def format(self):
parser = self.parser
@@ -185,6 +194,7 @@ class TexiIndNode(WikiIndNode):
self.content.format()
parser._print(None, nl=True)
+
class TexiEnvNode(WikiEnvNode):
def format(self):
parser = self.parser
@@ -216,7 +226,8 @@ class TexiEnvNode(WikiEnvNode):
parser._print(None, nl=True)
parser._print('\n')
parser._print('@end table\n', nl=True, escape=False)
-
+
+
class TexiLinkNode(WikiSeqNode):
def format(self):
parser = self.parser
@@ -242,12 +253,13 @@ class TexiLinkNode(WikiSeqNode):
if len(s) > 1 and s[1] == 'thumb':
return
- (qual,sep,tgt) = arg.partition(':')
+ (qual, sep, tgt) = arg.partition(':')
if text:
parser._print("@ref{%s,%s}" % (qual, text), escape=False)
else:
parser._print("@ref{%s}" % qual, escape=False)
+
class TexiRefNode(WikiRefNode):
def format(self):
parser = self.parser
@@ -259,10 +271,11 @@ class TexiRefNode(WikiRefNode):
parser._print("@uref{%s,%s}" % (target, text), escape=False)
else:
parser._print("@uref{%s}" % target, escape=False)
-
+
+
class TexiWikiMarkup(WikiMarkup):
"""Wiki markup to Texinfo translator class.
-
+
Usage:
x = TexiWikiMarkup(file="input.wiki")
@@ -270,16 +283,16 @@ class TexiWikiMarkup(WikiMarkup):
x.parse()
# Print it as Texi:
print(str(x))
-
+
"""
-
+
nested = 0
sectcomm = {
'numbered': [
'@top',
- '@chapter',
- '@section',
- '@subsection',
+ '@chapter',
+ '@section',
+ '@subsection',
'@subsubsection'
],
'unnumbered': [
@@ -317,7 +330,7 @@ class TexiWikiMarkup(WikiMarkup):
For a discussion of generic arguments, see the constructor of
the WikiMarkup class.
-
+
Additional arguments:
sectioning_model=MODEL
@@ -342,9 +355,9 @@ class TexiWikiMarkup(WikiMarkup):
"@section A" on output. Now, if given "sectioning_start=1", this
directive will produce "@subsection A" instead.
"""
-
+
super(TexiWikiMarkup, self).__init__(*args, **keywords)
-
+
self.token_class['TEXT'] = TexiTextNode
self.token_class['TAG'] = TexiTagNode
self.token_class['PARA'] = TexiParaNode
@@ -357,7 +370,7 @@ class TexiWikiMarkup(WikiMarkup):
self.token_class['ENV'] = TexiEnvNode
self.token_class['LINK'] = TexiLinkNode
self.token_class['REF'] = TexiRefNode
-
+
if "sectioning_model" in keywords:
val = keywords["sectioning_model"]
if val in self.sectcomm:
@@ -394,17 +407,10 @@ class TexiWikiMarkup(WikiMarkup):
s = self.acc
self.acc = val
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.