1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008,2015 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/>.
from __future__ import print_function
import sys
from optparse import OptionParser
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from WikiTrans.wiki2html import HtmlWikiMarkup, HtmlWiktionaryMarkup
from WikiTrans.wiki2text import TextWikiMarkup, TextWiktionaryMarkup
from WikiTrans.wiki2texi import TexiWikiMarkup
from WikiTrans.wikimarkup import WikiMarkup
class DumpWikiMarkup (WikiMarkup):
def __str__(self):
if self.tree:
s = StringIO()
self.dump(self.tree, 0, s)
return s.getvalue()
else:
return ""
handlers = {
'dump': {
'default': DumpWikiMarkup
},
'html': {
'default': HtmlWikiMarkup,
'wiktionary': HtmlWiktionaryMarkup
},
'text': {
'default': TextWikiMarkup,
'wiktionary': TextWiktionaryMarkup
},
'texi': {
'default': TexiWikiMarkup
}
}
def setkw(option, opt, value, parser):
if not parser.values.kwdict:
parser.values.kwdict = {}
(kw,sep,val) = value.partition('=')
if val:
parser.values.kwdict[kw] = val
def main():
usage = '%prog [OPTIONS] FILE'
version = '%prog 1.0'
description = """Translates MediaWiki documents markup to various other formats.
"""
epilog = "Report bugs to: <gray+wikitrans@gnu.org.ua>"
parser = OptionParser(usage=usage,
version=version,
description=description,
epilog=epilog)
parser.add_option('-v', '--verbose',
action="count", dest="verbose",
help="verbose operation")
parser.add_option('-I', '--input-type',
action='store', type='string', dest='itype',
default='default',
help='set input document type')
parser.add_option('-t', '--to', '--type',
action='store', type='string', dest='otype',
default='html',
help='set output document type')
parser.add_option('-l', '--lang',
action='store', type='string', dest='lang',
default='pl',
help='set input document language')
parser.add_option('-o', '--option',
action='callback', callback=setkw,
type='string', dest='kwdict',
default={},
help='set keyword option for the parser class')
parser.add_option('-d', '--debug',
action='store', type='int', dest='debug',
default=0,
help='set debug level (0..100)')
parser.add_option('-D', '--dump',
action='store_const', const='dump',
dest='otype',
help='dump parse tree and exit; similar to --type=dump')
(options, args) = parser.parse_args()
if len(args) == 1:
if args[0] == '-':
options.kwdict['file'] = sys.stdin
else:
options.kwdict['filename'] = args[0]
else:
parser.error("bad number of arguments")
options.kwdict['lang'] = options.lang # FIXME
if options.otype in handlers:
if options.itype in handlers[options.otype]:
markup = handlers[options.otype][options.itype](**options.kwdict)
markup.debug_level = options.debug
markup.parse()
print("%s" % str(markup))
exit(0)
else:
print("unsupported input type: %s" % options.itype)
else:
print("unsupported output type: %s" % options.otype)
exit(1)
if __name__ == '__main__':
main()
|