summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2017-02-21 02:14:57 +0000
committerrsiddharth <s@ricketyspace.net>2017-02-21 02:31:32 +0000
commita8a5fcbfdbead2093ca23010ec684dc225830596 (patch)
treee2ad9427648a15755f10765abc13b04069739e4d
parent9a5fabf9c3a92680e56ff02bf1325a86f9eec551 (diff)
mdl_style: Add LSBlockGrammar
mdl_style.LSBlockLexer uses LSBlockGrammar instead of the default mistune.BlockGrammar. * mdl_style.py (LSBlockGrammar): Add class (_inline_tags, _valid_end, _block_tag): Add variables (from mistune). (_pure_pattern): Add function (from mistune). (LSBlockLexer): Update class. * markdown_link_style/_version.py (__version__): Update variable.
-rw-r--r--markdown_link_style/_version.py2
-rw-r--r--mdl_style.py44
2 files changed, 44 insertions, 2 deletions
diff --git a/markdown_link_style/_version.py b/markdown_link_style/_version.py
index 5d3c1c3..1489137 100644
--- a/markdown_link_style/_version.py
+++ b/markdown_link_style/_version.py
@@ -18,4 +18,4 @@
# along with markdown-link-style (see COPYING). If not, see
# <http://www.gnu.org/licenses/>.
-__version__ = '0.1.0.dev2'
+__version__ = '0.1.0.dev3'
diff --git a/mdl_style.py b/mdl_style.py
index 89ed590..3f9d716 100644
--- a/mdl_style.py
+++ b/mdl_style.py
@@ -19,20 +19,62 @@
# <http://www.gnu.org/licenses/>.
import argparse
+import re
-from mistune import BlockLexer, InlineLexer, Renderer, Markdown
+from mistune import (BlockGrammar, BlockLexer, InlineLexer, Renderer,
+ Markdown)
from markdown_link_style.logging import MDLSLogger
from markdown_link_style._version import __version__
+
# Initialize logger for this module.
logger = MDLSLogger(__name__)
+# from mistune
+_inline_tags = [
+ 'a', 'em', 'strong', 'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data',
+ 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b', 'u', 'mark',
+ 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr', 'ins', 'del',
+ 'img', 'font',
+]
+_valid_end = r'(?!:/|[^\w\s@]*@)\b'
+_block_tag = r'(?!(?:%s)\b)\w+%s' % ('|'.join(_inline_tags), _valid_end)
+
+
+def _pure_pattern(regex):
+ """Function from mistune."""
+ pattern = regex.pattern
+ if pattern.startswith('^'):
+ pattern = pattern[1:]
+ return pattern
+
+
+class LSBlockGrammar(BlockGrammar):
+
+ def __init__(self):
+ # remove list_block and block_quote from paragraph
+ self.paragraph = re.compile(
+ r'^((?:[^\n]+\n?(?!'
+ r'%s|%s|%s|%s|%s|%s|%s'
+ r'))+)\n*' % (
+ _pure_pattern(self.fences).replace(r'\1', r'\2'),
+ _pure_pattern(self.hrule),
+ _pure_pattern(self.heading),
+ _pure_pattern(self.lheading),
+ _pure_pattern(self.def_links),
+ _pure_pattern(self.def_footnotes),
+ '<' + _block_tag,
+ )
+ )
+
+
class LSBlockLexer(BlockLexer):
"""Link Style Block Lexer.
"""
+ grammar_class = LSBlockGrammar
def __init__(self, rules=None, **kwargs):
super(LSBlockLexer, self).__init__(rules, **kwargs)