Various fixes to markdown found by the automatic python checker

This commit is contained in:
Kovid Goyal 2011-09-03 14:42:04 -06:00
parent 33d6ae3486
commit c7bf67e6c3
10 changed files with 53 additions and 54 deletions

View File

@ -63,10 +63,10 @@ class Check(Command):
for f in x[-1]: for f in x[-1]:
y = self.j(x[0], f) y = self.j(x[0], f)
mtime = os.stat(y).st_mtime mtime = os.stat(y).st_mtime
if f.endswith('.py') and f not in ('ptempfile.py', 'feedparser.py', if (f.endswith('.py') and f not in ('ptempfile.py', 'feedparser.py',
'pyparsing.py', 'markdown.py') and \ 'pyparsing.py', 'markdown.py') and
'genshi' not in y and cache.get(y, 0) != mtime and \ 'genshi' not in y and cache.get(y, 0) != mtime and
'prs500/driver.py' not in y: 'prs500/driver.py' not in y):
yield y, mtime yield y, mtime
for x in os.walk(self.j(self.d(self.SRC), 'recipes')): for x in os.walk(self.j(self.d(self.SRC), 'recipes')):

View File

@ -3,10 +3,10 @@ CORE MARKDOWN BLOCKPARSER
============================================================================= =============================================================================
This parser handles basic parsing of Markdown blocks. It doesn't concern itself This parser handles basic parsing of Markdown blocks. It doesn't concern itself
with inline elements such as **bold** or *italics*, but rather just catches with inline elements such as **bold** or *italics*, but rather just catches
blocks, lists, quotes, etc. blocks, lists, quotes, etc.
The BlockParser is made up of a bunch of BlockProssors, each handling a The BlockParser is made up of a bunch of BlockProssors, each handling a
different type of block. Extensions may add/replace/remove BlockProcessors different type of block. Extensions may add/replace/remove BlockProcessors
as they need to alter how markdown blocks are parsed. as they need to alter how markdown blocks are parsed.
@ -16,8 +16,8 @@ import re
import markdown import markdown
class BlockProcessor: class BlockProcessor:
""" Base class for block processors. """ Base class for block processors.
Each subclass will provide the methods below to work with the source and Each subclass will provide the methods below to work with the source and
tree. Each processor will need to define it's own ``test`` and ``run`` tree. Each processor will need to define it's own ``test`` and ``run``
methods. The ``test`` method should return True or False, to indicate methods. The ``test`` method should return True or False, to indicate
@ -58,32 +58,32 @@ class BlockProcessor:
return '\n'.join(lines) return '\n'.join(lines)
def test(self, parent, block): def test(self, parent, block):
""" Test for block type. Must be overridden by subclasses. """ Test for block type. Must be overridden by subclasses.
As the parser loops through processors, it will call the ``test`` method As the parser loops through processors, it will call the ``test`` method
on each to determine if the given block of text is of that type. This on each to determine if the given block of text is of that type. This
method must return a boolean ``True`` or ``False``. The actual method of method must return a boolean ``True`` or ``False``. The actual method of
testing is left to the needs of that particular block type. It could testing is left to the needs of that particular block type. It could
be as simple as ``block.startswith(some_string)`` or a complex regular be as simple as ``block.startswith(some_string)`` or a complex regular
expression. As the block type may be different depending on the parent expression. As the block type may be different depending on the parent
of the block (i.e. inside a list), the parent etree element is also of the block (i.e. inside a list), the parent etree element is also
provided and may be used as part of the test. provided and may be used as part of the test.
Keywords: Keywords:
* ``parent``: A etree element which will be the parent of the block. * ``parent``: A etree element which will be the parent of the block.
* ``block``: A block of text from the source which has been split at * ``block``: A block of text from the source which has been split at
blank lines. blank lines.
""" """
pass pass
def run(self, parent, blocks): def run(self, parent, blocks):
""" Run processor. Must be overridden by subclasses. """ Run processor. Must be overridden by subclasses.
When the parser determines the appropriate type of a block, the parser When the parser determines the appropriate type of a block, the parser
will call the corresponding processor's ``run`` method. This method will call the corresponding processor's ``run`` method. This method
should parse the individual lines of the block and append them to should parse the individual lines of the block and append them to
the etree. the etree.
Note that both the ``parent`` and ``etree`` keywords are pointers Note that both the ``parent`` and ``etree`` keywords are pointers
to instances of the objects which should be edited in place. Each to instances of the objects which should be edited in place. Each
@ -103,8 +103,8 @@ class BlockProcessor:
class ListIndentProcessor(BlockProcessor): class ListIndentProcessor(BlockProcessor):
""" Process children of list items. """ Process children of list items.
Example: Example:
* a list item * a list item
process this part process this part
@ -154,7 +154,7 @@ class ListIndentProcessor(BlockProcessor):
""" Create a new li and parse the block with it as the parent. """ """ Create a new li and parse the block with it as the parent. """
li = markdown.etree.SubElement(parent, 'li') li = markdown.etree.SubElement(parent, 'li')
self.parser.parseBlocks(li, [block]) self.parser.parseBlocks(li, [block])
def get_level(self, parent, block): def get_level(self, parent, block):
""" Get level of indent based on list level. """ """ Get level of indent based on list level. """
# Get indent level # Get indent level
@ -188,7 +188,7 @@ class CodeBlockProcessor(BlockProcessor):
def test(self, parent, block): def test(self, parent, block):
return block.startswith(' '*markdown.TAB_LENGTH) return block.startswith(' '*markdown.TAB_LENGTH)
def run(self, parent, blocks): def run(self, parent, blocks):
sibling = self.lastChild(parent) sibling = self.lastChild(parent)
block = blocks.pop(0) block = blocks.pop(0)
@ -208,7 +208,7 @@ class CodeBlockProcessor(BlockProcessor):
block, theRest = self.detab(block) block, theRest = self.detab(block)
code.text = markdown.AtomicString('%s\n' % block.rstrip()) code.text = markdown.AtomicString('%s\n' % block.rstrip())
if theRest: if theRest:
# This block contained unindented line(s) after the first indented # This block contained unindented line(s) after the first indented
# line. Insert these lines as the first block of the master blocks # line. Insert these lines as the first block of the master blocks
# list for future processing. # list for future processing.
blocks.insert(0, theRest) blocks.insert(0, theRest)
@ -229,7 +229,7 @@ class BlockQuoteProcessor(BlockProcessor):
# Pass lines before blockquote in recursively for parsing forst. # Pass lines before blockquote in recursively for parsing forst.
self.parser.parseBlocks(parent, [before]) self.parser.parseBlocks(parent, [before])
# Remove ``> `` from begining of each line. # Remove ``> `` from begining of each line.
block = '\n'.join([self.clean(line) for line in block = '\n'.join([self.clean(line) for line in
block[m.start():].split('\n')]) block[m.start():].split('\n')])
sibling = self.lastChild(parent) sibling = self.lastChild(parent)
if sibling and sibling.tag == "blockquote": if sibling and sibling.tag == "blockquote":
@ -355,7 +355,7 @@ class HashHeaderProcessor(BlockProcessor):
blocks.insert(0, after) blocks.insert(0, after)
else: else:
# This should never happen, but just in case... # This should never happen, but just in case...
message(CRITICAL, "We've got a problem header!") print("We've got a problem header!")
class SetextHeaderProcessor(BlockProcessor): class SetextHeaderProcessor(BlockProcessor):
@ -407,7 +407,7 @@ class HRProcessor(BlockProcessor):
# Recursively parse lines before hr so they get parsed first. # Recursively parse lines before hr so they get parsed first.
self.parser.parseBlocks(parent, ['\n'.join(prelines)]) self.parser.parseBlocks(parent, ['\n'.join(prelines)])
# create hr # create hr
hr = markdown.etree.SubElement(parent, 'hr') markdown.etree.SubElement(parent, 'hr')
# check for lines in block after hr. # check for lines in block after hr.
lines = lines[len(prelines)+1:] lines = lines[len(prelines)+1:]
if len(lines): if len(lines):
@ -418,7 +418,7 @@ class HRProcessor(BlockProcessor):
class EmptyBlockProcessor(BlockProcessor): class EmptyBlockProcessor(BlockProcessor):
""" Process blocks and start with an empty line. """ """ Process blocks and start with an empty line. """
# Detect a block that only contains whitespace # Detect a block that only contains whitespace
# or only whitespace on the first line. # or only whitespace on the first line.
RE = re.compile(r'^\s*\n') RE = re.compile(r'^\s*\n')

