OLD | NEW |
| (Empty) |
1 """ | |
2 COMMAND-LINE SPECIFIC STUFF | |
3 ============================================================================= | |
4 | |
5 """ | |
6 | |
7 import markdown | |
8 import sys | |
9 import optparse | |
10 | |
11 import logging | |
12 from logging import DEBUG, INFO, CRITICAL | |
13 | |
14 logger = logging.getLogger('MARKDOWN') | |
15 | |
16 def parse_options(): | |
17 """ | |
18 Define and parse `optparse` options for command-line usage. | |
19 """ | |
20 usage = """%prog [options] [INPUTFILE] | |
21 (STDIN is assumed if no INPUTFILE is given)""" | |
22 desc = "A Python implementation of John Gruber's Markdown. " \ | |
23 "http://packages.python.org/Markdown/" | |
24 ver = "%%prog %s" % markdown.version | |
25 | |
26 parser = optparse.OptionParser(usage=usage, description=desc, version=ver) | |
27 parser.add_option("-f", "--file", dest="filename", default=None, | |
28 help="Write output to OUTPUT_FILE. Defaults to STDOUT.", | |
29 metavar="OUTPUT_FILE") | |
30 parser.add_option("-e", "--encoding", dest="encoding", | |
31 help="Encoding for input and output files.",) | |
32 parser.add_option("-q", "--quiet", default = CRITICAL, | |
33 action="store_const", const=CRITICAL+10, dest="verbose", | |
34 help="Suppress all warnings.") | |
35 parser.add_option("-v", "--verbose", | |
36 action="store_const", const=INFO, dest="verbose", | |
37 help="Print all warnings.") | |
38 parser.add_option("-s", "--safe", dest="safe", default=False, | |
39 metavar="SAFE_MODE", | |
40 help="'replace', 'remove' or 'escape' HTML tags in input") | |
41 parser.add_option("-o", "--output_format", dest="output_format", | |
42 default='xhtml1', metavar="OUTPUT_FORMAT", | |
43 help="'xhtml1' (default), 'html4' or 'html5'.") | |
44 parser.add_option("--noisy", | |
45 action="store_const", const=DEBUG, dest="verbose", | |
46 help="Print debug messages.") | |
47 parser.add_option("-x", "--extension", action="append", dest="extensions", | |
48 help = "Load extension EXTENSION.", metavar="EXTENSION") | |
49 parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", | |
50 action='store_false', default=True, | |
51 help="Observe number of first item of ordered lists.") | |
52 | |
53 (options, args) = parser.parse_args() | |
54 | |
55 if len(args) == 0: | |
56 input_file = None | |
57 else: | |
58 input_file = args[0] | |
59 | |
60 if not options.extensions: | |
61 options.extensions = [] | |
62 | |
63 return {'input': input_file, | |
64 'output': options.filename, | |
65 'safe_mode': options.safe, | |
66 'extensions': options.extensions, | |
67 'encoding': options.encoding, | |
68 'output_format': options.output_format, | |
69 'lazy_ol': options.lazy_ol}, options.verbose | |
70 | |
71 def run(): | |
72 """Run Markdown from the command line.""" | |
73 | |
74 # Parse options and adjust logging level if necessary | |
75 options, logging_level = parse_options() | |
76 if not options: sys.exit(2) | |
77 logger.setLevel(logging_level) | |
78 logger.addHandler(logging.StreamHandler()) | |
79 | |
80 # Run | |
81 markdown.markdownFromFile(**options) | |
82 | |
83 if __name__ == '__main__': | |
84 # Support running module as a commandline command. | |
85 # Python 2.5 & 2.6 do: `python -m markdown.__main__ [options] [args]`. | |
86 # Python 2.7 & 3.x do: `python -m markdown [options] [args]`. | |
87 run() | |
OLD | NEW |