1# markdown is released under the BSD license 2# Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later) 3# Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) 4# Copyright 2004 Manfred Stienstra (the original version) 5# 6# All rights reserved. 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions are met: 10# 11# * Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# * Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# * Neither the name of the <organization> nor the 17# names of its contributors may be used to endorse or promote products 18# derived from this software without specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY 21# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23# DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT 24# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30# POSSIBILITY OF SUCH DAMAGE. 31 32 33""" 34COMMAND-LINE SPECIFIC STUFF 35============================================================================= 36 37""" 38 39import markdown 40import sys 41import optparse 42 43import logging 44from logging import DEBUG, INFO, CRITICAL 45 46logger = logging.getLogger('MARKDOWN') 47 48def parse_options(): 49 """ 50 Define and parse `optparse` options for command-line usage. 51 """ 52 usage = """%prog [options] [INPUTFILE] 53 (STDIN is assumed if no INPUTFILE is given)""" 54 desc = "A Python implementation of John Gruber's Markdown. " \ 55 "http://packages.python.org/Markdown/" 56 ver = "%%prog %s" % markdown.version 57 58 parser = optparse.OptionParser(usage=usage, description=desc, version=ver) 59 parser.add_option("-f", "--file", dest="filename", default=None, 60 help="Write output to OUTPUT_FILE. Defaults to STDOUT.", 61 metavar="OUTPUT_FILE") 62 parser.add_option("-e", "--encoding", dest="encoding", 63 help="Encoding for input and output files.",) 64 parser.add_option("-q", "--quiet", default = CRITICAL, 65 action="store_const", const=CRITICAL+10, dest="verbose", 66 help="Suppress all warnings.") 67 parser.add_option("-v", "--verbose", 68 action="store_const", const=INFO, dest="verbose", 69 help="Print all warnings.") 70 parser.add_option("-s", "--safe", dest="safe", default=False, 71 metavar="SAFE_MODE", 72 help="'replace', 'remove' or 'escape' HTML tags in input") 73 parser.add_option("-o", "--output_format", dest="output_format", 74 default='xhtml1', metavar="OUTPUT_FORMAT", 75 help="'xhtml1' (default), 'html4' or 'html5'.") 76 parser.add_option("--noisy", 77 action="store_const", const=DEBUG, dest="verbose", 78 help="Print debug messages.") 79 parser.add_option("-x", "--extension", action="append", dest="extensions", 80 help = "Load extension EXTENSION.", metavar="EXTENSION") 81 parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", 82 action='store_false', default=True, 83 help="Observe number of first item of ordered lists.") 84 85 (options, args) = parser.parse_args() 86 87 if len(args) == 0: 88 input_file = None 89 else: 90 input_file = args[0] 91 92 if not options.extensions: 93 options.extensions = [] 94 95 return {'input': input_file, 96 'output': options.filename, 97 'safe_mode': options.safe, 98 'extensions': options.extensions, 99 'encoding': options.encoding, 100 'output_format': options.output_format, 101 'lazy_ol': options.lazy_ol}, options.verbose 102 103def run(): 104 """Run Markdown from the command line.""" 105 106 # Parse options and adjust logging level if necessary 107 options, logging_level = parse_options() 108 if not options: sys.exit(2) 109 logger.setLevel(logging_level) 110 logger.addHandler(logging.StreamHandler()) 111 112 # Run 113 markdown.markdownFromFile(**options) 114 115if __name__ == '__main__': 116 # Support running module as a commandline command. 117 # Python 2.5 & 2.6 do: `python -m markdown.__main__ [options] [args]`. 118 # Python 2.7 & 3.x do: `python -m markdown [options] [args]`. 119 run() 120