-
Notifications
You must be signed in to change notification settings - Fork 11
/
readme.py
281 lines (229 loc) · 6.99 KB
/
readme.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
from __future__ import annotations
import inspect
import logging
from typing import Callable
from snakemd import Block, Code, Document, Inline, MDList, Paragraph, Table
def _introduction(doc: Document):
doc.add_paragraph(
"""
SnakeMD is your ticket to generating Markdown in Python.
To prove it to you, we've generated this entire README using SnakeMD.
See readme.py for how it was done. To get started, download and install SnakeMD:
"""
)
doc.add_code("pip install snakemd", lang="shell")
p = doc.add_paragraph(
"""
In the remainder of this document, we'll show you all of
the things this library can do. For more information, check
out the official documentation here.
"""
)
p.insert_link("here", "https://snakemd.io")
def _table_of_contents(doc: Document):
doc.add_table_of_contents(range(2, 4))
def _ordered_list(doc: Document):
doc.add_ordered_list(["Deku", "Bakugo", "Uraraka", "Tsuyu"])
def _unordered_list(doc: Document):
doc.add_unordered_list(["Crosby", "Malkin", "Lemieux"])
def _checklist(doc: Document):
doc.add_checklist(["Pass the puck", "Shoot the puck", "Score a goal"])
def _nested_list(doc: Document):
doc.add_block(
MDList(
[
"Apples",
Inline("Onions", bold=True),
MDList(["Sweet", "Red"]),
Paragraph(["This is the end of the list!"]),
]
)
)
def _table(doc: Document):
doc.add_table(
["Height (cm)", "Weight (kg)", "Age (y)"],
[["150", "70", "21"], ["164", "75", "19"], ["181", "87", "40"]],
[Table.Align.LEFT, Table.Align.CENTER, Table.Align.RIGHT],
0,
)
def _insert_link(doc: Document):
doc.add_paragraph(
"Learn to program with The Renegade Coder (@RenegadeCoder94)."
).insert_link("The Renegade Coder", "https://therenegadecoder.com").insert_link(
"@RenegadeCoder94", "https://twitter.com/RenegadeCoder94"
)
def _image(doc: Document):
logo = "https://therenegadecoder.com/wp-content/uploads/2020/05/header-logo-without-tag-300x75.png"
doc.add_block(Paragraph([Inline("Logo", image=logo)]))
def _code(doc: Document):
doc.add_code("x = 5", lang="py")
def _paragraph(doc: Document):
doc.add_paragraph("I think. Therefore, I am.")
def _quote(doc: Document):
doc.add_quote("How Now Brown Cow")
def _horizontal_rule(doc: Document):
doc.add_horizontal_rule()
def _raw(doc: Document):
doc.add_raw("4<sup>2</sup> = 16<br />How cool is that?")
def _section(doc: Document, title: str, desc: str, func: Callable, level: int = 2):
doc.add_heading(title, level=level)
doc.add_paragraph(desc)
doc.add_block(Paragraph([Inline("SnakeMD Source", italics=True)]))
doc.add_code(inspect.getsource(func).strip(), lang="py")
doc.add_block(Paragraph([Inline("Rendered Result", italics=True)]))
func(doc)
doc.add_block(Paragraph([Inline("Markdown Source", italics=True)]))
elements = doc.get_elements()
block: Block = elements[-2]
doc.add_block(
Code(block if isinstance(block, Code) else str(block), lang="markdown")
)
elements.insert(-3, elements.pop())
elements.insert(-3, elements.pop())
def main() -> None:
"""Generates the repo README."""
doc = Document()
# Introduction
doc.add_heading("Welcome to SnakeMD")
_introduction(doc)
# Table of Contents
doc.add_heading("Table of Contents", level=2)
doc.add_paragraph(
"""
Below you'll find the table of contents, but
these can also be generated programatically for every Markdown
document as follows:
"""
)
doc.add_code(inspect.getsource(_table_of_contents).strip(), lang="py")
_table_of_contents(doc)
# Paragraphs
_section(
doc,
"Paragraphs",
"""
Paragraphs are the most basic feature of any Markdown file.
As a result, they are very easy to create using SnakeMD.
""",
_paragraph,
)
# Insert Links
_section(
doc,
"Links",
"""
Links are targets to files or web pages and can be embedded
in paragraphs in a variety of ways, such as with the insert_link()
method.
""",
_insert_link,
)
# Images
_section(
doc,
"Images",
"Images can be added by embedding Inline elements in a paragraph.",
_image,
)
# Lists
doc.add_heading("Lists", level=2)
doc.add_paragraph(
"""
SnakeMD can make a variety of Markdown lists. The three main types
of lists are ordered, unordered, and checked.
"""
)
# Ordered lists
_section(
doc,
"Ordered List",
"""
Ordered lists are lists in which the order of the items
matters. As a result, we number them.
""",
_ordered_list,
level=3,
)
# Unordered lists
_section(
doc,
"Unordered List",
"""
Unordered lists are lists in which the order of the items
does not matter. As a result, we bullet them.
""",
_unordered_list,
level=3,
)
# Checklist
_section(
doc,
"Checklist",
"""
Checklists are lists in which the items themselves can be
checked on and off. This feature is new as of v0.10.0.
""",
_checklist,
level=3,
)
# Nested lists
_section(
doc,
"Nested Lists",
"""
Nested lists are complex lists that contain lists. Currently,
SnakeMD does not support any convenience methods to generate nested
lists, but they can be created manually using the MDList object.
""",
_nested_list,
level=3,
)
# Tables
_section(
doc,
"Tables",
"""
Tables are sets of rows and columns which can display text in a
grid. To style any of the contents of a table, consider using
Paragraph or Inline.
""",
_table,
)
# Code
_section(
doc,
"Code Blocks",
"""
Code blocks are a form of structured text for sharing code
snippets with syntax highlighting.
""",
_code,
)
# Quote
_section(
doc,
"Quotes",
"Quotes are blocks of text that represent quotes from people.",
_quote,
)
_section(
doc,
"Horizontal Rule",
"Horizontal Rules are visible dividers in a document.",
_horizontal_rule,
)
_section(
doc,
"Raw",
"""
If at any time SnakeMD doesn't meet your needs, you can
always add your own text using a raw block. These can
be used to insert any preformatted text you like,
such as HTML tags, Jekyll frontmatter, and more.
""",
_raw,
)
doc.dump("README")
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()