View File

@ -9,7 +9,7 @@ Markdown is called from the command line.
import markdown import markdown
import sys import sys
import logging import logging
from logging import DEBUG, INFO, WARN, ERROR, CRITICAL from logging import DEBUG, INFO, CRITICAL
EXECUTABLE_NAME_FOR_USAGE = "python markdown.py" EXECUTABLE_NAME_FOR_USAGE = "python markdown.py"
""" The name used in the usage statement displayed for python versions < 2.3. """ The name used in the usage statement displayed for python versions < 2.3.
@ -57,7 +57,7 @@ def parse_options():
parser.add_option("-s", "--safe", dest="safe", default=False, parser.add_option("-s", "--safe", dest="safe", default=False,
metavar="SAFE_MODE", metavar="SAFE_MODE",
help="safe mode ('replace', 'remove' or 'escape' user's HTML tag)") help="safe mode ('replace', 'remove' or 'escape' user's HTML tag)")
parser.add_option("-o", "--output_format", dest="output_format", parser.add_option("-o", "--output_format", dest="output_format",
default='xhtml1', metavar="OUTPUT_FORMAT", default='xhtml1', metavar="OUTPUT_FORMAT",
help="Format of output. One of 'xhtml1' (default) or 'html4'.") help="Format of output. One of 'xhtml1' (default) or 'html4'.")
parser.add_option("--noisy", parser.add_option("--noisy",

View File

@ -8,9 +8,11 @@ def importETree():
etree_in_c = None etree_in_c = None
try: # Is it Python 2.5+ with C implemenation of ElementTree installed? try: # Is it Python 2.5+ with C implemenation of ElementTree installed?
import xml.etree.cElementTree as etree_in_c import xml.etree.cElementTree as etree_in_c
etree_in_c
except ImportError: except ImportError:
try: # Is it Python 2.5+ with Python implementation of ElementTree? try: # Is it Python 2.5+ with Python implementation of ElementTree?
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
etree
except ImportError: except ImportError:
try: # An earlier version of Python with cElementTree installed? try: # An earlier version of Python with cElementTree installed?
import cElementTree as etree_in_c import cElementTree as etree_in_c

View File

@ -8,7 +8,7 @@ Added parsing of Definition Lists to Python-Markdown.
A simple example: A simple example:
Apple Apple
: Pomaceous fruit of plants of the genus Malus in : Pomaceous fruit of plants of the genus Malus in
the family Rosaceae. the family Rosaceae.
: An american computer company. : An american computer company.
@ -80,11 +80,11 @@ class DefListIndentProcessor(markdown.blockprocessors.ListIndentProcessor):
ITEM_TYPES = ['dd'] ITEM_TYPES = ['dd']
LIST_TYPES = ['dl'] LIST_TYPES = ['dl']
def create_item(parent, block): def create_item(self, parent, block):
""" Create a new dd and parse the block with it as the parent. """ """ Create a new dd and parse the block with it as the parent. """
dd = markdown.etree.SubElement(parent, 'dd') dd = markdown.etree.SubElement(parent, 'dd')
self.parser.parseBlocks(dd, [block]) self.parser.parseBlocks(dd, [block])
class DefListExtension(markdown.Extension): class DefListExtension(markdown.Extension):
@ -95,7 +95,7 @@ class DefListExtension(markdown.Extension):
md.parser.blockprocessors.add('defindent', md.parser.blockprocessors.add('defindent',
DefListIndentProcessor(md.parser), DefListIndentProcessor(md.parser),
'>indent') '>indent')
md.parser.blockprocessors.add('deflist', md.parser.blockprocessors.add('deflist',
DefListProcessor(md.parser), DefListProcessor(md.parser),
'>ulist') '>ulist')

View File

@ -43,7 +43,7 @@ class FootnoteExtension(markdown.Extension):
for key, value in configs: for key, value in configs:
self.config[key][0] = value self.config[key][0] = value
self.reset() self.reset()
def extendMarkdown(self, md, md_globals): def extendMarkdown(self, md, md_globals):
@ -82,7 +82,7 @@ class FootnoteExtension(markdown.Extension):
return (child, element), False return (child, element), False
finder(child) finder(child)
return None return None
res = finder(root) res = finder(root)
return res return res
@ -106,7 +106,7 @@ class FootnoteExtension(markdown.Extension):
div = etree.Element("div") div = etree.Element("div")
div.set('class', 'footnote') div.set('class', 'footnote')
hr = etree.SubElement(div, "hr") etree.SubElement(div, "hr")
ol = etree.SubElement(div, "ol") ol = etree.SubElement(div, "ol")
for id in self.footnotes.keys(): for id in self.footnotes.keys():
@ -149,9 +149,9 @@ class FootnotePreprocessor(markdown.preprocessors.Preprocessor):
Keywords: Keywords:
* lines: A list of lines of text * lines: A list of lines of text
Return: A list of lines with footnote definitions removed. Return: A list of lines with footnote definitions removed.
""" """
i, id, footnote = self._findFootnoteDefinition(lines) i, id, footnote = self._findFootnoteDefinition(lines)
@ -175,9 +175,9 @@ class FootnotePreprocessor(markdown.preprocessors.Preprocessor):
* lines: A list of lines of text. * lines: A list of lines of text.
Return: A three item tuple containing the index of the first line of a Return: A three item tuple containing the index of the first line of a
footnote definition, the id of the definition and the body of the footnote definition, the id of the definition and the body of the
definition. definition.
""" """
counter = 0 counter = 0
for line in lines: for line in lines:
@ -199,7 +199,6 @@ class FootnotePreprocessor(markdown.preprocessors.Preprocessor):
""" """
items = [] items = []
item = -1
i = 0 # to keep track of where we are i = 0 # to keep track of where we are
def detab(line): def detab(line):
@ -277,7 +276,6 @@ class FootnoteTreeprocessor(markdown.treeprocessors.Treeprocessor):
ind = element.getchildren().find(child) ind = element.getchildren().find(child)
element.getchildren().insert(ind + 1, footnotesDiv) element.getchildren().insert(ind + 1, footnotesDiv)
child.tail = None child.tail = None
fnPlaceholder.parent.replaceChild(fnPlaceholder, footnotesDiv)
else: else:
root.append(footnotesDiv) root.append(footnotesDiv)

View File

@ -57,7 +57,7 @@ Copyright 2007-2008 [Waylan Limberg](http://achinghead.com/).
Project website: <http://www.freewisdom.org/project/python-markdown/HeaderId> Project website: <http://www.freewisdom.org/project/python-markdown/HeaderId>
Contact: markdown@freewisdom.org Contact: markdown@freewisdom.org
License: BSD (see ../docs/LICENSE for details) License: BSD (see ../docs/LICENSE for details)
Dependencies: Dependencies:
* [Python 2.3+](http://python.org) * [Python 2.3+](http://python.org)
@ -66,7 +66,6 @@ Dependencies:
""" """
import calibre.ebooks.markdown.markdown as markdown import calibre.ebooks.markdown.markdown as markdown
from calibre.ebooks.markdown.markdown import etree
import re import re
from string import ascii_lowercase, digits, punctuation from string import ascii_lowercase, digits, punctuation
@ -106,7 +105,7 @@ class HeaderIdProcessor(markdown.blockprocessors.BlockProcessor):
# Create header using named groups from RE # Create header using named groups from RE
start_level, force_id = self._get_meta() start_level, force_id = self._get_meta()
level = len(m.group('level')) + start_level level = len(m.group('level')) + start_level
if level > 6: if level > 6:
level = 6 level = 6
h = markdown.etree.SubElement(parent, 'h%d' % level) h = markdown.etree.SubElement(parent, 'h%d' % level)
h.text = m.group('header').strip() h.text = m.group('header').strip()
@ -119,7 +118,7 @@ class HeaderIdProcessor(markdown.blockprocessors.BlockProcessor):
blocks.insert(0, after) blocks.insert(0, after)
else: else:
# This should never happen, but just in case... # This should never happen, but just in case...
message(CRITICAL, "We've got a problem header!") print ("We've got a problem header!")
def _get_meta(self): def _get_meta(self):
""" Return meta data suported by this ext as a tuple """ """ Return meta data suported by this ext as a tuple """
@ -128,7 +127,7 @@ class HeaderIdProcessor(markdown.blockprocessors.BlockProcessor):
if hasattr(self.md, 'Meta'): if hasattr(self.md, 'Meta'):
if self.md.Meta.has_key('header_level'): if self.md.Meta.has_key('header_level'):
level = int(self.md.Meta['header_level'][0]) - 1 level = int(self.md.Meta['header_level'][0]) - 1
if self.md.Meta.has_key('header_forceid'): if self.md.Meta.has_key('header_forceid'):
force = self._str2bool(self.md.Meta['header_forceid'][0]) force = self._str2bool(self.md.Meta['header_forceid'][0])
return level, force return level, force

View File

@ -47,6 +47,7 @@ from urlparse import urlparse, urlunparse
import sys import sys
if sys.version >= "3.0": if sys.version >= "3.0":
from html import entities as htmlentitydefs from html import entities as htmlentitydefs
htmlentitydefs
else: else:
import htmlentitydefs import htmlentitydefs
@ -215,7 +216,6 @@ class HtmlPattern (Pattern):
""" Store raw inline html and return a placeholder. """ """ Store raw inline html and return a placeholder. """
def handleMatch (self, m): def handleMatch (self, m):
rawhtml = m.group(2) rawhtml = m.group(2)
inline = True
place_holder = self.markdown.htmlStash.store(rawhtml) place_holder = self.markdown.htmlStash.store(rawhtml)
return place_holder return place_holder

View File

@ -1,7 +1,7 @@
class OrderedDict(dict): class OrderedDict(dict):
""" """
A dictionary that keeps its keys in the order in which they're inserted. A dictionary that keeps its keys in the order in which they're inserted.
Copied from Django's SortedDict with some modifications. Copied from Django's SortedDict with some modifications.
""" """
@ -156,7 +156,7 @@ class OrderedDict(dict):
self.keyOrder.insert(i, key) self.keyOrder.insert(i, key)
else: else:
self.keyOrder.append(key) self.keyOrder.append(key)
except Error: except Exception as e:
# restore to prevent data loss and reraise # restore to prevent data loss and reraise
self.keyOrder.insert(n, key) self.keyOrder.insert(n, key)
raise Error raise e

View File

@ -24,8 +24,8 @@ class Treeprocessor(Processor):
def run(self, root): def run(self, root):
""" """
Subclasses of Treeprocessor should implement a `run` method, which Subclasses of Treeprocessor should implement a `run` method, which
takes a root ElementTree. This method can return another ElementTree takes a root ElementTree. This method can return another ElementTree
object, and the existing root ElementTree will be replaced, or it can object, and the existing root ElementTree will be replaced, or it can
modify the current tree and return None. modify the current tree and return None.
""" """
pass pass
@ -185,7 +185,7 @@ class InlineProcessor(Treeprocessor):
result.append(node) result.append(node)
else: # wrong placeholder else: # wrong placeholder
end = index + len(prefix) end = index + len(self.__placeholder_prefix)
linkText(data[strartIndex:end]) linkText(data[strartIndex:end])
strartIndex = end strartIndex = end
else: else:
@ -278,7 +278,7 @@ class InlineProcessor(Treeprocessor):
for element, lst in insertQueue: for element, lst in insertQueue:
if element.text: if element.text:
element.text = \ element.text = \
markdown.inlinepatterns.handleAttributes(element.text, markdown.inlinepatterns.handleAttributes(element.text,
element) element)
i = 0 i = 0
for newChild in lst: for newChild in lst: