diff options
| author | Waylan Limberg <waylan.limberg@icloud.com> | 2015-03-28 15:35:02 -0400 |
|---|---|---|
| committer | Waylan Limberg <waylan.limberg@icloud.com> | 2015-04-22 19:53:48 -0400 |
| commit | a1aa1b6bcf20d382479cd88293c655aed1095cc7 (patch) | |
| tree | 2d5cfcda795e4aef9b4d619f712ec2126eb2129b /markdown | |
| parent | ec54b7a47f36922af9fab4b6ae04bba4ad75ad94 (diff) | |
| download | python-markdown-a1aa1b6bcf20d382479cd88293c655aed1095cc7.tar.gz | |
All Markdown instances are now 'md'.
Previously, instances of the Markdown class were represented as any one of
'md', 'md_instance', or 'markdown'. This inconsistancy made it diffcult
when developing extensions, or just maintaining the existing code.
Now, all instances are consistantly represented as 'md'.
Diffstat (limited to 'markdown')
| -rw-r--r-- | markdown/blockparser.py | 6 | ||||
| -rw-r--r-- | markdown/blockprocessors.py | 8 | ||||
| -rw-r--r-- | markdown/extensions/abbr.py | 2 | ||||
| -rw-r--r-- | markdown/extensions/codehilite.py | 4 | ||||
| -rw-r--r-- | markdown/extensions/extra.py | 2 | ||||
| -rw-r--r-- | markdown/extensions/fenced_code.py | 4 | ||||
| -rw-r--r-- | markdown/extensions/meta.py | 2 | ||||
| -rw-r--r-- | markdown/extensions/smarty.py | 6 | ||||
| -rw-r--r-- | markdown/extensions/toc.py | 10 | ||||
| -rw-r--r-- | markdown/inlinepatterns.py | 48 | ||||
| -rw-r--r-- | markdown/postprocessors.py | 12 | ||||
| -rw-r--r-- | markdown/preprocessors.py | 42 | ||||
| -rw-r--r-- | markdown/treeprocessors.py | 12 | ||||
| -rw-r--r-- | markdown/util.py | 6 |
14 files changed, 82 insertions, 82 deletions
diff --git a/markdown/blockparser.py b/markdown/blockparser.py index 32d3254..4980f26 100644 --- a/markdown/blockparser.py +++ b/markdown/blockparser.py @@ -45,10 +45,10 @@ class BlockParser: looping through them and creating an ElementTree object. """ - def __init__(self, markdown): + def __init__(self, md): self.blockprocessors = odict.OrderedDict() self.state = State() - self.markdown = markdown + self.md = md def parseDocument(self, lines): """ Parse a markdown document into an ElementTree. @@ -61,7 +61,7 @@ class BlockParser: """ # Create a ElementTree from the lines - self.root = util.etree.Element(self.markdown.doc_tag) + self.root = util.etree.Element(self.md.doc_tag) self.parseChunk(self.root, '\n'.join(lines)) return util.etree.ElementTree(self.root) diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 29db022..02f2bb3 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -22,9 +22,9 @@ from .blockparser import BlockParser logger = logging.getLogger('MARKDOWN') -def build_block_parser(md_instance, **kwargs): +def build_block_parser(md, **kwargs): """ Build the default block parser used by Markdown. """ - parser = BlockParser(md_instance) + parser = BlockParser(md) parser.blockprocessors['empty'] = EmptyBlockProcessor(parser) parser.blockprocessors['indent'] = ListIndentProcessor(parser) parser.blockprocessors['code'] = CodeBlockProcessor(parser) @@ -51,7 +51,7 @@ class BlockProcessor: def __init__(self, parser): self.parser = parser - self.tab_length = parser.markdown.tab_length + self.tab_length = parser.md.tab_length def lastChild(self, parent): """ Return the last child of an etree element. """ @@ -360,7 +360,7 @@ class OListProcessor(BlockProcessor): # This is a new list so create parent with appropriate tag. lst = util.etree.SubElement(parent, self.TAG) # Check if a custom start integer is set - if not self.parser.markdown.lazy_ol and self.STARTSWITH != '1': + if not self.parser.md.lazy_ol and self.STARTSWITH != '1': lst.attrib['start'] = self.STARTSWITH self.parser.state.set('list') diff --git a/markdown/extensions/abbr.py b/markdown/extensions/abbr.py index a78c9a3..dd98b6c 100644 --- a/markdown/extensions/abbr.py +++ b/markdown/extensions/abbr.py @@ -51,7 +51,7 @@ class AbbrPreprocessor(Preprocessor): if m: abbr = m.group('abbr').strip() title = m.group('title').strip() - self.markdown.inlinePatterns['abbr-%s' % abbr] = \ + self.md.inlinePatterns['abbr-%s' % abbr] = \ AbbrPattern(self._generate_pattern(abbr), title) else: new_text.append(line) diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py index ed6da7b..622aef8 100644 --- a/markdown/extensions/codehilite.py +++ b/markdown/extensions/codehilite.py @@ -212,10 +212,10 @@ class HiliteTreeprocessor(Treeprocessor): css_class=self.config['css_class'], style=self.config['pygments_style'], noclasses=self.config['noclasses'], - tab_length=self.markdown.tab_length, + tab_length=self.md.tab_length, use_pygments=self.config['use_pygments'] ) - placeholder = self.markdown.htmlStash.store(code.hilite()) + placeholder = self.md.htmlStash.store(code.hilite()) # Clear codeblock in etree instance block.clear() # Change to p element which will later diff --git a/markdown/extensions/extra.py b/markdown/extensions/extra.py index e3ead8a..4a4909b 100644 --- a/markdown/extensions/extra.py +++ b/markdown/extensions/extra.py @@ -91,7 +91,7 @@ class MarkdownInHtmlProcessor(BlockProcessor): block[nest_index[-1][1]:], True) # nest def run(self, parent, blocks, tail=None, nest=False): - self._tag_data = self.parser.markdown.htmlStash.tag_data + self._tag_data = self.parser.md.htmlStash.tag_data self.parser.blockprocessors.tag_counter += 1 tag = self._tag_data[self.parser.blockprocessors.tag_counter] diff --git a/markdown/extensions/fenced_code.py b/markdown/extensions/fenced_code.py index 41d14fd..0ea1569 100644 --- a/markdown/extensions/fenced_code.py +++ b/markdown/extensions/fenced_code.py @@ -57,7 +57,7 @@ class FencedBlockPreprocessor(Preprocessor): # Check for code hilite extension if not self.checked_for_codehilite: - for ext in self.markdown.registeredExtensions: + for ext in self.md.registeredExtensions: if isinstance(ext, CodeHiliteExtension): self.codehilite_conf = ext.config break @@ -91,7 +91,7 @@ class FencedBlockPreprocessor(Preprocessor): code = self.CODE_WRAP % (lang, self._escape(m.group('code'))) - placeholder = self.markdown.htmlStash.store(code) + placeholder = self.md.htmlStash.store(code) text = '%s\n%s\n%s' % (text[:m.start()], placeholder, text[m.end():]) diff --git a/markdown/extensions/meta.py b/markdown/extensions/meta.py index bea50e2..688c78b 100644 --- a/markdown/extensions/meta.py +++ b/markdown/extensions/meta.py @@ -70,5 +70,5 @@ class MetaPreprocessor(Preprocessor): else: lines.insert(0, line) break # no meta data - done - self.markdown.Meta = meta + self.md.Meta = meta return lines diff --git a/markdown/extensions/smarty.py b/markdown/extensions/smarty.py index 85a2611..5ac6dc9 100644 --- a/markdown/extensions/smarty.py +++ b/markdown/extensions/smarty.py @@ -149,11 +149,11 @@ remainingDoubleQuotesRegex = '"' class SubstituteTextPattern(HtmlPattern): - def __init__(self, pattern, replace, markdown_instance): + def __init__(self, pattern, replace, md): """ Replaces matches with some text. """ HtmlPattern.__init__(self, pattern) self.replace = replace - self.markdown = markdown_instance + self.md = md def handleMatch(self, m): result = '' @@ -161,7 +161,7 @@ class SubstituteTextPattern(HtmlPattern): if isinstance(part, int): result += m.group(part) else: - result += self.markdown.htmlStash.store(part) + result += self.md.htmlStash.store(part) return result diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 74cd5f4..922463a 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -215,7 +215,7 @@ class TocTreeprocessor(Treeprocessor): return ul build_etree_ul(toc_list, div) - prettify = self.markdown.treeprocessors.get('prettify') + prettify = self.md.treeprocessors.get('prettify') if prettify: prettify.run(div) return div @@ -235,7 +235,7 @@ class TocTreeprocessor(Treeprocessor): # Do not override pre-existing ids if "id" not in el.attrib: - innertext = stashedHTML2text(text, self.markdown) + innertext = stashedHTML2text(text, self.md) el.attrib["id"] = unique(self.slugify(innertext, self.sep), used_ids) toc_tokens.append({ @@ -254,10 +254,10 @@ class TocTreeprocessor(Treeprocessor): self.replace_marker(doc, div) # serialize and attach to markdown instance. - toc = self.markdown.serializer(div) - for pp in self.markdown.postprocessors.values(): + toc = self.md.serializer(div) + for pp in self.md.postprocessors.values(): toc = pp.run(toc) - self.markdown.toc = toc + self.md.toc = toc class TocExtension(Extension): diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py index ecc3dca..a66c5bd 100644 --- a/markdown/inlinepatterns.py +++ b/markdown/inlinepatterns.py @@ -52,31 +52,31 @@ except ImportError: # pragma: no cover import htmlentitydefs as entities -def build_inlinepatterns(md_instance, **kwargs): +def build_inlinepatterns(md, **kwargs): """ Build the default set of inline patterns for Markdown. """ inlinePatterns = odict.OrderedDict() inlinePatterns["backtick"] = BacktickPattern(BACKTICK_RE) - inlinePatterns["escape"] = EscapePattern(ESCAPE_RE, md_instance) - inlinePatterns["reference"] = ReferencePattern(REFERENCE_RE, md_instance) - inlinePatterns["link"] = LinkPattern(LINK_RE, md_instance) - inlinePatterns["image_link"] = ImagePattern(IMAGE_LINK_RE, md_instance) + inlinePatterns["escape"] = EscapePattern(ESCAPE_RE, md) + inlinePatterns["reference"] = ReferencePattern(REFERENCE_RE, md) + inlinePatterns["link"] = LinkPattern(LINK_RE, md) + inlinePatterns["image_link"] = ImagePattern(IMAGE_LINK_RE, md) inlinePatterns["image_reference"] = ImageReferencePattern( - IMAGE_REFERENCE_RE, md_instance + IMAGE_REFERENCE_RE, md ) inlinePatterns["short_reference"] = ReferencePattern( - SHORT_REF_RE, md_instance + SHORT_REF_RE, md ) - inlinePatterns["autolink"] = AutolinkPattern(AUTOLINK_RE, md_instance) - inlinePatterns["automail"] = AutomailPattern(AUTOMAIL_RE, md_instance) + inlinePatterns["autolink"] = AutolinkPattern(AUTOLINK_RE, md) + inlinePatterns["automail"] = AutomailPattern(AUTOMAIL_RE, md) inlinePatterns["linebreak"] = SubstituteTagPattern(LINE_BREAK_RE, 'br') - inlinePatterns["html"] = HtmlPattern(HTML_RE, md_instance) - inlinePatterns["entity"] = HtmlPattern(ENTITY_RE, md_instance) + inlinePatterns["html"] = HtmlPattern(HTML_RE, md) + inlinePatterns["entity"] = HtmlPattern(ENTITY_RE, md) inlinePatterns["not_strong"] = SimpleTextPattern(NOT_STRONG_RE) inlinePatterns["em_strong"] = DoubleTagPattern(EM_STRONG_RE, 'strong,em') inlinePatterns["strong_em"] = DoubleTagPattern(STRONG_EM_RE, 'em,strong') inlinePatterns["strong"] = SimpleTagPattern(STRONG_RE, 'strong') inlinePatterns["emphasis"] = SimpleTagPattern(EMPHASIS_RE, 'em') - if md_instance.smart_emphasis: + if md.smart_emphasis: inlinePatterns["emphasis2"] = SimpleTagPattern(SMART_EMPHASIS_RE, 'em') else: inlinePatterns["emphasis2"] = SimpleTagPattern(EMPHASIS_2_RE, 'em') @@ -183,7 +183,7 @@ The pattern classes class Pattern(object): """Base class that inline patterns subclass. """ - def __init__(self, pattern, markdown_instance=None): + def __init__(self, pattern, md=None): """ Create an instant of an inline pattern. @@ -196,8 +196,8 @@ class Pattern(object): self.compiled_re = re.compile("^(.*?)%s(.*?)$" % pattern, re.DOTALL | re.UNICODE) - if markdown_instance: - self.markdown = markdown_instance + if md: + self.md = md def getCompiledRegExp(self): """ Return a compiled regular expression. """ @@ -222,7 +222,7 @@ class Pattern(object): def unescape(self, text): """ Return unescaped text given text with an inline placeholder. """ try: - stash = self.markdown.treeprocessors['inline'].stashed_nodes + stash = self.md.treeprocessors['inline'].stashed_nodes except KeyError: # pragma: no cover return text @@ -262,7 +262,7 @@ class EscapePattern(Pattern): def handleMatch(self, m): char = m.group(2) - if char in self.markdown.ESCAPED_CHARS: + if char in self.md.ESCAPED_CHARS: return '%s%s%s' % (util.STX, ord(char), util.ETX) else: return None @@ -322,13 +322,13 @@ class HtmlPattern(Pattern): """ Store raw inline html and return a placeholder. """ def handleMatch(self, m): rawhtml = self.unescape(m.group(2)) - place_holder = self.markdown.htmlStash.store(rawhtml) + place_holder = self.md.htmlStash.store(rawhtml) return place_holder def unescape(self, text): """ Return unescaped text given text with an inline placeholder. """ try: - stash = self.markdown.treeprocessors['inline'].stashed_nodes + stash = self.md.treeprocessors['inline'].stashed_nodes except KeyError: # pragma: no cover return text @@ -337,7 +337,7 @@ class HtmlPattern(Pattern): value = stash.get(id) if value is not None: try: - return self.markdown.serializer(value) + return self.md.serializer(value) except: return '\%s' % value @@ -380,7 +380,7 @@ class ImagePattern(LinkPattern): if len(src_parts) > 1: el.set('title', dequote(self.unescape(" ".join(src_parts[1:])))) - if self.markdown.enable_attributes: + if self.md.enable_attributes: truealt = handleAttributes(m.group(2), el) else: truealt = m.group(2) @@ -406,9 +406,9 @@ class ReferencePattern(LinkPattern): # Clean up linebreaks in id id = self.NEWLINE_CLEANUP_RE.sub(' ', id) - if id not in self.markdown.references: # ignore undefined refs + if id not in self.md.references: # ignore undefined refs return None - href, title = self.markdown.references[id] + href, title = self.md.references[id] text = m.group(2) return self.makeTag(href, title, text) @@ -432,7 +432,7 @@ class ImageReferencePattern(ReferencePattern): if title: el.set("title", title) - if self.markdown.enable_attributes: + if self.md.enable_attributes: text = handleAttributes(text, el) el.set("alt", self.unescape(text)) diff --git a/markdown/postprocessors.py b/markdown/postprocessors.py index 31c1199..4c5bf74 100644 --- a/markdown/postprocessors.py +++ b/markdown/postprocessors.py @@ -15,10 +15,10 @@ from . import odict import re -def build_postprocessors(md_instance, **kwargs): +def build_postprocessors(md, **kwargs): """ Build the default postprocessors for Markdown. """ postprocessors = odict.OrderedDict() - postprocessors["raw_html"] = RawHtmlPostprocessor(md_instance) + postprocessors["raw_html"] = RawHtmlPostprocessor(md) postprocessors["amp_substitute"] = AndSubstitutePostprocessor() postprocessors["unescape"] = UnescapePostprocessor() return postprocessors @@ -50,15 +50,15 @@ class RawHtmlPostprocessor(Postprocessor): def run(self, text): """ Iterate over html stash and restore html. """ - for i in range(self.markdown.htmlStash.html_counter): - html = self.markdown.htmlStash.rawHtmlBlocks[i] + for i in range(self.md.htmlStash.html_counter): + html = self.md.htmlStash.rawHtmlBlocks[i] if self.isblocklevel(html): text = text.replace( - "<p>%s</p>" % (self.markdown.htmlStash.get_placeholder(i)), + "<p>%s</p>" % (self.md.htmlStash.get_placeholder(i)), html + "\n" ) text = text.replace( - self.markdown.htmlStash.get_placeholder(i), html + self.md.htmlStash.get_placeholder(i), html ) return text diff --git a/markdown/preprocessors.py b/markdown/preprocessors.py index a498d80..75cfa3a 100644 --- a/markdown/preprocessors.py +++ b/markdown/preprocessors.py @@ -13,12 +13,12 @@ from . import odict import re -def build_preprocessors(md_instance, **kwargs): +def build_preprocessors(md, **kwargs): """ Build the default set of preprocessors used by Markdown. """ preprocessors = odict.OrderedDict() - preprocessors['normalize_whitespace'] = NormalizeWhitespace(md_instance) - preprocessors["html_block"] = HtmlBlockPreprocessor(md_instance) - preprocessors["reference"] = ReferencePreprocessor(md_instance) + preprocessors['normalize_whitespace'] = NormalizeWhitespace(md) + preprocessors["html_block"] = HtmlBlockPreprocessor(md) + preprocessors["reference"] = ReferencePreprocessor(md) return preprocessors @@ -50,7 +50,7 @@ class NormalizeWhitespace(Preprocessor): source = '\n'.join(lines) source = source.replace(util.STX, "").replace(util.ETX, "") source = source.replace("\r\n", "\n").replace("\r", "\n") + "\n\n" - source = source.expandtabs(self.markdown.tab_length) + source = source.expandtabs(self.md.tab_length) source = re.sub(r'(?<=\n) +\n', '\n', source) return source.split('\n') @@ -167,7 +167,7 @@ class HtmlBlockPreprocessor(Preprocessor): self._stringindex_to_listindex(data_index, items[i:]) + i if 'markdown' in attrs.keys(): items[i] = items[i][left_index:] # remove opening tag - placeholder = self.markdown.htmlStash.store_tag( + placeholder = self.md.htmlStash.store_tag( left_tag, attrs, i + 1, right_listindex + 1) items.insert(i, placeholder) if len(items) - right_listindex <= 1: # last nest, no tail @@ -178,7 +178,7 @@ class HtmlBlockPreprocessor(Preprocessor): if len(items) - right_listindex <= 1: # last element right_listindex -= 1 offset = 1 if i == right_listindex else 0 - placeholder = self.markdown.htmlStash.store('\n\n'.join( + placeholder = self.md.htmlStash.store('\n\n'.join( items[i:right_listindex + offset])) del items[i:right_listindex + offset] items.insert(i, placeholder) @@ -231,12 +231,12 @@ class HtmlBlockPreprocessor(Preprocessor): and self._equal_tags(left_tag, right_tag): if self.markdown_in_raw and 'markdown' in attrs.keys(): block = block[left_index:-len(right_tag) - 2] - new_blocks.append(self.markdown.htmlStash. + new_blocks.append(self.md.htmlStash. store_tag(left_tag, attrs, 0, 2)) new_blocks.extend([block]) else: new_blocks.append( - self.markdown.htmlStash.store(block.strip())) + self.md.htmlStash.store(block.strip())) continue else: # if is block level tag and is not complete @@ -246,7 +246,7 @@ class HtmlBlockPreprocessor(Preprocessor): in_tag = True else: new_blocks.append( - self.markdown.htmlStash.store(block.strip()) + self.md.htmlStash.store(block.strip()) ) continue @@ -274,18 +274,18 @@ class HtmlBlockPreprocessor(Preprocessor): right_index = len(items) + 3 else: right_index = len(items) + 2 - new_blocks.append(self.markdown.htmlStash.store_tag( + new_blocks.append(self.md.htmlStash.store_tag( left_tag, attrs, 0, right_index)) - placeholderslen = len(self.markdown.htmlStash.tag_data) + placeholderslen = len(self.md.htmlStash.tag_data) new_blocks.extend( self._nested_markdown_in_html(items)) - nests = len(self.markdown.htmlStash.tag_data) - \ + nests = len(self.md.htmlStash.tag_data) - \ placeholderslen - self.markdown.htmlStash.tag_data[-1 - nests][ + self.md.htmlStash.tag_data[-1 - nests][ 'right_index'] += nests - 2 else: new_blocks.append( - self.markdown.htmlStash.store('\n\n'.join(items))) + self.md.htmlStash.store('\n\n'.join(items))) items = [] if items: @@ -297,16 +297,16 @@ class HtmlBlockPreprocessor(Preprocessor): else: right_index = len(items) + 2 new_blocks.append( - self.markdown.htmlStash.store_tag( + self.md.htmlStash.store_tag( left_tag, attrs, 0, right_index)) - placeholderslen = len(self.markdown.htmlStash.tag_data) + placeholderslen = len(self.md.htmlStash.tag_data) new_blocks.extend(self._nested_markdown_in_html(items)) - nests = len(self.markdown.htmlStash.tag_data) - placeholderslen - self.markdown.htmlStash.tag_data[-1 - nests][ + nests = len(self.md.htmlStash.tag_data) - placeholderslen + self.md.htmlStash.tag_data[-1 - nests][ 'right_index'] += nests - 2 else: new_blocks.append( - self.markdown.htmlStash.store('\n\n'.join(items))) + self.md.htmlStash.store('\n\n'.join(items))) new_blocks.append('\n') new_text = "\n\n".join(new_blocks) @@ -337,7 +337,7 @@ class ReferencePreprocessor(Preprocessor): if tm: lines.pop(0) t = tm.group(2) or tm.group(3) or tm.group(4) - self.markdown.references[id] = (link, t) + self.md.references[id] = (link, t) else: new_text.append(line) diff --git a/markdown/treeprocessors.py b/markdown/treeprocessors.py index d06f192..7c79330 100644 --- a/markdown/treeprocessors.py +++ b/markdown/treeprocessors.py @@ -5,11 +5,11 @@ from . import odict from . import inlinepatterns -def build_treeprocessors(md_instance, **kwargs): +def build_treeprocessors(md, **kwargs): """ Build the default treeprocessors for Markdown. """ treeprocessors = odict.OrderedDict() - treeprocessors["inline"] = InlineProcessor(md_instance) - treeprocessors["prettify"] = PrettifyTreeprocessor(md_instance) + treeprocessors["inline"] = InlineProcessor(md) + treeprocessors["prettify"] = PrettifyTreeprocessor(md) return treeprocessors @@ -52,7 +52,7 @@ class InlineProcessor(Treeprocessor): self.__placeholder_length = 4 + len(self.__placeholder_prefix) \ + len(self.__placeholder_suffix) self.__placeholder_re = util.INLINE_PLACEHOLDER_RE - self.markdown = md + self.md = md self.inlinePatterns = md.inlinePatterns def __makePlaceholder(self, type): @@ -311,14 +311,14 @@ class InlineProcessor(Treeprocessor): stack.append(child) for element, lst in insertQueue: - if self.markdown.enable_attributes: + if self.md.enable_attributes: if element.text and isString(element.text): element.text = inlinepatterns.handleAttributes( element.text, element ) i = 0 for newChild in lst: - if self.markdown.enable_attributes: + if self.md.enable_attributes: # Processing attributes if newChild.tail and isString(newChild.tail): newChild.tail = inlinepatterns.handleAttributes( diff --git a/markdown/util.py b/markdown/util.py index 1e59834..ed3b93d 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -123,9 +123,9 @@ class AtomicString(text_type): class Processor(object): - def __init__(self, markdown_instance=None): - if markdown_instance: - self.markdown = markdown_instance + def __init__(self, md=None): + if md: + self.md = md class HtmlStash(object): |
