aboutsummaryrefslogtreecommitdiffstats
path: root/markdown/extensions
diff options
context:
space:
mode:
authorIsaac Muse <faceless.shop@gmail.com>2018-01-17 18:36:34 -0700
committerWaylan Limberg <waylan.limberg@icloud.com>2018-01-17 20:36:34 -0500
commitd18c3d0acab0e7469c3284c897afcb61f9dd1fea (patch)
treece123f6ea56e105e1635126c61821d01acc82fb4 /markdown/extensions
parentde9cc42f2d8bc29d8f16058a6ae65a434caab7d6 (diff)
downloadmarkdown-d18c3d0acab0e7469c3284c897afcb61f9dd1fea.tar.gz
markdown-d18c3d0acab0e7469c3284c897afcb61f9dd1fea.tar.bz2
markdown-d18c3d0acab0e7469c3284c897afcb61f9dd1fea.zip
Flexible inline (#629)
Add new InlineProcessor class that handles inline processing much better and allows for more flexibility. This adds new InlineProcessors that no longer utilize unnecessary pretext and posttext captures. New class can accept the buffer that is being worked on and manually process the text without regex and return new replacement bounds. This helps us to handle links in a better way and handle nested brackets and logic that is too much for regular expression. The refactor also allows image links to have links/paths with spaces like links. Ref #551, #613, #590, #161.
Diffstat (limited to 'markdown/extensions')
-rw-r--r--markdown/extensions/abbr.py12
-rw-r--r--markdown/extensions/footnotes.py16
-rw-r--r--markdown/extensions/nl2br.py4
-rw-r--r--markdown/extensions/smart_strong.py10
-rw-r--r--markdown/extensions/smarty.py18
-rw-r--r--markdown/extensions/wikilinks.py16
6 files changed, 38 insertions, 38 deletions
diff --git a/markdown/extensions/abbr.py b/markdown/extensions/abbr.py
index 2553aac..5e8845b 100644
--- a/markdown/extensions/abbr.py
+++ b/markdown/extensions/abbr.py
@@ -20,7 +20,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals
from . import Extension
from ..preprocessors import Preprocessor
-from ..inlinepatterns import Pattern
+from ..inlinepatterns import InlineProcessor
from ..util import etree, AtomicString
import re
@@ -52,7 +52,7 @@ class AbbrPreprocessor(Preprocessor):
abbr = m.group('abbr').strip()
title = m.group('title').strip()
self.markdown.inlinePatterns['abbr-%s' % abbr] = \
- AbbrPattern(self._generate_pattern(abbr), title)
+ AbbrInlineProcessor(self._generate_pattern(abbr), title)
# Preserve the line to prevent raw HTML indexing issue.
# https://github.com/Python-Markdown/markdown/issues/584
new_text.append('')
@@ -76,18 +76,18 @@ class AbbrPreprocessor(Preprocessor):
return r'(?P<abbr>\b%s\b)' % (r''.join(chars))
-class AbbrPattern(Pattern):
+class AbbrInlineProcessor(InlineProcessor):
""" Abbreviation inline pattern. """
def __init__(self, pattern, title):
- super(AbbrPattern, self).__init__(pattern)
+ super(AbbrInlineProcessor, self).__init__(pattern)
self.title = title
- def handleMatch(self, m):
+ def handleMatch(self, m, data):
abbr = etree.Element('abbr')
abbr.text = AtomicString(m.group('abbr'))
abbr.set('title', self.title)
- return abbr
+ return abbr, m.start(0), m.end(0)
def makeExtension(**kwargs): # pragma: no cover
diff --git a/markdown/extensions/footnotes.py b/markdown/extensions/footnotes.py
index d16cf84..a957278 100644
--- a/markdown/extensions/footnotes.py
+++ b/markdown/extensions/footnotes.py
@@ -17,7 +17,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals
from . import Extension
from ..preprocessors import Preprocessor
-from ..inlinepatterns import Pattern
+from ..inlinepatterns import InlineProcessor
from ..treeprocessors import Treeprocessor
from ..postprocessors import Postprocessor
from .. import util
@@ -77,7 +77,7 @@ class FootnoteExtension(Extension):
# Insert an inline pattern before ImageReferencePattern
FOOTNOTE_RE = r'\[\^([^\]]*)\]' # blah blah [^1] blah
md.inlinePatterns.add(
- "footnote", FootnotePattern(FOOTNOTE_RE, self), "<reference"
+ "footnote", FootnoteInlineProcessor(FOOTNOTE_RE, self), "<reference"
)
# Insert a tree-processor that would actually add the footnote div
# This must be before all other treeprocessors (i.e., inline and
@@ -315,15 +315,15 @@ class FootnotePreprocessor(Preprocessor):
return items, i
-class FootnotePattern(Pattern):
+class FootnoteInlineProcessor(InlineProcessor):
""" InlinePattern for footnote markers in a document's body text. """
def __init__(self, pattern, footnotes):
- super(FootnotePattern, self).__init__(pattern)
+ super(FootnoteInlineProcessor, self).__init__(pattern)
self.footnotes = footnotes
- def handleMatch(self, m):
- id = m.group(2)
+ def handleMatch(self, m, data):
+ id = m.group(1)
if id in self.footnotes.footnotes.keys():
sup = util.etree.Element("sup")
a = util.etree.SubElement(sup, "a")
@@ -333,9 +333,9 @@ class FootnotePattern(Pattern):
a.set('rel', 'footnote') # invalid in HTML5
a.set('class', 'footnote-ref')
a.text = util.text_type(self.footnotes.footnotes.index(id) + 1)
- return sup
+ return sup, m.start(0), m.end(0)
else:
- return None
+ return None, None, None
class FootnotePostTreeprocessor(Treeprocessor):
diff --git a/markdown/extensions/nl2br.py b/markdown/extensions/nl2br.py
index 687d1eb..5b9373f 100644
--- a/markdown/extensions/nl2br.py
+++ b/markdown/extensions/nl2br.py
@@ -19,7 +19,7 @@ License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
from __future__ import absolute_import
from __future__ import unicode_literals
from . import Extension
-from ..inlinepatterns import SubstituteTagPattern
+from ..inlinepatterns import SubstituteTagInlineProcessor
BR_RE = r'\n'
@@ -27,7 +27,7 @@ BR_RE = r'\n'
class Nl2BrExtension(Extension):
def extendMarkdown(self, md, md_globals):
- br_tag = SubstituteTagPattern(BR_RE, 'br')
+ br_tag = SubstituteTagInlineProcessor(BR_RE, 'br')
md.inlinePatterns.add('nl', br_tag, '_end')
diff --git a/markdown/extensions/smart_strong.py b/markdown/extensions/smart_strong.py
index 1b00f84..f34531d 100644
--- a/markdown/extensions/smart_strong.py
+++ b/markdown/extensions/smart_strong.py
@@ -18,10 +18,10 @@ License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
from __future__ import absolute_import
from __future__ import unicode_literals
from . import Extension
-from ..inlinepatterns import SimpleTagPattern
+from ..inlinepatterns import SimpleTagInlineProcessor
-SMART_STRONG_RE = r'(?<!\w)(_{2})(?!_)(.+?)(?<!_)\2(?!\w)'
-STRONG_RE = r'(\*{2})(.+?)\2'
+SMART_STRONG_RE = r'(?<!\w)(_{2})(?!_)(.+?)(?<!_)\1(?!\w)'
+STRONG_RE = r'(\*{2})(.+?)\1'
class SmartEmphasisExtension(Extension):
@@ -29,10 +29,10 @@ class SmartEmphasisExtension(Extension):
def extendMarkdown(self, md, md_globals):
""" Modify inline patterns. """
- md.inlinePatterns['strong'] = SimpleTagPattern(STRONG_RE, 'strong')
+ md.inlinePatterns['strong'] = SimpleTagInlineProcessor(STRONG_RE, 'strong')
md.inlinePatterns.add(
'strong2',
- SimpleTagPattern(SMART_STRONG_RE, 'strong'),
+ SimpleTagInlineProcessor(SMART_STRONG_RE, 'strong'),
'>emphasis2'
)
diff --git a/markdown/extensions/smarty.py b/markdown/extensions/smarty.py
index 35c78a8..189651f 100644
--- a/markdown/extensions/smarty.py
+++ b/markdown/extensions/smarty.py
@@ -83,7 +83,7 @@ smartypants.py license:
from __future__ import unicode_literals
from . import Extension
-from ..inlinepatterns import HtmlPattern, HTML_RE
+from ..inlinepatterns import HtmlInlineProcessor, HTML_RE
from ..odict import OrderedDict
from ..treeprocessors import InlineProcessor
@@ -150,21 +150,21 @@ remainingDoubleQuotesRegex = r'"'
HTML_STRICT_RE = HTML_RE + r'(?!\>)'
-class SubstituteTextPattern(HtmlPattern):
+class SubstituteTextPattern(HtmlInlineProcessor):
def __init__(self, pattern, replace, markdown_instance):
""" Replaces matches with some text. """
- HtmlPattern.__init__(self, pattern)
+ HtmlInlineProcessor.__init__(self, pattern)
self.replace = replace
self.markdown = markdown_instance
- def handleMatch(self, m):
+ def handleMatch(self, m, data):
result = ''
for part in self.replace:
if isinstance(part, int):
result += m.group(part)
else:
result += self.markdown.htmlStash.store(part)
- return result
+ return result, m.start(0), m.end(0)
class SmartyExtension(Extension):
@@ -233,11 +233,11 @@ class SmartyExtension(Extension):
(doubleQuoteSetsRe, (ldquo + lsquo,)),
(singleQuoteSetsRe, (lsquo + ldquo,)),
(decadeAbbrRe, (rsquo,)),
- (openingSingleQuotesRegex, (2, lsquo)),
+ (openingSingleQuotesRegex, (1, lsquo)),
(closingSingleQuotesRegex, (rsquo,)),
- (closingSingleQuotesRegex2, (rsquo, 2)),
+ (closingSingleQuotesRegex2, (rsquo, 1)),
(remainingSingleQuotesRegex, (lsquo,)),
- (openingDoubleQuotesRegex, (2, ldquo)),
+ (openingDoubleQuotesRegex, (1, ldquo)),
(closingDoubleQuotesRegex, (rdquo,)),
(closingDoubleQuotesRegex2, (rdquo,)),
(remainingDoubleQuotesRegex, (ldquo,))
@@ -255,7 +255,7 @@ class SmartyExtension(Extension):
self.educateAngledQuotes(md)
# Override HTML_RE from inlinepatterns.py so that it does not
# process tags with duplicate closing quotes.
- md.inlinePatterns["html"] = HtmlPattern(HTML_STRICT_RE, md)
+ md.inlinePatterns["html"] = HtmlInlineProcessor(HTML_STRICT_RE, md)
if configs['smart_dashes']:
self.educateDashes(md)
inlineProcessor = InlineProcessor(md)
diff --git a/markdown/extensions/wikilinks.py b/markdown/extensions/wikilinks.py
index a4a3515..b535d9c 100644
--- a/markdown/extensions/wikilinks.py
+++ b/markdown/extensions/wikilinks.py
@@ -18,7 +18,7 @@ License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
from __future__ import absolute_import
from __future__ import unicode_literals
from . import Extension
-from ..inlinepatterns import Pattern
+from ..inlinepatterns import InlineProcessor
from ..util import etree
import re
@@ -46,20 +46,20 @@ class WikiLinkExtension(Extension):
# append to end of inline patterns
WIKILINK_RE = r'\[\[([\w0-9_ -]+)\]\]'
- wikilinkPattern = WikiLinks(WIKILINK_RE, self.getConfigs())
+ wikilinkPattern = WikiLinksInlineProcessor(WIKILINK_RE, self.getConfigs())
wikilinkPattern.md = md
md.inlinePatterns.add('wikilink', wikilinkPattern, "<not_strong")
-class WikiLinks(Pattern):
+class WikiLinksInlineProcessor(InlineProcessor):
def __init__(self, pattern, config):
- super(WikiLinks, self).__init__(pattern)
+ super(WikiLinksInlineProcessor, self).__init__(pattern)
self.config = config
- def handleMatch(self, m):
- if m.group(2).strip():
+ def handleMatch(self, m, data):
+ if m.group(1).strip():
base_url, end_url, html_class = self._getMeta()
- label = m.group(2).strip()
+ label = m.group(1).strip()
url = self.config['build_url'](label, base_url, end_url)
a = etree.Element('a')
a.text = label
@@ -68,7 +68,7 @@ class WikiLinks(Pattern):
a.set('class', html_class)
else:
a = ''
- return a
+ return a, m.start(0), m.end(0)
def _getMeta(self):
""" Return meta data or config data. """