Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
910ffa5
commit 116dd81
Showing
30 changed files
with
5,281 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
COMMAND-LINE SPECIFIC STUFF | ||
============================================================================= | ||
""" | ||
|
||
import markdown | ||
import sys | ||
import optparse | ||
|
||
import logging | ||
from logging import DEBUG, INFO, CRITICAL | ||
|
||
logger = logging.getLogger('MARKDOWN') | ||
|
||
def parse_options(): | ||
""" | ||
Define and parse `optparse` options for command-line usage. | ||
""" | ||
usage = """%prog [options] [INPUTFILE] | ||
(STDIN is assumed if no INPUTFILE is given)""" | ||
desc = "A Python implementation of John Gruber's Markdown. " \ | ||
"http://packages.python.org/Markdown/" | ||
ver = "%%prog %s" % markdown.version | ||
|
||
parser = optparse.OptionParser(usage=usage, description=desc, version=ver) | ||
parser.add_option("-f", "--file", dest="filename", default=None, | ||
help="Write output to OUTPUT_FILE. Defaults to STDOUT.", | ||
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 warnings.") | ||
parser.add_option("-v", "--verbose", | ||
action="store_const", const=INFO, dest="verbose", | ||
help="Print all warnings.") | ||
parser.add_option("-s", "--safe", dest="safe", default=False, | ||
metavar="SAFE_MODE", | ||
help="'replace', 'remove' or 'escape' HTML tags in input") | ||
parser.add_option("-o", "--output_format", dest="output_format", | ||
default='xhtml1', metavar="OUTPUT_FORMAT", | ||
help="'xhtml1' (default), 'html4' or 'html5'.") | ||
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") | ||
parser.add_option("-n", "--no_lazy_ol", dest="lazy_ol", | ||
action='store_false', default=True, | ||
help="Observe number of first item of ordered lists.") | ||
|
||
(options, args) = parser.parse_args() | ||
|
||
if len(args) == 0: | ||
input_file = None | ||
else: | ||
input_file = args[0] | ||
|
||
if not options.extensions: | ||
options.extensions = [] | ||
|
||
return {'input': input_file, | ||
'output': options.filename, | ||
'safe_mode': options.safe, | ||
'extensions': options.extensions, | ||
'encoding': options.encoding, | ||
'output_format': options.output_format, | ||
'lazy_ol': options.lazy_ol}, options.verbose | ||
|
||
def run(): | ||
"""Run Markdown from the command line.""" | ||
|
||
# Parse options and adjust logging level if necessary | ||
options, logging_level = parse_options() | ||
if not options: sys.exit(2) | ||
logger.setLevel(logging_level) | ||
logger.addHandler(logging.StreamHandler()) | ||
|
||
# Run | ||
markdown.markdownFromFile(**options) | ||
|
||
if __name__ == '__main__': | ||
# Support running module as a commandline command. | ||
# Python 2.5 & 2.6 do: `python -m markdown.__main__ [options] [args]`. | ||
# Python 2.7 & 3.x do: `python -m markdown [options] [args]`. | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# | ||
# markdown/__version__.py | ||
# | ||
# version_info should conform to PEP 386 | ||
# (major, minor, micro, alpha/beta/rc/final, #) | ||
# (1, 1, 2, 'alpha', 0) => "1.1.2.dev" | ||
# (1, 2, 0, 'beta', 2) => "1.2b2" | ||
version_info = (2, 3, 1, 'final', 0) | ||
|
||
def _get_version(): | ||
" Returns a PEP 386-compliant version number from version_info. " | ||
assert len(version_info) == 5 | ||
assert version_info[3] in ('alpha', 'beta', 'rc', 'final') | ||
|
||
parts = 2 if version_info[2] == 0 else 3 | ||
main = '.'.join(map(str, version_info[:parts])) | ||
|
||
sub = '' | ||
if version_info[3] == 'alpha' and version_info[4] == 0: | ||
# TODO: maybe append some sort of git info here?? | ||
sub = '.dev' | ||
elif version_info[3] != 'final': | ||
mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'} | ||
sub = mapping[version_info[3]] + str(version_info[4]) | ||
|
||
return str(main + sub) | ||
|
||
version = _get_version() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from __future__ import unicode_literals | ||
from __future__ import absolute_import | ||
from . import util | ||
from . import odict | ||
|
||
class State(list): | ||
""" Track the current and nested state of the parser. | ||
This utility class is used to track the state of the BlockParser and | ||
support multiple levels if nesting. It's just a simple API wrapped around | ||
a list. Each time a state is set, that state is appended to the end of the | ||
list. Each time a state is reset, that state is removed from the end of | ||
the list. | ||
Therefore, each time a state is set for a nested block, that state must be | ||
reset when we back out of that level of nesting or the state could be | ||
corrupted. | ||
While all the methods of a list object are available, only the three | ||
defined below need be used. | ||
""" | ||
|
||
def set(self, state): | ||
""" Set a new state. """ | ||
self.append(state) | ||
|
||
def reset(self): | ||
""" Step back one step in nested state. """ | ||
self.pop() | ||
|
||
def isstate(self, state): | ||
""" Test that top (current) level is of given state. """ | ||
if len(self): | ||
return self[-1] == state | ||
else: | ||
return False | ||
|
||
class BlockParser: | ||
""" Parse Markdown blocks into an ElementTree object. | ||
A wrapper class that stitches the various BlockProcessors together, | ||
looping through them and creating an ElementTree object. | ||
""" | ||
|
||
def __init__(self, markdown): | ||
self.blockprocessors = odict.OrderedDict() | ||
self.state = State() | ||
self.markdown = markdown | ||
|
||
def parseDocument(self, lines): | ||
""" Parse a markdown document into an ElementTree. | ||
Given a list of lines, an ElementTree object (not just a parent Element) | ||
is created and the root element is passed to the parser as the parent. | ||
The ElementTree object is returned. | ||
This should only be called on an entire document, not pieces. | ||
""" | ||
# Create a ElementTree from the lines | ||
self.root = util.etree.Element(self.markdown.doc_tag) | ||
self.parseChunk(self.root, '\n'.join(lines)) | ||
return util.etree.ElementTree(self.root) | ||
|
||
def parseChunk(self, parent, text): | ||
""" Parse a chunk of markdown text and attach to given etree node. | ||
While the ``text`` argument is generally assumed to contain multiple | ||
blocks which will be split on blank lines, it could contain only one | ||
block. Generally, this method would be called by extensions when | ||
block parsing is required. | ||
The ``parent`` etree Element passed in is altered in place. | ||
Nothing is returned. | ||
""" | ||
self.parseBlocks(parent, text.split('\n\n')) | ||
|
||
def parseBlocks(self, parent, blocks): | ||
""" Process blocks of markdown text and attach to given etree node. | ||
Given a list of ``blocks``, each blockprocessor is stepped through | ||
until there are no blocks left. While an extension could potentially | ||
call this method directly, it's generally expected to be used internally. | ||
This is a public method as an extension may need to add/alter additional | ||
BlockProcessors which call this method to recursively parse a nested | ||
block. | ||
""" | ||
while blocks: | ||
for processor in self.blockprocessors.values(): | ||
if processor.test(parent, blocks[0]): | ||
if processor.run(parent, blocks) is not False: | ||
# run returns True or None | ||
break | ||
|
||
|
Oops, something went wrong.