diff options
Diffstat (limited to 'wikitrans/wiki2texi.py')
-rw-r--r-- | wikitrans/wiki2texi.py | 82 |
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 @@ | |||
1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
3 | # Copyright (C) 2015-2018 Sergey Poznyakoff | 3 | # Copyright (C) 2015-2018 Sergey Poznyakoff |
4 | # | 4 | # |
5 | # This program is free software; you can redistribute it and/or modify | 5 | # This program is free software; you can redistribute it and/or modify |
6 | # it under the terms of the GNU General Public License as published by | 6 | # it under the terms of the GNU General Public License as published by |
7 | # the Free Software Foundation; either version 3, or (at your option) | 7 | # the Free Software Foundation; either version 3, or (at your option) |
8 | # any later version. | 8 | # any later version. |
9 | # | 9 | # |
10 | # This program is distributed in the hope that it will be useful, | 10 | # This program is distributed in the hope that it will be useful, |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | # GNU General Public License for more details. | 13 | # GNU General Public License for more details. |
14 | # | 14 | # |
15 | # You should have received a copy of the GNU General Public License | 15 | # You should have received a copy of the GNU General Public License |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -30,16 +30,17 @@ from wikitrans.wikins import wiki_ns_re, wiki_ns | |||
30 | import re | 30 | import re |
31 | import urllib | 31 | import urllib |
32 | 32 | ||
33 | |||
33 | class Acc(list): | 34 | class Acc(list): |
34 | def prepend(self,x): | 35 | def prepend(self, x): |
35 | self.insert(0,x) | 36 | self.insert(0, x) |
36 | 37 | ||
37 | def is_empty(self): | 38 | def is_empty(self): |
38 | return len(self) == 0 | 39 | return len(self) == 0 |
39 | 40 | ||
40 | def clear(self): | 41 | def clear(self): |
41 | self = [] | 42 | self = [] |
42 | 43 | ||
43 | def tail(self, n = 1): | 44 | def tail(self, n = 1): |
44 | s = Acc() | 45 | s = Acc() |
45 | i = len(self) | 46 | i = len(self) |
@@ -65,33 +66,35 @@ class Acc(list): | |||
65 | self += elt[0:-n] | 66 | self += elt[0:-n] |
66 | break | 67 | break |
67 | n -= l | 68 | n -= l |
68 | 69 | ||
69 | def trimnl(self): | 70 | def trimnl(self): |
70 | if self.endswith('\n'): | 71 | if self.endswith('\n'): |
71 | self.trim(1) | 72 | self.trim(1) |
72 | 73 | ||
73 | def trimpara(self): | 74 | def trimpara(self): |
74 | if self.endswith('\n\n'): | 75 | if self.endswith('\n\n'): |
75 | self.trim(2) | 76 | self.trim(2) |
76 | 77 | ||
77 | def endswith(self, x): | 78 | def endswith(self, x): |
78 | return self.tail(len(x)) == x | 79 | return self.tail(len(x)) == x |
79 | 80 | ||
80 | def in_new_para(self): | 81 | def in_new_para(self): |
81 | return self.is_empty() or self.endswith('\n\n') | 82 | return self.is_empty() or self.endswith('\n\n') |
82 | 83 | ||
83 | def __str__(self): | 84 | def __str__(self): |
84 | return ''.join(self) | 85 | return ''.join(self) |
85 | 86 | ||
87 | |||
86 | class TexiTextNode(WikiTextNode): | 88 | class TexiTextNode(WikiTextNode): |
87 | def format(self): | 89 | def format(self): |
88 | parser = self.parser | 90 | parser = self.parser |
89 | if isinstance(self.content,list): | 91 | if isinstance(self.content, list): |
90 | for s in self.content: | 92 | for s in self.content: |
91 | parser._print(s) | 93 | parser._print(s) |
92 | else: | 94 | else: |
93 | parser._print(self.content) | 95 | parser._print(self.content) |
94 | 96 | ||
97 | |||
95 | class TexiTagNode(WikiTagNode): | 98 | class TexiTagNode(WikiTagNode): |
96 | def format(self): | 99 | def format(self): |
97 | parser = self.parser | 100 | parser = self.parser |
@@ -125,9 +128,10 @@ class TexiTagNode(WikiTagNode): | |||
125 | parser._print('>'); | 128 | parser._print('>'); |
126 | self.content.format() | 129 | self.content.format() |
127 | parser._print('</' + self.tag + '>') | 130 | parser._print('</' + self.tag + '>') |
128 | 131 | ||
132 | |||
129 | class TexiParaNode(WikiSeqNode): | 133 | class TexiParaNode(WikiSeqNode): |
130 | def format(self): | 134 | def format(self): |
131 | parser = self.parser | 135 | parser = self.parser |
132 | if not parser.acc.in_new_para(): | 136 | if not parser.acc.in_new_para(): |
133 | parser._print('\n', nl=True) | 137 | parser._print('\n', nl=True) |
@@ -135,7 +139,8 @@ class TexiParaNode(WikiSeqNode): | |||
135 | x.format() | 139 | x.format() |
136 | if not parser.acc.in_new_para(): | 140 | if not parser.acc.in_new_para(): |
137 | parser._print('\n', nl=True) | 141 | parser._print('\n', nl=True) |
138 | 142 | ||
143 | |||
139 | class TexiPreNode(WikiSeqNode): | 144 | class TexiPreNode(WikiSeqNode): |
140 | def format(self): | 145 | def format(self): |
141 | parser = self.parser | 146 | parser = self.parser |
@@ -146,6 +151,7 @@ class TexiPreNode(WikiSeqNode): | |||
146 | if not parser.nested: | 151 | if not parser.nested: |
147 | parser._print('@end example\n', nl=True, escape=False) | 152 | parser._print('@end example\n', nl=True, escape=False) |
148 | 153 | ||
154 | |||
149 | class TexiFontNode(WikiSeqNode): | 155 | class TexiFontNode(WikiSeqNode): |
150 | def format(self): | 156 | def format(self): |
151 | parser = self.parser | 157 | parser = self.parser |
@@ -156,6 +162,7 @@ class TexiFontNode(WikiSeqNode): | |||
156 | x.format() | 162 | x.format() |
157 | parser._print('}', escape=False) | 163 | parser._print('}', escape=False) |
158 | 164 | ||
165 | |||
159 | class TexiHdrNode(WikiHdrNode): | 166 | class TexiHdrNode(WikiHdrNode): |
160 | def format(self): | 167 | def format(self): |
161 | parser = self.parser | 168 | parser = self.parser |
@@ -174,10 +181,12 @@ class TexiHdrNode(WikiHdrNode): | |||
174 | parser._print('\n') | 181 | parser._print('\n') |
175 | parser._print(None, nl=True) | 182 | parser._print(None, nl=True) |
176 | 183 | ||
184 | |||
177 | class TexiBarNode(WikiNode): | 185 | class TexiBarNode(WikiNode): |
178 | def format(self): | 186 | def format(self): |
179 | self.parser._print("\n-----\n") | 187 | self.parser._print("\n-----\n") |
180 | 188 | ||
189 | |||
181 | class TexiIndNode(WikiIndNode): | 190 | class TexiIndNode(WikiIndNode): |
182 | def format(self): | 191 | def format(self): |
183 | parser = self.parser | 192 | parser = self.parser |
@@ -185,6 +194,7 @@ class TexiIndNode(WikiIndNode): | |||
185 | self.content.format() | 194 | self.content.format() |
186 | parser._print(None, nl=True) | 195 | parser._print(None, nl=True) |
187 | 196 | ||
197 | |||
188 | class TexiEnvNode(WikiEnvNode): | 198 | class TexiEnvNode(WikiEnvNode): |
189 | def format(self): | 199 | def format(self): |
190 | parser = self.parser | 200 | parser = self.parser |
@@ -216,7 +226,8 @@ class TexiEnvNode(WikiEnvNode): | |||
216 | parser._print(None, nl=True) | 226 | parser._print(None, nl=True) |
217 | parser._print('\n') | 227 | parser._print('\n') |
218 | parser._print('@end table\n', nl=True, escape=False) | 228 | parser._print('@end table\n', nl=True, escape=False) |
219 | 229 | ||
230 | |||
220 | class TexiLinkNode(WikiSeqNode): | 231 | class TexiLinkNode(WikiSeqNode): |
221 | def format(self): | 232 | def format(self): |
222 | parser = self.parser | 233 | parser = self.parser |
@@ -242,12 +253,13 @@ class TexiLinkNode(WikiSeqNode): | |||
242 | if len(s) > 1 and s[1] == 'thumb': | 253 | if len(s) > 1 and s[1] == 'thumb': |
243 | return | 254 | return |
244 | 255 | ||
245 | (qual,sep,tgt) = arg.partition(':') | 256 | (qual, sep, tgt) = arg.partition(':') |
246 | if text: | 257 | if text: |
247 | parser._print("@ref{%s,%s}" % (qual, text), escape=False) | 258 | parser._print("@ref{%s,%s}" % (qual, text), escape=False) |
248 | else: | 259 | else: |
249 | parser._print("@ref{%s}" % qual, escape=False) | 260 | parser._print("@ref{%s}" % qual, escape=False) |
250 | 261 | ||
262 | |||
251 | class TexiRefNode(WikiRefNode): | 263 | class TexiRefNode(WikiRefNode): |
252 | def format(self): | 264 | def format(self): |
253 | parser = self.parser | 265 | parser = self.parser |
@@ -259,10 +271,11 @@ class TexiRefNode(WikiRefNode): | |||
259 | parser._print("@uref{%s,%s}" % (target, text), escape=False) | 271 | parser._print("@uref{%s,%s}" % (target, text), escape=False) |
260 | else: | 272 | else: |
261 | parser._print("@uref{%s}" % target, escape=False) | 273 | parser._print("@uref{%s}" % target, escape=False) |
262 | 274 | ||
275 | |||
263 | class TexiWikiMarkup(WikiMarkup): | 276 | class TexiWikiMarkup(WikiMarkup): |
264 | """Wiki markup to Texinfo translator class. | 277 | """Wiki markup to Texinfo translator class. |
265 | 278 | ||
266 | Usage: | 279 | Usage: |
267 | 280 | ||
268 | x = TexiWikiMarkup(file="input.wiki") | 281 | x = TexiWikiMarkup(file="input.wiki") |
@@ -270,16 +283,16 @@ class TexiWikiMarkup(WikiMarkup): | |||
270 | x.parse() | 283 | x.parse() |
271 | # Print it as Texi: | 284 | # Print it as Texi: |
272 | print(str(x)) | 285 |