commandline.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. COMMAND-LINE SPECIFIC STUFF
  3. =============================================================================
  4. The rest of the code is specifically for handling the case where Python
  5. Markdown is called from the command line.
  6. """
  7. import markdown
  8. import sys
  9. import logging
  10. from logging import DEBUG, INFO, WARN, ERROR, CRITICAL
  11. EXECUTABLE_NAME_FOR_USAGE = "python markdown.py"
  12. """ The name used in the usage statement displayed for python versions < 2.3.
  13. (With python 2.3 and higher the usage statement is generated by optparse
  14. and uses the actual name of the executable called.) """
  15. OPTPARSE_WARNING = """
  16. Python 2.3 or higher required for advanced command line options.
  17. For lower versions of Python use:
  18. %s INPUT_FILE > OUTPUT_FILE
  19. """ % EXECUTABLE_NAME_FOR_USAGE
  20. def parse_options():
  21. """
  22. Define and parse `optparse` options for command-line usage.
  23. """
  24. try:
  25. optparse = __import__("optparse")
  26. except:
  27. if len(sys.argv) == 2:
  28. return {'input': sys.argv[1],
  29. 'output': None,
  30. 'safe': False,
  31. 'extensions': [],
  32. 'encoding': None }, CRITICAL
  33. else:
  34. print OPTPARSE_WARNING
  35. return None, None
  36. parser = optparse.OptionParser(usage="%prog INPUTFILE [options]")
  37. parser.add_option("-f", "--file", dest="filename", default=sys.stdout,
  38. help="write output to OUTPUT_FILE",
  39. metavar="OUTPUT_FILE")
  40. parser.add_option("-e", "--encoding", dest="encoding",
  41. help="encoding for input and output files",)
  42. parser.add_option("-q", "--quiet", default = CRITICAL,
  43. action="store_const", const=CRITICAL+10, dest="verbose",
  44. help="suppress all messages")
  45. parser.add_option("-v", "--verbose",
  46. action="store_const", const=INFO, dest="verbose",
  47. help="print info messages")
  48. parser.add_option("-s", "--safe", dest="safe", default=False,
  49. metavar="SAFE_MODE",
  50. help="safe mode ('replace', 'remove' or 'escape' user's HTML tag)")
  51. parser.add_option("-o", "--output_format", dest="output_format",
  52. default='xhtml1', metavar="OUTPUT_FORMAT",
  53. help="Format of output. One of 'xhtml1' (default) or 'html4'.")
  54. parser.add_option("--noisy",
  55. action="store_const", const=DEBUG, dest="verbose",
  56. help="print debug messages")
  57. parser.add_option("-x", "--extension", action="append", dest="extensions",
  58. help = "load extension EXTENSION", metavar="EXTENSION")
  59. (options, args) = parser.parse_args()
  60. if not len(args) == 1:
  61. parser.print_help()
  62. return None, None
  63. else:
  64. input_file = args[0]
  65. if not options.extensions:
  66. options.extensions = []
  67. return {'input': input_file,
  68. 'output': options.filename,
  69. 'safe_mode': options.safe,
  70. 'extensions': options.extensions,
  71. 'encoding': options.encoding,
  72. 'output_format': options.output_format}, options.verbose
  73. def run():
  74. """Run Markdown from the command line."""
  75. # Parse options and adjust logging level if necessary
  76. options, logging_level = parse_options()
  77. if not options: sys.exit(0)
  78. if logging_level: logging.getLogger('MARKDOWN').setLevel(logging_level)
  79. # Run
  80. markdown.markdownFromFile(**options)