summaryrefslogtreecommitdiffstats
path: root/mdl_style.py
blob: 86a7b0bd0f8c70283cfaf423678f34c5e31fd7e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- coding: utf-8 -*-
#
#   Copyright © 2017 markdown-link-style contributors.
#
#    This file is part of markdown-link-style.
#
#   markdown-link-style is free software: you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 3 of
#   the License, or (at your option) any later version.
#
#   markdown-link-style is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#   See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with markdown-link-style (see COPYING).  If not, see
#   <http://www.gnu.org/licenses/>.

import argparse
import re

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)

        # 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 text(self, text):
        return text

    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 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))

    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):
        # Reset footnote link variables.
        self.renderer.fn_lnk_num = 0
        self.renderer.fn_lnk_refs = []

        # Parse text.
        out = super(LSMarkdown, self).parse(text)

        # Spit out.
        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 _write_to(file_, content):
    """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()


def _mdl_stylize(args):
    ls = LinkStyler(args.link_style)
    stylized_content = ls(args.in_file)

    if args.out_file:
        args.in_file.close()
        _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.",
            ]
        ),
    )

    return parser.parse_args(args)


def main():
    args = _get_args()
    _mdl_stylize(args)