diff options
author | rsiddharth <s@ricketyspace.net> | 2017-02-19 01:47:54 +0000 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2017-02-19 01:47:54 +0000 |
commit | 74b4642a96ebbb4b8f6860abb2e782f72f2fc2af (patch) | |
tree | 83db9af698f268df69763e1ef743e138e461f622 /mdl_style.py | |
parent | fb0a03dfabb29dae899c7d2f1dc94ff38a3240c6 (diff) |
Add initial version the script.
* markdown_link_style/_version.py (__version__): Update version.
* markdown_link_style/logging.py: New module
* mdl_style.py (logger): Add variable.
(LSBlockLexer, LSInlineLexer, LSRenderer, LSMarkdown)
(LinkStyler): Add classes.
(_mdl_stylize, _get_args): Add functions.
(main): Update function.
* tests/__init__.py: Add file.
* tests/data/autolink_00-expected.md: Add file.
* tests/data/autolink_00.md: Add file.
* tests/data/footnote_link_style_00-expected.md: Add file.
* tests/data/footnote_link_style_00.md: Add file.
* tests/data/inline_link_style_00-expected.md: Add file.
* tests/data/inline_link_style_00.md: Add file.
* tests/test_mdl_style.py (TestMdlStyle): Remove class.
(TestLSRendererIL, TestLSRendererFN): Add classes.
Diffstat (limited to 'mdl_style.py')
-rw-r--r-- | mdl_style.py | 177 |
1 files changed, 176 insertions, 1 deletions
diff --git a/mdl_style.py b/mdl_style.py index 44cafab..c8573ed 100644 --- a/mdl_style.py +++ b/mdl_style.py @@ -18,5 +18,180 @@ # along with markdown-link-style (see COPYING). If not, see # <http://www.gnu.org/licenses/>. +import argparse + +from mistune import 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__) + + +class LSBlockLexer(BlockLexer): + """Link Style Block Lexer. + + """ + + def __init__(self, rules=None, **kwargs): + super(LSBlockLexer, self).__init__(rules, **kwargs) + + # Only parse these block rules. + self.default_rules = ['def_links', 'paragraph', 'text'] + + +class LSInlineLexer(InlineLexer): + """Link Style Inline Lexer. + + """ + + def __init__(self, renderer, rules=None, **kwargs): + super(LSInlineLexer, self).__init__(renderer, rules, **kwargs) + + # Only parse these inline rules + self.default_rules = ['autolink', 'link', 'reflink', 'text'] + + +class LSRenderer(Renderer): + """Link Style Renderer. + + """ + + def __init__(self, **kwargs): + super(LSRenderer, self).__init__(**kwargs) + + # Link style is either 'inline' or 'footnote'. + 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 + + def autolink(self, link, is_email=False): + return '<{}>'.format(link) + + def paragraph(self, text): + p = text + fn_refs = self._pop_fn_refs() + + if fn_refs: + # Insert footnote refs, if any, after paragraph. + return '\n{}\n\n{}'.format(p, fn_refs) + + return '\n{}\n'.format(p) + + def link(self, link, title, text): + link_text = self._stylize_link(link, title, text) + return link_text + + def _stylize_link(self, link, title, text): + if self.link_style == 'inline': + return self._gen_inline_link(link, title, text) + else: + return self._gen_footnote_link(link, title, text) + + def _gen_inline_link(self, link, title, text): + if title: + return '[{}]({} "{}")'.format(text, link, title) + else: + 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) + + def _st_fn_ref(self, link, title): + """Store footnote link reference. + + """ + fn_num = self._get_fn_lnk_num() + + if title: + fn_ref = '[{}]: {} ({})'.format(fn_num, link, title) + else: + fn_ref = '[{}]: {}'.format(fn_num, link) + + self.fn_lnk_refs.append(fn_ref) + return fn_num + + def _get_fn_lnk_num(self): + """Get footnote link number. + + """ + fn_num = self.fn_lnk_num + self.fn_lnk_num = self.fn_lnk_num + 1 + return fn_num + + def _pop_fn_refs(self): + """Pop all footnote refs and return them as a string. + + """ + refs = '' + + for ref in self.fn_lnk_refs: + refs += '{}\n'.format(ref) + + # empty fn_lnk_refs + self.fn_lnk_refs = [] + + return refs + + +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' + + if not renderer: + renderer = LSRenderer(link_style=link_style) + if not inline: + inline = LSInlineLexer(renderer) + if not block: + block = LSBlockLexer() + + super(LSMarkdown, self).__init__(renderer, inline, block, **kwargs) + + def parse(self, text): + out = super(LSMarkdown, self).parse(text) + + return out.lstrip('\n') + + +class LinkStyler(object): + """Markdown Link Styler. + + """ + + def __init__(self, link_style='inline'): + self.style = link_style + + def __call__(self, file_): + return self._link_stylize(file_) + + def _link_stylize(self, file_): + text = file_.read() + md = LSMarkdown(link_style=self.style) + + return md(text) + + +def _mdl_stylize(args): + ls = LinkStyler(args.link_style) + print(ls(args.file), end='') + + +def _get_args(): + 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('file', type=argparse.FileType('r'), + help="Path to Markdown file.") + return parser.parse_args() + + def main(): - print('md-lnk-style main function') + args = _get_args() + _mdl_stylize(args) |