4 # Authors: Garth Kidd <garth@deadlybloodyserious.com>;
5 # David Goodger <goodger@python.org>
6 # Copyright: This module has been placed in the public domain.
10 locale
.setlocale(locale
.LC_ALL
, '')
18 from docutils
.frontend
import OptionParser
19 from docutils
.utils
import new_document
20 from docutils
.parsers
.rst
import Parser
24 quicktest.py: Quickly test the reStructuredText parser. This is not an
25 interface to the full functionality of Docutils. Use one of the ``rst2*.py``
26 front-end tools instead.
30 quicktest.py [options] [<source> [<destination>]]
32 ``source`` is the name of the file to use as input (default is stdin).
33 ``destination`` is the name of the file to create as output (default is
39 options
= [('pretty', 'p',
40 'output pretty pseudo-xml: no "&abc;" entities (default)'),
41 ('test', 't', 'output test-ready data (input & expected output, '
42 'ready to be copied to a parser test module)'),
43 ('rawxml', 'r', 'output raw XML'),
44 ('styledxml=', 's', 'output raw XML with XSL style sheet '
45 'reference (filename supplied in the option argument)'),
46 ('xml', 'x', 'output pretty XML (indented)'),
47 ('attributes', 'A', 'dump document attributes after processing'),
48 ('debug', 'd', 'debug mode (lots of output)'),
49 ('version', 'V', 'show Docutils version then exit'),
50 ('help', 'h', 'show help text then exit')]
51 """See ``distutils.fancy_getopt.FancyGetopt.__init__`` for a description of
52 the data structure: (long option, short option, description)."""
56 for longopt
, shortopt
, description
in options
:
57 if longopt
[-1:] == '=':
58 opts
= '-%s arg, --%sarg' % (shortopt
, longopt
)
60 opts
= '-%s, --%s' % (shortopt
, longopt
)
64 while len(description
) > 60:
65 limit
= description
.rindex(' ', 0, 60)
66 print description
[:limit
].strip()
67 description
= description
[limit
+ 1:]
71 def _pretty(input, document
, optargs
):
72 return document
.pformat()
74 def _rawxml(input, document
, optargs
):
75 return document
.asdom().toxml()
77 def _styledxml(input, document
, optargs
):
78 docnode
= document
.asdom().childNodes
[0]
79 return '%s\n%s\n%s' % (
80 '<?xml version="1.0" encoding="ISO-8859-1"?>',
81 '<?xml-stylesheet type="text/xsl" href="%s"?>'
82 % optargs
['styledxml'], docnode
.toxml())
84 def _prettyxml(input, document
, optargs
):
85 return document
.asdom().toprettyxml(' ', '\n')
87 def _test(input, document
, optargs
):
89 output
= document
.pformat() # same as _pretty()
91 totest['change_this_test_name'] = [
99 """ % ( tq
, escape(input.rstrip()), tq
, tq
, escape(output
.rstrip()), tq
)
103 Return `text` in triple-double-quoted Python string form.
105 text
= text
.replace('\\', '\\\\') # escape backslashes
106 text
= text
.replace('"""', '""\\"') # break up triple-double-quotes
107 text
= text
.replace(' \n', ' \\n\\\n') # protect trailing whitespace
110 _outputFormatters
= {
112 'styledxml': _styledxml
,
118 def format(outputFormat
, input, document
, optargs
):
119 formatter
= _outputFormatters
[outputFormat
]
120 return formatter(input, document
, optargs
)
123 if os
.name
== 'mac' and len(sys
.argv
) <= 1:
126 return posixGetArgs(sys
.argv
[1:])
128 def posixGetArgs(argv
):
129 outputFormat
= 'pretty'
130 # convert fancy_getopt style option list to getopt.getopt() arguments
131 shortopts
= ''.join([option
[1] + ':' * (option
[0][-1:] == '=')
132 for option
in options
if option
[1]])
133 longopts
= [option
[0] for option
in options
if option
[0]]
135 opts
, args
= getopt
.getopt(argv
, shortopts
, longopts
)
136 except getopt
.GetoptError
:
139 optargs
= {'debug': 0, 'attributes': 0}
141 if o
in ['-h', '--help']:
144 elif o
in ['-V', '--version']:
145 print >>sys
.stderr
, ('quicktest.py (Docutils %s [%s])'
146 % (docutils
.__version
__,
147 docutils
.__version
_details
__))
149 elif o
in ['-r', '--rawxml']:
150 outputFormat
= 'rawxml'
151 elif o
in ['-s', '--styledxml']:
152 outputFormat
= 'styledxml'
153 optargs
['styledxml'] = a
154 elif o
in ['-x', '--xml']:
156 elif o
in ['-p', '--pretty']:
157 outputFormat
= 'pretty'
158 elif o
in ['-t', '--test']:
159 outputFormat
= 'test'
160 elif o
in ['--attributes', '-A']:
161 optargs
['attributes'] = 1
162 elif o
in ['-d', '--debug']:
165 raise getopt
.GetoptError
, "getopt should have saved us!"
167 print 'Maximum 2 arguments allowed.'
170 inputFile
= sys
.stdin
171 outputFile
= sys
.stdout
173 inputFile
= open(args
.pop(0))
175 outputFile
= open(args
.pop(0), 'w')
176 return inputFile
, outputFile
, outputFormat
, optargs
180 EasyDialogs
.Message("""\
181 Use the next dialog to build a command line:
183 1. Choose an output format from the [Option] list
185 3. Choose an input file: [Add existing file...]
186 4. Save the output: [Add new file...]
188 optionlist
= [(longopt
, description
)
189 for (longopt
, shortopt
, description
) in options
]
190 argv
= EasyDialogs
.GetArgv(optionlist
=optionlist
, addfolder
=0)
191 return posixGetArgs(argv
)
194 # process cmdline arguments:
195 inputFile
, outputFile
, outputFormat
, optargs
= getArgs()
196 settings
= OptionParser(components
=(Parser
,)).get_default_values()
197 settings
.debug
= optargs
['debug']
199 input = inputFile
.read()
200 document
= new_document(inputFile
.name
, settings
)
201 parser
.parse(input, document
)
202 output
= format(outputFormat
, input, document
, optargs
)
203 outputFile
.write(output
)
204 if optargs
['attributes']:
206 pprint
.pprint(document
.__dict
__)
209 if __name__
== '__main__':
210 sys
.stderr
= sys
.stdout