__main__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. COMMAND-LINE SPECIFIC STUFF
  33. =============================================================================
  34. """
  35. import markdown
  36. import sys
  37. import optparse
  38. import logging
  39. from logging import DEBUG, INFO, CRITICAL
  40. logger = logging.getLogger('MARKDOWN')
  41. def parse_options():
  42. """
  43. Define and parse `optparse` options for command-line usage.
  44. """
  45. usage = """%prog [options] [INPUTFILE]
  46. (STDIN is assumed if no INPUTFILE is given)"""
  47. desc = "A Python implementation of John Gruber's Markdown. " \
  48. "http://packages.python.org/Markdown/"
  49. ver = "%%prog %s" % markdown.version
  50. parser = optparse.OptionParser(usage=usage, description=desc, version=ver)
  51. parser.add_option("-f", "--file", dest="filename", default=None,
  52. help="Write output to OUTPUT_FILE. Defaults to STDOUT.",
  53. metavar="OUTPUT_FILE")
  54. parser.add_option("-e", "--encoding", dest="encoding",
  55. help="Encoding for input and output files.",)
  56. parser.add_option("-q", "--quiet", default = CRITICAL,
  57. action="store_const", const=CRITICAL+10, dest="verbose",
  58. help="Suppress all warnings.")
  59. parser.add_option("-v", "--verbose",
  60. action="store_const", const=INFO, dest="verbose",
  61. help="Print all warnings.")
  62. parser.add_option("-s", "--safe", dest="safe", default=False,
  63. metavar="SAFE_MODE",
  64. help="'replace', 'remove' or 'escape' HTML tags in input")
  65. parser.add_option("-o", "--output_format", dest="output_format",
  66. default='xhtml1', metavar="OUTPUT_FORMAT",
  67. help="'xhtml1' (default), 'html4' or 'html5'.")
  68. parser.add_option("--noisy",
  69. action="store_const", const=DEBUG, dest="verbose",
  70. help="Print debug messages.")
  71. parser.add_option("-x", "--extension", action="append", dest="extensions",
  72. help = "Load extension EXTENSION.", metavar="EXTENSION")
  73. parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol",
  74. action='store_false', default=True,
  75. help="Observe number of first item of ordered lists.")
  76. (options, args) = parser.parse_args()
  77. if len(args) == 0:
  78. input_file = None
  79. else:
  80. input_file = args[0]
  81. if not options.extensions:
  82. options.extensions = []
  83. return {'input': input_file,
  84. 'output': options.filename,
  85. 'safe_mode': options.safe,
  86. 'extensions': options.extensions,
  87. 'encoding': options.encoding,
  88. 'output_format': options.output_format,
  89. 'lazy_ol': options.lazy_ol}, options.verbose
  90. def run():
  91. """Run Markdown from the command line."""
  92. # Parse options and adjust logging level if necessary
  93. options, logging_level = parse_options()
  94. if not options: sys.exit(2)
  95. logger.setLevel(logging_level)
  96. logger.addHandler(logging.StreamHandler())
  97. # Run
  98. markdown.markdownFromFile(**options)
  99. if __name__ == '__main__':
  100. # Support running module as a commandline command.
  101. # Python 2.5 & 2.6 do: `python -m markdown.__main__ [options] [args]`.
  102. # Python 2.7 & 3.x do: `python -m markdown [options] [args]`.
  103. run()