-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2rm.py
executable file
·166 lines (139 loc) · 4.41 KB
/
md2rm.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
#!/usr/bin/env python3
import marko
import argparse
import sys
__author__ = "HexPandaa"
__version__ = "1.0"
__repository__ = "https://github.com/HexPandaa/md2rm"
class RootMeRenderer(marko.renderer.Renderer):
def render_heading(self, element: marko.block.Heading) -> str:
"""
md: "# Level 1 heading\n"
rm: "{{{Level 1 heading}}}\n"
md: "## Level 2 heading\n"
rm: "{{## Level 2 heading}}\n"
"""
hashes = "#" * element.level + " " if element.level > 1 else ""
result = f"{{{{{ hashes }{ self.render_children(element) }}}}}"
if element.level == 1:
result = f"{{{result}}}" # Add the extra brackets
return result + "\n"
def render_raw_text(self, element: marko.inline.RawText) -> str:
"""
md: "simple string"
ex: "simple string"
"""
return element.children
def render_blank_line(self, element: marko.block.BlankLine) -> str:
"""
md: "\n"
rm: "\n"
"""
return "\n"
def render_code_span(self, element: marko.inline.CodeSpan) -> str:
"""
md: "`something`"
rm: "<code>something</code>"
"""
return f"<code>{element.children}</code>"
def render_fenced_code(self, element: marko.block.FencedCode) -> str:
"""
md:
"```lang
line1
line2
...
```"
rm:
"<cadre class="lang">
line1
line2
...
</cadre>\n"
"""
return f"<cadre class=\"{element.lang}\">\n{ self.render_children(element) }</cadre>\n"
def render_paragraph(self, element: marko.block.Paragraph) -> str:
"""
md: "simple string"
rm: "simple string"
"""
return self.render_children(element) + "\n"
def render_emphasis(self, element: marko.inline.Emphasis) -> str:
"""
md: "*simple string*"
rm: "{{{simple string}}}"
"""
return f"{{{ self.render_children(element) }}}"
def render_strong_emphasis(self, element: marko.inline.StrongEmphasis) -> str:
"""
md: "**simple string**"
rm: "{{simple string}}"
"""
return f"{{{{{ self.render_children(element) }}}}}"
def render_link(self, element: marko.inline.Link) -> str:
"""
md: "[title](link)"
rm: "[title->link]"
"""
return f"[{ self.render_children(element) }->{ element.dest }]"
def render_list(self, element: marko.block.List) -> str:
"""
md:
"- unordered
- list"
rm:
"-* unordered
-* list"
md:
"1. ordered
2. list"
rm:
"-# ordered
-# list"
note: currently does not properly support nested lists
"""
result = ""
bullet = "-#" if element.ordered else "-*"
list_item: marko.block.ListItem
for list_item in element.children:
result += f"{bullet} {self.render(list_item)}" # newline will be added by the inner paragraph
return result
def render_quote(self, element: marko.block.FencedCode) -> str:
"""
md:
"> multiline
> quote\n"
rm:
"<quote>
multine
quote
</quote>\n"
"""
return f"<quote>\n{ self.render_children(element) }</quote>\n"
def render_line_break(self, element: marko.inline.LineBreak) -> str:
"""
md: "\n
rm: "\n"
"""
return "\n"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="md2rm: A Markdown to Root-Me syntax converter.",
epilog=f"v{__version__}, {__repository__}")
parser.add_argument("input", type=argparse.FileType(mode="r"),
help="markdown file")
parser.add_argument("output", nargs="?", type=argparse.FileType(mode="w"), default=sys.stdout,
help="output file. default: stdout")
if len(sys.argv) < 2:
parser.print_help()
sys.exit(0)
args = parser.parse_args()
return args
def main(args: argparse.Namespace) -> None:
parser = marko.Markdown(renderer=RootMeRenderer)
# markdown = parser.parse(args.input.read())
# result = parser.render(markdown)
result = parser.convert(args.input.read())
args.output.write(result)
if __name__ == "__main__":
main(parse_args())