diff options
author | Yuri Takhteyev <yuri@freewisdom.org> | 2008-11-17 00:17:15 -0800 |
---|---|---|
committer | Yuri Takhteyev <yuri@freewisdom.org> | 2008-11-17 00:17:15 -0800 |
commit | 159a274a977c496434dbc484a1b253663cde4eed (patch) | |
tree | 53c9a6d3c69cbb8be3e6b47ea0d35cab075f03c9 /markdown/commandline.py | |
parent | 3dfcbc8d7900aa0f07124c9d7598cb7ecc2ff41b (diff) | |
download | markdown-159a274a977c496434dbc484a1b253663cde4eed.tar.gz markdown-159a274a977c496434dbc484a1b253663cde4eed.tar.bz2 markdown-159a274a977c496434dbc484a1b253663cde4eed.zip |
Attempting a refactoring, breaking markdown into multiple files.
Diffstat (limited to 'markdown/commandline.py')
-rw-r--r-- | markdown/commandline.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/markdown/commandline.py b/markdown/commandline.py new file mode 100644 index 0000000..9b1a1bf --- /dev/null +++ b/markdown/commandline.py @@ -0,0 +1,97 @@ +""" +COMMAND-LINE SPECIFIC STUFF +============================================================================= + +The rest of the code is specifically for handling the case where Python +Markdown is called from the command line. +""" + +import markdown +import sys +import logging +from logging import DEBUG, INFO, WARN, ERROR, CRITICAL + +EXECUTABLE_NAME_FOR_USAGE = "python markdown.py" +""" The name used in the usage statement displayed for python versions < 2.3. +(With python 2.3 and higher the usage statement is generated by optparse +and uses the actual name of the executable called.) """ + +OPTPARSE_WARNING = """ +Python 2.3 or higher required for advanced command line options. +For lower versions of Python use: + + %s INPUT_FILE > OUTPUT_FILE + +""" % EXECUTABLE_NAME_FOR_USAGE + +def parse_options(): + """ + Define and parse `optparse` options for command-line usage. + """ + + try: + optparse = __import__("optparse") + except: + if len(sys.argv) == 2: + return {'input': sys.argv[1], + 'output': None, + 'safe': False, + 'extensions': [], + 'encoding': None }, CRITICAL + else: + print OPTPARSE_WARNING + return None, None + + parser = optparse.OptionParser(usage="%prog INPUTFILE [options]") + parser.add_option("-f", "--file", dest="filename", default=sys.stdout, + help="write output to OUTPUT_FILE", + metavar="OUTPUT_FILE") + parser.add_option("-e", "--encoding", dest="encoding", + help="encoding for input and output files",) + parser.add_option("-q", "--quiet", default = CRITICAL, + action="store_const", const=CRITICAL+10, dest="verbose", + help="suppress all messages") + parser.add_option("-v", "--verbose", + action="store_const", const=INFO, dest="verbose", + help="print info messages") + parser.add_option("-s", "--safe", dest="safe", default=False, + metavar="SAFE_MODE", + help="safe mode ('replace', 'remove' or 'escape' user's HTML tag)") + parser.add_option("--noisy", + action="store_const", const=DEBUG, dest="verbose", + help="print debug messages") + parser.add_option("-x", "--extension", action="append", dest="extensions", + help = "load extension EXTENSION", metavar="EXTENSION") + + (options, args) = parser.parse_args() + + if not len(args) == 1: + parser.print_help() + return None, None + else: + input_file = args[0] + + if not options.extensions: + options.extensions = [] + + return {'input': input_file, + 'output': options.filename, + 'safe': options.safe, + 'extensions': options.extensions, + 'encoding': options.encoding }, options.verbose + +def run(): + """Run Markdown from the command line.""" + + # Setup a logger manually for compatibility with Python 2.3 + logger = logging.getLogger('MARKDOWN') + logger.setLevel(markdown.COMMAND_LINE_LOGGING_LEVEL) + logger.addHandler(logging.StreamHandler()) + + # Parse options and adjust logging level if necessary + options, logging_level = parse_options() + if not options: sys.exit(0) + if logging_level: logging.getLogger('MARKDOWN').setLevel(logging_level) + + # Run + markdown.markdownFromFile(**options) |