OLD | NEW |
(Empty) | |
| 1 """ |
| 2 COMMAND-LINE SPECIFIC STUFF |
| 3 ============================================================================= |
| 4 |
| 5 """ |
| 6 |
| 7 import sys |
| 8 import optparse |
| 9 import codecs |
| 10 import warnings |
| 11 import markdown |
| 12 try: |
| 13 import yaml |
| 14 except ImportError: # pragma: no cover |
| 15 import json as yaml |
| 16 |
| 17 import logging |
| 18 from logging import DEBUG, WARNING, CRITICAL |
| 19 |
| 20 logger = logging.getLogger('MARKDOWN') |
| 21 |
| 22 |
| 23 def parse_options(args=None, values=None): |
| 24 """ |
| 25 Define and parse `optparse` options for command-line usage. |
| 26 """ |
| 27 usage = """%prog [options] [INPUTFILE] |
| 28 (STDIN is assumed if no INPUTFILE is given)""" |
| 29 desc = "A Python implementation of John Gruber's Markdown. " \ |
| 30 "https://pythonhosted.org/Markdown/" |
| 31 ver = "%%prog %s" % markdown.version |
| 32 |
| 33 parser = optparse.OptionParser(usage=usage, description=desc, version=ver) |
| 34 parser.add_option("-f", "--file", dest="filename", default=None, |
| 35 help="Write output to OUTPUT_FILE. Defaults to STDOUT.", |
| 36 metavar="OUTPUT_FILE") |
| 37 parser.add_option("-e", "--encoding", dest="encoding", |
| 38 help="Encoding for input and output files.",) |
| 39 parser.add_option("-s", "--safe", dest="safe", default=False, |
| 40 metavar="SAFE_MODE", |
| 41 help="Deprecated! 'replace', 'remove' or 'escape' HTML " |
| 42 "tags in input") |
| 43 parser.add_option("-o", "--output_format", dest="output_format", |
| 44 default='xhtml1', metavar="OUTPUT_FORMAT", |
| 45 help="'xhtml1' (default), 'html4' or 'html5'.") |
| 46 parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", |
| 47 action='store_false', default=True, |
| 48 help="Observe number of first item of ordered lists.") |
| 49 parser.add_option("-x", "--extension", action="append", dest="extensions", |
| 50 help="Load extension EXTENSION.", metavar="EXTENSION") |
| 51 parser.add_option("-c", "--extension_configs", |
| 52 dest="configfile", default=None, |
| 53 help="Read extension configurations from CONFIG_FILE. " |
| 54 "CONFIG_FILE must be of JSON or YAML format. YAML" |
| 55 "format requires that a python YAML library be " |
| 56 "installed. The parsed JSON or YAML must result in a " |
| 57 "python dictionary which would be accepted by the " |
| 58 "'extension_configs' keyword on the markdown.Markdown " |
| 59 "class. The extensions must also be loaded with the " |
| 60 "`--extension` option.", |
| 61 metavar="CONFIG_FILE") |
| 62 parser.add_option("-q", "--quiet", default=CRITICAL, |
| 63 action="store_const", const=CRITICAL+10, dest="verbose", |
| 64 help="Suppress all warnings.") |
| 65 parser.add_option("-v", "--verbose", |
| 66 action="store_const", const=WARNING, dest="verbose", |
| 67 help="Print all warnings.") |
| 68 parser.add_option("--noisy", |
| 69 action="store_const", const=DEBUG, dest="verbose", |
| 70 help="Print debug messages.") |
| 71 |
| 72 (options, args) = parser.parse_args(args, values) |
| 73 |
| 74 if len(args) == 0: |
| 75 input_file = None |
| 76 else: |
| 77 input_file = args[0] |
| 78 |
| 79 if not options.extensions: |
| 80 options.extensions = [] |
| 81 |
| 82 extension_configs = {} |
| 83 if options.configfile: |
| 84 with codecs.open( |
| 85 options.configfile, mode="r", encoding=options.encoding |
| 86 ) as fp: |
| 87 try: |
| 88 extension_configs = yaml.load(fp) |
| 89 except Exception as e: |
| 90 message = "Failed parsing extension config file: %s" % \ |
| 91 options.configfile |
| 92 e.args = (message,) + e.args[1:] |
| 93 raise |
| 94 |
| 95 opts = { |
| 96 'input': input_file, |
| 97 'output': options.filename, |
| 98 'extensions': options.extensions, |
| 99 'extension_configs': extension_configs, |
| 100 'encoding': options.encoding, |
| 101 'output_format': options.output_format, |
| 102 'lazy_ol': options.lazy_ol |
| 103 } |
| 104 |
| 105 if options.safe: |
| 106 # Avoid deprecation warning if user didn't set option |
| 107 opts['safe_mode'] = options.safe |
| 108 |
| 109 return opts, options.verbose |
| 110 |
| 111 |
| 112 def run(): # pragma: no cover |
| 113 """Run Markdown from the command line.""" |
| 114 |
| 115 # Parse options and adjust logging level if necessary |
| 116 options, logging_level = parse_options() |
| 117 if not options: |
| 118 sys.exit(2) |
| 119 logger.setLevel(logging_level) |
| 120 console_handler = logging.StreamHandler() |
| 121 logger.addHandler(console_handler) |
| 122 if logging_level <= WARNING: |
| 123 # Ensure deprecation warnings get displayed |
| 124 warnings.filterwarnings('default') |
| 125 logging.captureWarnings(True) |
| 126 warn_logger = logging.getLogger('py.warnings') |
| 127 warn_logger.addHandler(console_handler) |
| 128 |
| 129 # Run |
| 130 markdown.markdownFromFile(**options) |
| 131 |
| 132 |
| 133 if __name__ == '__main__': # pragma: no cover |
| 134 # Support running module as a commandline command. |
| 135 # Python 2.7 & 3.x do: `python -m markdown [options] [args]`. |
| 136 run() |
OLD | NEW |