forked from esphome/esphome-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.py
194 lines (146 loc) · 4.8 KB
/
markdown.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
#!/usr/bin/env python
# reStructuredText (RST) to GitHub-flavored Markdown converter
import re
import sys
from docutils import core, nodes, writers
from urllib import parse
class Translator(nodes.NodeVisitor):
def __init__(self, base_url, document):
nodes.NodeVisitor.__init__(self, document)
self.output = ""
self.indent = 0
self.preserve_newlines = False
self.base_url = base_url
def write(self, text):
self.output += text.replace("\n", "\n" + " " * self.indent)
def visit_document(self, node):
pass
def depart_document(self, node):
pass
def visit_section(self, node):
pass
def depart_section(self, node):
# Skip all sections except the first one.
raise nodes.StopTraversal
def visit_title(self, node):
self.version = re.match(r"(\d+\.\d+\.\d+).*", node.children[0]).group(1)
raise nodes.SkipChildren
def visit_title_reference(self, node):
raise Exception(
f'Markdown conversion: Improper format or not supported feature in "{node.parent.rawsource}". Check "`"'
)
def depart_title(self, node):
pass
def visit_Text(self, node):
if not self.preserve_newlines:
node = node.replace("\n", " ")
self.write(node)
def depart_Text(self, node):
pass
def visit_bullet_list(self, node):
self.write("\n")
pass
def depart_bullet_list(self, node):
pass
def visit_list_item(self, node):
self.write("* ")
self.indent += 2
def depart_list_item(self, node):
self.indent -= 2
self.write("\n\n")
def visit_paragraph(self, node):
pass
def depart_paragraph(self, node):
pass
def visit_reference(self, node):
# if not is_github_ref(node):
self.write("[")
def depart_reference(self, node):
# if not is_github_ref(node):
# self.write('](' + node['refuri'] + ')')
refid = node.get("refid")
if refid:
self.write("](" + parse.urljoin(self.base_url, "#" + refid) + ")")
else:
refuri = node.get("refuri")
if refuri:
# if refuri.startswith('../'):
# refuri = parse.urljoin(self.base_url, refuri)
# self.write('](' + refuri + ')')
self.write("](" + parse.urljoin(self.base_url, refuri) + ")")
def visit_emphasis(self, node):
self.write("*")
pass
def depart_emphasis(self, node):
self.write("*")
pass
def visit_target(self, node):
pass
def depart_target(self, node):
pass
def visit_literal(self, node):
self.write("`")
def depart_literal(self, node):
self.write("`")
def visit_literal_block(self, node):
self.write("\n\n```")
if "c++" in node["classes"]:
self.write("c++")
self.write("\n")
self.preserve_newlines = True
def depart_literal_block(self, node):
self.write("\n```\n")
self.preserve_newlines = False
def visit_inline(self, node):
pass
def depart_inline(self, node):
pass
def visit_image(self, node):
self.write("![](" + node["uri"] + ")")
def depart_image(self, node):
pass
def write_row(self, row, widths):
for i, entry in enumerate(row):
text = entry[0][0] if len(entry) > 0 else ""
if i != 0:
self.write("|")
self.write("{:{}}".format(text, widths[i]))
self.write("\n")
def visit_table(self, node):
table = node.children[0]
colspecs = table[:-2]
thead = table[-2]
tbody = table[-1]
widths = [int(cs["colwidth"]) for cs in colspecs]
sep = "|".join(["-" * w for w in widths]) + "\n"
self.write("\n\n")
self.write_row(thead[0], widths)
self.write(sep)
for row in tbody:
self.write_row(row, widths)
raise nodes.SkipChildren
def depart_table(self, node):
pass
def visit_strong(self, node):
self.write("**")
pass
def depart_strong(self, node):
self.write("**")
pass
def visit_block_quote(self, node):
pass
def depart_block_quote(self, node):
pass
class MDWriter(writers.Writer):
"""GitHub-flavored markdown writer"""
supported = ("md",)
"""Formats this writer supports."""
def translate(self):
translator = Translator(self.document)
self.document.walkabout(translator)
self.output = (translator.output, translator.version)
def convert(rst_path):
"""Converts RST file to Markdown."""
return core.publish_file(source_path=rst_path, writer=MDWriter())
if __name__ == "__main__":
convert(sys.argv[1])