summaryrefslogtreecommitdiffstats
path: root/mdl_style.py
diff options
context:
space:
mode:
Diffstat (limited to 'mdl_style.py')
-rw-r--r--mdl_style.py153
1 files changed, 94 insertions, 59 deletions
diff --git a/mdl_style.py b/mdl_style.py
index eb6ed12..86a7b0b 100644
--- a/mdl_style.py
+++ b/mdl_style.py
@@ -21,8 +21,7 @@
import argparse
import re
-from mistune import (BlockGrammar, 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__
@@ -34,38 +33,67 @@ 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',
+ "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)
+_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('^'):
+ 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'),
+ 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,
+ "<" + _block_tag,
)
)
@@ -74,13 +102,14 @@ class LSBlockLexer(BlockLexer):
"""Link Style Block Lexer.
"""
+
grammar_class = LSBlockGrammar
def __init__(self, rules=None, **kwargs):
super(LSBlockLexer, self).__init__(rules, **kwargs)
# Only parse these block rules.
- self.default_rules = ['def_links', 'paragraph', 'text']
+ self.default_rules = ["def_links", "paragraph", "text"]
class LSInlineLexer(InlineLexer):
@@ -92,7 +121,7 @@ class LSInlineLexer(InlineLexer):
super(LSInlineLexer, self).__init__(renderer, rules, **kwargs)
# Only parse these inline rules
- self.default_rules = ['autolink', 'link', 'reflink', 'text']
+ self.default_rules = ["autolink", "link", "reflink", "text"]
class LSRenderer(Renderer):
@@ -104,16 +133,16 @@ class LSRenderer(Renderer):
super(LSRenderer, self).__init__(**kwargs)
# Link style is either 'inline' or 'footnote'.
- self.link_style = self.options.get('link_style')
+ self.link_style = self.options.get("link_style")
- self.fn_lnk_num = 0 # footnote style link number
- self.fn_lnk_refs = [] # footnote style link refs
+ self.fn_lnk_num = 0 # footnote style link number
+ self.fn_lnk_refs = [] # footnote style link refs
def text(self, text):
return text
def autolink(self, link, is_email=False):
- return '<{}>'.format(link)
+ return "<{}>".format(link)
def paragraph(self, text):
p = text
@@ -121,9 +150,9 @@ class LSRenderer(Renderer):
if fn_refs:
# Insert footnote refs, if any, after paragraph.
- return '\n{}\n\n{}'.format(p, fn_refs)
+ return "\n{}\n\n{}".format(p, fn_refs)
- return '\n{}\n'.format(p)
+ return "\n{}\n".format(p)
def link(self, link, title, text):
link_text = self._stylize_link(link, title, text)
@@ -132,10 +161,10 @@ class LSRenderer(Renderer):
def image(self, src, title, text):
# Markup for images are same as links, except it is prefixed
# with a bang (!).
- return '{}{}'.format('!', self.link(src, title, text))
+ return "{}{}".format("!", self.link(src, title, text))
def _stylize_link(self, link, title, text):
- if self.link_style == 'inline':
+ if self.link_style == "inline":
return self._gen_inline_link(link, title, text)
else:
return self._gen_footnote_link(link, title, text)
@@ -144,11 +173,11 @@ class LSRenderer(Renderer):
if title:
return '[{}]({} "{}")'.format(text, link, title)
else:
- return '[{}]({})'.format(text, link)
+ return "[{}]({})".format(text, link)
def _gen_footnote_link(self, link, title, text):
fn_num = self._st_fn_ref(link, title)
- return '[{}][{}]'.format(text, fn_num)
+ return "[{}][{}]".format(text, fn_num)
def _st_fn_ref(self, link, title):
"""Store footnote link reference.
@@ -157,9 +186,9 @@ class LSRenderer(Renderer):
fn_num = self._get_fn_lnk_num()
if title:
- fn_ref = '[{}]: {} ({})'.format(fn_num, link, title)
+ fn_ref = "[{}]: {} ({})".format(fn_num, link, title)
else:
- fn_ref = '[{}]: {}'.format(fn_num, link)
+ fn_ref = "[{}]: {}".format(fn_num, link)
self.fn_lnk_refs.append(fn_ref)
return fn_num
@@ -176,10 +205,10 @@ class LSRenderer(Renderer):
"""Pop all footnote refs and return them as a string.
"""
- refs = ''
+ refs = ""
for ref in self.fn_lnk_refs:
- refs += '{}\n'.format(ref)
+ refs += "{}\n".format(ref)
# Empty fn_lnk_refs
self.fn_lnk_refs = []
@@ -191,9 +220,8 @@ class LSMarkdown(Markdown):
"""Link Style Markdown parser.
"""
- def __init__(self, renderer=None, inline=None, block=None,
- **kwargs):
- link_style = kwargs.get('link_style') or 'inline'
+ def __init__(self, renderer=None, inline=None, block=None, **kwargs):
+ link_style = kwargs.get("link_style") or "inline"
if not renderer:
renderer = LSRenderer(link_style=link_style)
@@ -202,8 +230,7 @@ class LSMarkdown(Markdown):
if not block:
block = LSBlockLexer()
- super(LSMarkdown, self).__init__(renderer, inline, block,
- **kwargs)
+ super(LSMarkdown, self).__init__(renderer, inline, block, **kwargs)
def parse(self, text):
# Reset footnote link variables.
@@ -214,7 +241,7 @@ class LSMarkdown(Markdown):
out = super(LSMarkdown, self).parse(text)
# Spit out.
- return out.lstrip('\n')
+ return out.lstrip("\n")
class LinkStyler(object):
@@ -222,7 +249,7 @@ class LinkStyler(object):
"""
- def __init__(self, link_style='inline'):
+ def __init__(self, link_style="inline"):
self.style = link_style
def __call__(self, file_):
@@ -236,15 +263,15 @@ class LinkStyler(object):
def _write_to(file_, content):
- """Write `content` to `file_`.
+ """Write `content` to `file_`.
`file_` is expected to be a sub-class of `io.TextIOBase`.
- """
- file_.truncate(0)
- file_.seek(0)
- file_.write(content)
- file_.flush()
- file_.close()
+ """
+ file_.truncate(0)
+ file_.seek(0)
+ file_.write(content)
+ file_.flush()
+ file_.close()
def _mdl_stylize(args):
@@ -253,26 +280,34 @@ def _mdl_stylize(args):
if args.out_file:
args.in_file.close()
- _write_to(open(args.out_file, 'wt'), stylized_content)
+ _write_to(open(args.out_file, "wt"), stylized_content)
else:
_write_to(args.in_file, stylized_content)
def _get_args(args=None):
parser = argparse.ArgumentParser()
- parser.add_argument('--version', action='version',
- version=__version__)
- parser.add_argument('link_style', choices=['inline', 'footnote'],
- help='markdown link style.')
- parser.add_argument('in_file', type=argparse.FileType('rt+'),
- help='path to markdown file.')
- parser.add_argument('out_file', nargs='?',
- type=str,
- default=None,
- help= ' '.join(['path to output file.',
- 'if it is not given, the output is',
- 'directly written to the original',
- 'in_file.']))
+ parser.add_argument("--version", action="version", version=__version__)
+ parser.add_argument(
+ "link_style", choices=["inline", "footnote"], help="markdown link style."
+ )
+ parser.add_argument(
+ "in_file", type=argparse.FileType("rt+"), help="path to markdown file."
+ )
+ parser.add_argument(
+ "out_file",
+ nargs="?",
+ type=str,
+ default=None,
+ help=" ".join(
+ [
+ "path to output file.",
+ "if it is not given, the output is",
+ "directly written to the original",
+ "in_file.",
+ ]
+ ),
+ )
return parser.parse_args(args)