forked from miyakogi/m2r
-
Notifications
You must be signed in to change notification settings - Fork 27
/
m2r2.py
732 lines (613 loc) · 22.9 KB
/
m2r2.py
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os
import os.path
import re
import sys
from argparse import ArgumentParser, Namespace
import mistune
from docutils import io, nodes, statemachine, utils
from docutils.parsers import rst
from docutils.utils import column_width
from pkg_resources import get_distribution
__version__ = get_distribution("m2r2").version
if sys.version_info < (3,):
from codecs import open as _open
from urlparse import urlparse
else:
_open = open
from urllib.parse import urlparse
_is_sphinx = False
prolog = """\
.. role:: raw-html-m2r(raw)
:format: html
"""
# for command-line use
parser = ArgumentParser()
options = Namespace()
parser.add_argument("input_file", nargs="*", help="files to convert to reST format")
parser.add_argument(
"--overwrite",
action="store_true",
default=False,
help="overwrite output file without confirmaion",
)
parser.add_argument(
"--dry-run",
action="store_true",
default=False,
help="print conversion result and not save output file",
)
parser.add_argument(
"--no-underscore-emphasis",
action="store_true",
default=False,
help="do not use underscore (_) for emphasis",
)
parser.add_argument(
"--parse-relative-links",
action="store_true",
default=False,
help="parse relative links into ref or doc directives",
)
parser.add_argument(
"--anonymous-references",
action="store_true",
default=False,
help="use anonymous references in generated rst",
)
parser.add_argument(
"--disable-inline-math",
action="store_true",
default=False,
help="disable parsing inline math",
)
def parse_options():
parser.parse_known_args(namespace=options)
class RestBlockGrammar(mistune.BlockGrammar):
directive = re.compile(
r"^( *\.\..*?)\n(?=\S)",
re.DOTALL | re.MULTILINE,
)
oneline_directive = re.compile(
r"^( *\.\..*?)$",
re.DOTALL | re.MULTILINE,
)
rest_code_block = re.compile(
r"^::\s*$",
re.DOTALL | re.MULTILINE,
)
class RestBlockLexer(mistune.BlockLexer):
grammar_class = RestBlockGrammar
default_rules = [
"directive",
"oneline_directive",
"rest_code_block",
] + mistune.BlockLexer.default_rules
def parse_directive(self, m):
self.tokens.append({"type": "directive", "text": m.group(1)})
def parse_oneline_directive(self, m):
# reuse directive output
self.tokens.append({"type": "directive", "text": m.group(1)})
def parse_rest_code_block(self, m):
self.tokens.append({"type": "rest_code_block"})
class RestInlineGrammar(mistune.InlineGrammar):
image_link = re.compile(
r"\[!\[(?P<alt>.*?)\]\((?P<url>.*?)\).*?\]\((?P<target>.*?)\)"
)
rest_role = re.compile(r":.*?:`.*?`|`[^`]+`:.*?:")
rest_link = re.compile(r"`[^`]*?`_")
inline_math = re.compile(r"`\$(.*?)\$`")
eol_literal_marker = re.compile(r"(\s+)?::\s*$")
# add colon and space as special text
text = re.compile(r"^[\s\S]+?(?=[\\<!\[:_*`~ ]|https?://| {2,}\n|$)")
# __word__ or **word**
double_emphasis = re.compile(r"^([_*]){2}(?P<text>[\s\S]+?)\1{2}(?!\1)")
# _word_ or *word*
emphasis = re.compile(
r"^\b_((?:__|[^_])+?)_\b" # _word_
r"|"
r"^\*(?P<text>(?:\*\*|[^\*])+?)\*(?!\*)" # *word*
)
def no_underscore_emphasis(self):
self.double_emphasis = re.compile(
r"^\*{2}(?P<text>[\s\S]+?)\*{2}(?!\*)" # **word**
)
self.emphasis = re.compile(r"^\*(?P<text>(?:\*\*|[^\*])+?)\*(?!\*)") # *word*
class RestInlineLexer(mistune.InlineLexer):
grammar_class = RestInlineGrammar
default_rules = [
"image_link",
"rest_role",
"rest_link",
"eol_literal_marker",
] + mistune.InlineLexer.default_rules
def __init__(self, *args, **kwargs):
no_underscore_emphasis = kwargs.pop("no_underscore_emphasis", False)
disable_inline_math = kwargs.pop("disable_inline_math", False)
super(RestInlineLexer, self).__init__(*args, **kwargs)
if not _is_sphinx:
parse_options()
if no_underscore_emphasis or getattr(options, "no_underscore_emphasis", False):
self.rules.no_underscore_emphasis()
inline_maths = "inline_math" in self.default_rules
if disable_inline_math or getattr(options, "disable_inline_math", False):
if inline_maths:
self.default_rules.remove("inline_math")
elif not inline_maths:
self.default_rules.insert(0, "inline_math")
def output_double_emphasis(self, m):
# may include code span
text = self.output(m.group("text"))
return self.renderer.double_emphasis(text)
def output_emphasis(self, m):
# may include code span
text = self.output(m.group("text") or m.group(1))
return self.renderer.emphasis(text)
def output_image_link(self, m):
"""Pass through rest role."""
return self.renderer.image_link(
m.group("url"), m.group("target"), m.group("alt")
)
def output_rest_role(self, m):
"""Pass through rest role."""
return self.renderer.rest_role(m.group(0))
def output_rest_link(self, m):
"""Pass through rest link."""
return self.renderer.rest_link(m.group(0))
def output_inline_math(self, m):
"""Pass through rest link."""
return self.renderer.inline_math(m.group(1))
def output_eol_literal_marker(self, m):
"""Pass through rest link."""
marker = ":" if m.group(1) is None else ""
return self.renderer.eol_literal_marker(marker)
class RestRenderer(mistune.Renderer):
_include_raw_html = False
list_indent_re = re.compile(r"^(\s*(#\.|\*)\s)")
indent = " " * 3
list_marker = "{#__rest_list_mark__#}"
hmarks = {
1: "=",
2: "-",
3: "^",
4: "~",
5: '"',
6: "#",
}
def __init__(self, *args, **kwargs):
self.parse_relative_links = kwargs.pop("parse_relative_links", False)
self.anonymous_references = kwargs.pop("anonymous_references", False)
self.use_mermaid = kwargs.pop("use_mermaid", False)
super(RestRenderer, self).__init__(*args, **kwargs)
if not _is_sphinx:
parse_options()
if getattr(options, "parse_relative_links", False):
self.parse_relative_links = options.parse_relative_links
if getattr(options, "anonymous_references", False):
self.anonymous_references = options.anonymous_references
def _indent_block(self, block):
return "\n".join(
self.indent + line if line else "" for line in block.splitlines()
)
def _raw_html(self, html):
self._include_raw_html = True
return r"\ :raw-html-m2r:`{}`\ ".format(html)
def block_code(self, code, lang=None):
if lang == "math":
first_line = "\n.. math::\n\n"
elif lang == "mermaid" and self.use_mermaid:
first_line = "\n.. mermaid::\n\n"
elif lang:
first_line = "\n.. code-block:: {}\n\n".format(lang)
elif _is_sphinx:
first_line = "\n::\n\n"
else:
first_line = "\n.. code-block::\n\n"
return first_line + self._indent_block(code) + "\n"
def block_quote(self, text):
# text includes some empty line
return "\n..\n\n{}\n\n".format(self._indent_block(text.strip("\n")))
def block_html(self, html):
"""Rendering block level pure html content.
:param html: text content of the html snippet.
"""
return "\n\n.. raw:: html\n\n" + self._indent_block(html) + "\n\n"
def header(self, text, level, raw=None):
"""Rendering header/heading tags like ``<h1>`` ``<h2>``.
:param text: rendered text content for the header.
:param level: a number for the header level, for example: 1.
:param raw: raw text content of the header.
"""
return "\n{0}\n{1}\n".format(text, self.hmarks[level] * column_width(text))
def hrule(self):
"""Rendering method for ``<hr>`` tag."""
return "\n----\n"
def list(self, body, ordered=True):
"""Rendering list tags like ``<ul>`` and ``<ol>``.
:param body: body contents of the list.
:param ordered: whether this list is ordered or not.
"""
mark = "#. " if ordered else "* "
lines = body.splitlines()
for i, line in enumerate(lines):
if line and not line.startswith(self.list_marker):
lines[i] = " " * len(mark) + line
return "\n{}\n".format("\n".join(lines)).replace(self.list_marker, mark)
def list_item(self, text):
"""Rendering list item snippet. Like ``<li>``."""
return "\n" + self.list_marker + text
def paragraph(self, text):
"""Rendering paragraph tags. Like ``<p>``."""
return "\n" + text + "\n"
def table(self, header, body):
"""Rendering table element. Wrap header and body in it.
:param header: header part of the table.
:param body: body part of the table.
"""
table = "\n.. list-table::\n"
if header and not header.isspace():
table = (
table
+ self.indent
+ ":header-rows: 1\n\n"
+ self._indent_block(header)
+ "\n"
)
else:
table = table + "\n"
table = table + self._indent_block(body) + "\n\n"
return table
def table_row(self, content):
"""Rendering a table row. Like ``<tr>``.
:param content: content of current table row.
"""
contents = content.splitlines()
if not contents:
return ""
clist = ["* " + contents[0]]
if len(contents) > 1:
for c in contents[1:]:
clist.append(" " + c)
return "\n".join(clist) + "\n"
def table_cell(self, content, **flags):
"""Rendering a table cell. Like ``<th>`` ``<td>``.
:param content: content of current table cell.
:param header: whether this is header or not.
:param align: align of current table cell.
"""
return "- " + content + "\n"
def double_emphasis(self, text):
"""Rendering **strong** text.
:param text: text content for emphasis.
"""
return r"\ **{}**\ ".format(text)
def emphasis(self, text):
"""Rendering *emphasis* text.
:param text: text content for emphasis.
"""
return r"\ *{}*\ ".format(text)
def codespan(self, text):
"""Rendering inline `code` text.
:param text: text content for inline code.
"""
if "``" not in text:
return r"\ ``{}``\ ".format(text)
else:
# actually, docutils split spaces in literal
return self._raw_html(
'<code class="docutils literal">'
'<span class="pre">{}</span>'
"</code>".format(text.replace("`", "`"))
)
def linebreak(self):
"""Rendering line break like ``<br>``."""
if self.options.get("use_xhtml"):
return self._raw_html("<br />") + "\n"
return self._raw_html("<br>") + "\n"
def strikethrough(self, text):
"""Rendering ~~strikethrough~~ text.
:param text: text content for strikethrough.
"""
return self._raw_html("<del>{}</del>".format(text))
def text(self, text):
"""Rendering unformatted text.
:param text: text content.
"""
return text
def autolink(self, link, is_email=False):
"""Rendering a given link or email address.
:param link: link content or email address.
:param is_email: whether this is an email or not.
"""
return link
def link(self, link, title, text):
"""Rendering a given link with content and title.
:param link: href link for ``<a>`` tag.
:param title: title content for `title` attribute.
:param text: text content for description.
"""
if self.anonymous_references:
underscore = "__"
else:
underscore = "_"
if title:
return self._raw_html(
'<a href="{link}" title="{title}">{text}</a>'.format(
link=link, title=title, text=text
)
)
if not self.parse_relative_links:
return r"\ `{text} <{target}>`{underscore}\ ".format(
target=link, text=text, underscore=underscore
)
else:
url_info = urlparse(link)
if url_info.scheme:
return r"\ `{text} <{target}>`{underscore}\ ".format(
target=link, text=text, underscore=underscore
)
else:
link_type = "doc"
anchor = url_info.fragment
if url_info.fragment:
if url_info.path:
# Can't link to anchors via doc directive.
anchor = ""
else:
# Example: [text](#anchor)
link_type = "ref"
doc_link = "{doc_name}{anchor}".format(
# splittext approach works whether or not path is set. It
# will return an empty string if unset, which leads to
# anchor only ref.
doc_name=os.path.splitext(url_info.path)[0],
anchor=anchor,
)
return r"\ :{link_type}:`{text} <{doc_link}>`\ ".format(
link_type=link_type, doc_link=doc_link, text=text
)
def image(self, src, title, text):
"""Rendering a image with title and text.
:param src: source link of the image.
:param title: title text of the image.
:param text: alt text of the image.
"""
# rst does not support title option
# and I couldn't find title attribute in HTML standard
return "\n".join(
[
"",
".. image:: {}".format(src),
" :target: {}".format(src),
" :alt: {}".format(text),
"",
]
)
def inline_html(self, html):
"""Rendering span level pure html content.
:param html: text content of the html snippet.
"""
return self._raw_html(html)
def newline(self):
"""Rendering newline element."""
return ""
def footnote_ref(self, key, index):
"""Rendering the ref anchor of a footnote.
:param key: identity key for the footnote.
:param index: the index count of current footnote.
"""
return r"\ [#fn-{}]_\ ".format(key)
def footnote_item(self, key, text):
"""Rendering a footnote item.
:param key: identity key for the footnote.
:param text: text content of the footnote.
"""
return ".. [#fn-{0}] {1}\n".format(key, text.strip())
def footnotes(self, text):
"""Wrapper for all footnotes.
:param text: contents of all footnotes.
"""
if text:
return "\n\n" + text
else:
return ""
"""Below outputs are for rst."""
def image_link(self, url, target, alt):
return "\n".join(
[
"",
".. image:: {}".format(url),
" :target: {}".format(target),
" :alt: {}".format(alt),
"",
]
)
def rest_role(self, text):
return text
def rest_link(self, text):
return text
def inline_math(self, math):
"""Extension of recommonmark."""
return r":math:`{}`".format(math)
def eol_literal_marker(self, marker):
"""Extension of recommonmark."""
return marker
def directive(self, text):
return "\n" + text
def rest_code_block(self):
return "\n\n"
class M2R(mistune.Markdown):
def __init__(
self, renderer=None, inline=RestInlineLexer, block=RestBlockLexer, **kwargs
):
if renderer is None:
renderer = RestRenderer(**kwargs)
super(M2R, self).__init__(renderer, inline=inline, block=block, **kwargs)
def parse(self, text):
output = super(M2R, self).parse(text)
return self.post_process(output)
def output_directive(self):
return self.renderer.directive(self.token["text"])
def output_rest_code_block(self):
return self.renderer.rest_code_block()
def post_process(self, text):
output = (
text.replace("\\ \n", "\n")
.replace("\n\\ ", "\n")
.replace(" \\ ", " ")
.replace("\\ ", " ")
.replace("\\ .", ".")
)
if self.renderer._include_raw_html:
return prolog + output
else:
return output
class M2RParser(rst.Parser, object):
# Explicitly tell supported formats to sphinx
supported = ("markdown", "md", "mkd")
def parse(self, inputstrings, document):
if isinstance(inputstrings, statemachine.StringList):
inputstring = "\n".join(inputstrings)
else:
inputstring = inputstrings
config = document.settings.env.config
converter = M2R(
no_underscore_emphasis=config.no_underscore_emphasis,
parse_relative_links=config.m2r_parse_relative_links,
anonymous_references=config.m2r_anonymous_references,
disable_inline_math=config.m2r_disable_inline_math,
use_mermaid=config.m2r_use_mermaid,
)
super(M2RParser, self).parse(converter(inputstring), document)
class MdInclude(rst.Directive):
"""Directive class to include markdown in sphinx.
Load a file and convert it to rst and insert as a node. Currently
directive-specific options are not implemented.
"""
required_arguments = 1
optional_arguments = 0
option_spec = {
"start-line": int,
"end-line": int,
}
def run(self):
"""Most of this method is from ``docutils.parser.rst.Directive``.
docutils version: 0.12
"""
if not self.state.document.settings.file_insertion_enabled:
raise self.warning('"%s" directive disabled.' % self.name)
source = self.state_machine.input_lines.source(
self.lineno - self.state_machine.input_offset - 1
)
source_dir = os.path.dirname(os.path.abspath(source))
path = rst.directives.path(self.arguments[0])
path = os.path.normpath(os.path.join(source_dir, path))
path = utils.relative_path(None, path)
path = nodes.reprunicode(path)
# get options (currently not use directive-specific options)
encoding = self.options.get(
"encoding", self.state.document.settings.input_encoding
)
e_handler = self.state.document.settings.input_encoding_error_handler
tab_width = self.options.get(
"tab-width", self.state.document.settings.tab_width
)
# open the including file
try:
self.state.document.settings.record_dependencies.add(path)
include_file = io.FileInput(
source_path=path, encoding=encoding, error_handler=e_handler
)
except UnicodeEncodeError:
raise self.severe(
'Problems with "%s" directive path:\n'
'Cannot encode input file path "%s" '
"(wrong locale?)." % (self.name, str(path))
)
except IOError as error:
raise self.severe(
'Problems with "%s" directive path:\n%s.'
% (self.name, io.error_string(error))
)
# read from the file
startline = self.options.get("start-line", None)
endline = self.options.get("end-line", None)
try:
if startline or (endline is not None):
lines = include_file.readlines()
rawtext = "".join(lines[startline:endline])
else:
rawtext = include_file.read()
except UnicodeError as error:
raise self.severe(
'Problem with "%s" directive:\n%s' % (self.name, io.error_string(error))
)
config = self.state.document.settings.env.config
converter = M2R(
no_underscore_emphasis=config.no_underscore_emphasis,
parse_relative_links=config.m2r_parse_relative_links,
anonymous_references=config.m2r_anonymous_references,
disable_inline_math=config.m2r_disable_inline_math,
use_mermaid=config.m2r_use_mermaid,
)
include_lines = statemachine.string2lines(
converter(rawtext), tab_width, convert_whitespace=True
)
self.state_machine.insert_input(include_lines, path)
return []
def setup(app):
"""When used for sphinx extension."""
global _is_sphinx
_is_sphinx = True
app.add_config_value("no_underscore_emphasis", False, "env")
app.add_config_value("m2r_parse_relative_links", False, "env")
app.add_config_value("m2r_anonymous_references", False, "env")
app.add_config_value("m2r_disable_inline_math", False, "env")
app.add_config_value(
"m2r_use_mermaid", "sphinxcontrib.mermaid" in app.config.extensions, "env"
)
try:
app.add_source_parser(".md", M2RParser) # for older sphinx versions
except (TypeError, AttributeError):
app.add_source_suffix(".md", "markdown")
app.add_source_parser(M2RParser)
app.add_directive("mdinclude", MdInclude)
metadata = dict(
version=__version__,
parallel_read_safe=True,
parallel_write_safe=True,
)
return metadata
def convert(text, **kwargs):
return M2R(**kwargs)(text)
def parse_from_file(file, encoding="utf-8", **kwargs):
if not os.path.exists(file):
raise OSError("No such file exists: {}".format(file))
with _open(file, encoding=encoding) as f:
src = f.read()
output = convert(src, **kwargs)
return output
def save_to_file(file, src, encoding="utf-8", **kwargs):
target = os.path.splitext(file)[0] + ".rst"
if not options.overwrite and os.path.exists(target):
confirm = input("{} already exists. overwrite it? [y/n]: ".format(target))
if confirm.upper() not in ("Y", "YES"):
print("skip {}".format(file))
return
with _open(target, "w", encoding=encoding) as f:
f.write(src)
def main():
parse_options() # parse cli options
if not options.input_file:
parser.print_help()
parser.exit(0)
for file in options.input_file:
output = parse_from_file(file)
if options.dry_run:
print(output)
else:
save_to_file(file, output)
if __name__ == "__main__":
main()