-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrender_shortcode_docs.py
More file actions
113 lines (91 loc) · 2.8 KB
/
Copy pathrender_shortcode_docs.py
File metadata and controls
113 lines (91 loc) · 2.8 KB
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
#!/usr/bin/env python3
# NOTE: In each shortcode's HTML file, options may be provided in the
# form of a Python dict after "options: " at the beginning of a line
# inside the specially commented preface. For example:
# {{/*
#
# options: {"render": False}
#
# doc: Foo bar.
#
# {{< foo bar >}}
#
# */}}
# That option (currently the only one) disables rendering of the
# shortcode in the documentation.
import os
import re
shortcodes = [
os.path.join(dirpath, filename)
for (dirpath, dirs, files) in os.walk(".")
for filename in sorted(files)
if (dirpath.endswith("/shortcodes") and filename.endswith(".html"))
]
def shortcode_doc(fn):
"""
Return description, example, code
"""
with open(fn) as f:
data = f.read()
match = re.match(
"^{{/\\*.*doc: ([^\n]*)(.*?)^\\*/}}$", data, re.MULTILINE | re.DOTALL
)
if not match:
return None, None, None
description, code = match.group(1), match.group(2).strip()
example = (
code.replace("{{< ", "{{</* ")
.replace(" >}}", " */>}}")
.replace("\n>}}", "\n*/>}}")
.replace("{{% ", "{{%/* ")
.replace(" %}}", " */%}}")
.replace("\n%}}", "\n*/%}}")
)
# Process rendering options.
options_match = re.match(
"^{{/\\*.*^options: +({[^\n]+}) *$.*\\*/}}$", data, re.MULTILINE | re.DOTALL
)
if options_match:
# Read Python dict of options.
from ast import literal_eval
options = literal_eval(options_match.group(1)) # Safely read expression.
assert isinstance(options, dict)
if "render" in options:
if not options["render"]:
# Disable rendering of the example.
code = None
return description, example, code
print(
"""\
---
title: Shortcodes
shortcutDepth: 1
---
<!-- DO NOT EDIT. This file is generated by `tools/render_shortcode_docs.py`. -->
Markdown is a convenient and simple format to write in.
However, it doesn't always do everything we want (or do it in a nice way).
Rather than adding raw HTML to our source files, Hugo allows us to use
_shortcodes_. Shortcodes are small snippets that look like this
```
{{</* _shortcodename parameters_ */>}}
```
that Hugo renders using a predefined template.
Here are some shortcodes used by this theme.
"""
)
for shortcode_fn in shortcodes:
title = os.path.basename(shortcode_fn).replace(".html", "")
description, example, code = shortcode_doc(shortcode_fn)
if description is None:
continue
print(f"## `{title}`")
print()
print(description)
# We use an extra backtick here so code blocks embedded in the
# examples work correctly.
print(f"````\n{example}\n````")
if code:
print("This example renders as:")
print("___")
print(code)
print("___")