| 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 |
# default logging level for command-line use |
| 17 |
COMMAND_LINE_LOGGING_LEVEL = CRITICAL |
| 18 |
|
| 19 |
def parse_options(): |
| 20 |
""" |
| 21 |
Define and parse `optparse` options for command-line usage. |
| 22 |
""" |
| 23 |
usage = """%prog [options] [INPUTFILE] |
| 24 |
(STDIN is assumed if no INPUTFILE is given)""" |
| 25 |
desc = "A Python implementation of John Gruber's Markdown. " \ |
| 26 |
"http://www.freewisdom.org/projects/python-markdown/" |
| 27 |
ver = "%%prog %s" % markdown.version |
| 28 |
|
| 29 |
parser = optparse.OptionParser(usage=usage, description=desc, version=ver) |
| 30 |
parser.add_option("-f", "--file", dest="filename", default=sys.stdout, |
| 31 |
help="write output to OUTPUT_FILE", |
| 32 |
metavar="OUTPUT_FILE") |
| 33 |
parser.add_option("-e", "--encoding", dest="encoding", |
| 34 |
help="encoding for input and output files",) |
| 35 |
parser.add_option("-q", "--quiet", default = CRITICAL, |
| 36 |
action="store_const", const=CRITICAL+10, dest="verbose", |
| 37 |
help="suppress all messages") |
| 38 |
parser.add_option("-v", "--verbose", |
| 39 |
action="store_const", const=INFO, dest="verbose", |
| 40 |
help="print info messages") |
| 41 |
parser.add_option("-s", "--safe", dest="safe", default=False, |
| 42 |
metavar="SAFE_MODE", |
| 43 |
help="'replace', 'remove' or 'escape' HTML tags in input") |
| 44 |
parser.add_option("-o", "--output_format", dest="output_format", |
| 45 |
default='xhtml1', metavar="OUTPUT_FORMAT", |
| 46 |
help="'xhtml1' (default) or 'html4'.") |
| 47 |
parser.add_option("--noisy", |
| 48 |
action="store_const", const=DEBUG, dest="verbose", |
| 49 |
help="print debug messages") |
| 50 |
parser.add_option("-x", "--extension", action="append", dest="extensions", |
| 51 |
help = "load extension EXTENSION", metavar="EXTENSION") |
| 52 |
parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", |
| 53 |
action='store_false', default=True, |
| 54 |
help="Observe number of first item of ordered lists.") |
| 55 |
|
| 56 |
(options, args) = parser.parse_args() |
| 57 |
|
| 58 |
if len(args) == 0: |
| 59 |
input_file = sys.stdin |
| 60 |
else: |
| 61 |
input_file = args[0] |
| 62 |
|
| 63 |
if not options.extensions: |
| 64 |
options.extensions = [] |
| 65 |
|
| 66 |
return {'input': input_file, |
| 67 |
'output': options.filename, |
| 68 |
'safe_mode': options.safe, |
| 69 |
'extensions': options.extensions, |
| 70 |
'encoding': options.encoding, |
| 71 |
'output_format': options.output_format, |
| 72 |
'lazy_ol': options.lazy_ol}, options.verbose |
| 73 |
|
| 74 |
def run(): |
| 75 |
"""Run Markdown from the command line.""" |
| 76 |
|
| 77 |
# Parse options and adjust logging level if necessary |
| 78 |
options, logging_level = parse_options() |
| 79 |
if not options: sys.exit(2) |
| 80 |
if logging_level: logger.setLevel(logging_level) |
| 81 |
|
| 82 |
# Run |
| 83 |
markdown.markdownFromFile(**options) |