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,20 +1,20 @@ #!/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/>. """ Wiki markup to Texinfo translator. @@ -27,22 +27,23 @@ TexiWikiMarkup -- Converts Wiki material to Texinfo. from wikitrans.wikimarkup import * from wikitrans.wikitoken import * 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) while i > 0 and n > 0: elt = self[i-1] l = len(elt) @@ -62,39 +63,41 @@ class Acc(list): 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 - 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 if self.tag in ['code', 'tt']: save = parser._begin_print() parser.nested += 1 @@ -122,43 +125,47 @@ class TexiTagNode(WikiTagNode): parser._print('<' + self.tag) if self.args: parser._print(' ' + self.args) 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) for x in self.content: x.format() if not parser.acc.in_new_para(): parser._print('\n', nl=True) - + + class TexiPreNode(WikiSeqNode): def format(self): parser = self.parser if not parser.nested: parser._print('@example\n', nl=True, escape=False) for x in self.content: x.format() if not parser.nested: parser._print('@end example\n', nl=True, escape=False) + class TexiFontNode(WikiSeqNode): def format(self): parser = self.parser comm = { 'IT': 'i', 'BOLD': 'b' } parser._print('@%s{' % comm[self.type], escape=False) for x in self.content: x.format() parser._print('}', escape=False) + class TexiHdrNode(WikiHdrNode): def format(self): parser = self.parser level = self.level # FIXME if level > len(parser.sectcomm[parser.sectioning_model]) - 1 - parser.sectioning_start: @@ -171,23 +178,26 @@ class TexiHdrNode(WikiHdrNode): if parser.sectcomm[parser.sectioning_model][0] == '@top': parser._print('@node ', nl=True, escape=False) self.content.format() 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 parser._print("@w{ }" * self.level, nl=True, escape=False) self.content.format() parser._print(None, nl=True) + class TexiEnvNode(WikiEnvNode): def format(self): parser = self.parser if self.envtype == 'unnumbered': parser._print('@itemize @bullet\n', nl=True, escape=False) for s in self.content: @@ -213,13 +223,14 @@ class TexiEnvNode(WikiEnvNode): parser._print(None, nl=True) else: s.content.format() 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 save = parser._begin_print() self.content[0].format() arg = parser._end_print() @@ -239,50 +250,52 @@ class TexiLinkNode(WikiSeqNode): if s: if s[0] == 'disambigR' or s[0] == 'wikiquote': return 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 target = self.ref save = parser._begin_print() self.content.format() text = parser._end_print(save) if text and text != '': 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") # Parse the input: x.parse() # Print it as Texi: print(str(x)) - + """ - + nested = 0 sectcomm = { 'numbered': [ '@top', - '@chapter', - '@section', - '@subsection', + '@chapter', + '@section', + '@subsection', '@subsubsection' ], 'unnumbered': [ '@top', '@unnumbered', '@unnumberedsec', @@ -314,13 +327,13 @@ class TexiWikiMarkup(WikiMarkup): TexiWikiMarkup([filename=FILE],[file=FD],[text=STRING],[lang=CODE], [html_base=URL],[image_base=URL],[media_base=URL], [sectioning_model=MODEL],[sectioning_start=N]) For a discussion of generic arguments, see the constructor of the WikiMarkup class. - + Additional arguments: sectioning_model=MODEL Select the Texinfo sectioning model for the output document. Possible values are: @@ -339,28 +352,28 @@ class TexiWikiMarkup(WikiMarkup): sectioning_start=N Shift resulting heading level by N positions. For example, supposing "sectioning_model='numbered'", "== A ==" normally produces "@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 self.token_class['PRE'] = TexiPreNode self.token_class['IT'] = TexiFontNode self.token_class['BOLD'] = TexiFontNode self.token_class['HDR'] = TexiHdrNode self.token_class['BAR'] = TexiBarNode self.token_class['IND'] = TexiIndNode 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: self.sectioning_model = val else: raise ValueError("Invalid value for sectioning model: %s" % val) @@ -391,20 +404,13 @@ class TexiWikiMarkup(WikiMarkup): return s def _end_print(self, val = None): 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() - - - - - - - |