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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#!/usr/bin/env python
"""
:Author: Garth Kidd
:Contact: garth@deadlybloodyserious.com
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:Revision: $Revision$
:Date: $Date$
:Copyright: This module has been placed in the public domain.
"""
import sys, os, getopt
import docutils.utils
from docutils.parsers.rst import Parser
usage_header = """\
quicktest.py: quickly test the restructuredtext parser.
Usage::
quicktest.py [options] [filename]
``filename`` is the name of the file to use as input (default is stdin).
Options:
"""
options = [('pretty', 'p',
'output pretty pseudo-xml: no "&abc;" entities (default)'),
('test', 't', 'output test-ready data (input & expected output, '
'ready to be copied to a parser test module)'),
('rawxml', 'r', 'output raw XML'),
('styledxml=', 's', 'output raw XML with XSL style sheet reference '
'(filename supplied in the option argument)'),
('xml', 'x', 'output pretty XML (indented)'),
('debug', 'd', 'debug mode (lots of output)'),
('help', 'h', 'show help text')]
"""See distutils.fancy_getopt.FancyGetopt.__init__ for a description of the
data structure: (long option, short option, description)."""
def usage():
print usage_header
for longopt, shortopt, description in options:
if longopt[-1:] == '=':
opts = '-%s arg, --%sarg' % (shortopt, longopt)
else:
opts = '-%s, --%s' % (shortopt, longopt),
print '%-15s' % opts,
if len(opts) > 14:
print '%-16s' % '\n',
while len(description) > 60:
limit = description.rindex(' ', 0, 60)
print description[:limit].strip()
description = description[limit + 1:]
print '%-15s' % ' ',
print description
def _pretty(input, document, optargs):
return document.pformat()
def _rawxml(input, document, optargs):
return document.asdom().toxml()
def _styledxml(input, document, optargs):
docnode = document.asdom().childNodes[0]
return '%s\n%s\n%s' % (
'<?xml version="1.0" encoding="ISO-8859-1"?>',
'<?xml-stylesheet type="text/xsl" href="%s"?>' % optargs['styledxml'],
docnode.toxml())
def _prettyxml(input, document, optargs):
return document.asdom().toprettyxml(' ', '\n')
def _test(input, document, optargs):
tq = '"""'
output = document.pformat() # same as _pretty()
return """\
totest['change_this_test_name'] = [
[%s\\
%s
%s,
%s\\
%s
%s],
]
""" % ( tq, escape(input.rstrip()), tq, tq, escape(output.rstrip()), tq )
def escape(text):
"""
Return `text` in a form compatible with triple-double-quoted Python strings.
"""
text = text.replace('\\', '\\\\') # escape backslashes
text = text.replace('"""', '""\\"') # break up triple-double-quotes
text = text.replace(' \n', ' \\n\\\n') # protect trailing whitespace
return text
_outputFormatters = {
'rawxml': _rawxml,
'styledxml': _styledxml,
'xml': _prettyxml,
'pretty' : _pretty,
'test': _test
}
def format(outputFormat, input, document, optargs):
formatter = _outputFormatters[outputFormat]
return formatter(input, document, optargs)
def getArgs():
if os.name == 'mac' and len(sys.argv) <= 1:
return macGetArgs()
else:
return posixGetArgs(sys.argv[1:])
def posixGetArgs(argv):
outputFormat = 'pretty'
# convert fancy_getopt style option list to getopt.getopt() arguments
shortopts = ''.join([option[1] + ':' * (option[0][-1:] == '=')
for option in options if option[1]])
longopts = [option[0] for option in options if option[0]]
try:
opts, args = getopt.getopt(argv, shortopts, longopts)
except getopt.GetoptError:
usage()
sys.exit(2)
optargs = {'debug': 0}
for o, a in opts:
if o in ['-h', '--help']:
usage()
sys.exit()
elif o in ['-r', '--rawxml']:
outputFormat = 'rawxml'
elif o in ['-s', '--styledxml']:
outputFormat = 'styledxml'
optargs['styledxml'] = a
elif o in ['-x', '--xml']:
outputFormat = 'xml'
elif o in ['-p', '--pretty']:
outputFormat = 'pretty'
elif o in ['-t', '--test']:
outputFormat = 'test'
elif o in ['-d', '--debug']:
optargs['debug'] = 1
else:
raise getopt.GetoptError, "getopt should have saved us!"
if len(args) > 1:
print "Only one file at a time, thanks."
usage()
sys.exit(1)
if len(args) == 1:
inputFile = open(args[0])
else:
inputFile = sys.stdin
return inputFile, outputFormat, optargs
def macGetArgs():
import EasyDialogs
EasyDialogs.Message("""\
In the following window, please:
1. Choose an output format from the "Option" list.
2. Click "Add" (if you don't, the default format will
be "pretty").
3. Click "Add existing file..." and choose an input file.
4. Click "OK".""")
optionlist = [(longopt, description)
for (longopt, shortopt, description) in options]
argv = EasyDialogs.GetArgv(optionlist=optionlist, addnewfile=0, addfolder=0)
return posixGetArgs(argv)
def main():
inputFile, outputFormat, optargs = getArgs() # process cmdline arguments
parser = Parser()
input = inputFile.read()
document = docutils.utils.newdocument(debug=optargs['debug'])
parser.parse(input, document)
output = format(outputFormat, input, document, optargs)
print output,
if __name__ == '__main__':
sys.stderr = sys.stdout
main()
|