markdown-link-style

switch between inline and footnote link style in markdown documents.
git clone git://git.ricketyspace.net/markdown-link-style.git
Log | Files | Refs

commit a8a5fcbfdbead2093ca23010ec684dc225830596
parent 9a5fabf9c3a92680e56ff02bf1325a86f9eec551
Author: rsiddharth <s@ricketyspace.net>
Date:   Tue, 21 Feb 2017 02:14:57 +0000

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.

Diffstat:
markdown_link_style/_version.py | 2+-
mdl_style.py | 44+++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 44 insertions(+), 2 deletions(-)

diff --git 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 @@ -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)