Skip to content

Commit b1dd9bc

Browse files
Docs: Add toctree shortcode docs and render option in the Python script (#406)
* Add: (render_shortcode_docs.py) Options processing This allows each shortcode file to pass a dict of options to this Python program. Currently the only one supported is "render", which may be set to False to disable rendering of the shortcode into the documentation. This is useful when a shortcode will not render meaningfully into the documentation file (e.g. the "toctree" shortcode only works when in an appropriate file hierarchy). * Docs: Add toctree shortcode documentation * '[pre-commit.ci 🤖] Apply code format tools to PR' --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ee505b1 commit b1dd9bc

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

layouts/shortcodes/toctree.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
{{/*
2+
3+
options: {"render": False}
4+
5+
doc: Shows a table-of-contents tree for the Hugo [`Sections`](https://gohugo.io/methods/page/sections/) in the current hierarchy. In this documentaion, an example of the `toctree` is seen on the [Examples]({{% relref "examples" %}}) page.
6+
7+
{{< toctree >}}
8+
9+
*/}}
10+
111
<div class="toctree-wrapper">
212
{{- range $section := .Page.Sections }}
313
<ul>

tools/render_shortcode_docs.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
#!/usr/bin/env python3
2+
3+
# NOTE: In each shortcode's HTML file, options may be provided in the
4+
# form of a Python dict after "options: " at the beginning of a line
5+
# inside the specially commented preface. For example:
6+
7+
# {{/*
8+
#
9+
# options: {"render": False}
10+
#
11+
# doc: Foo bar.
12+
#
13+
# {{< foo bar >}}
14+
#
15+
# */}}
16+
17+
# That option (currently the only one) disables rendering of the
18+
# shortcode in the documentation.
19+
120
import os
221
import re
322

@@ -32,6 +51,22 @@ def shortcode_doc(fn):
3251
.replace(" %}}", " */%}}")
3352
)
3453

54+
# Process rendering options.
55+
options_match = re.match(
56+
"^{{/\\*.*^options: +({[^\n]+}) *$.*\\*/}}$", data, re.MULTILINE | re.DOTALL
57+
)
58+
if options_match:
59+
# Read Python dict of options.
60+
from ast import literal_eval
61+
62+
options = literal_eval(options_match.group(1)) # Safely read expression.
63+
assert isinstance(options, dict)
64+
65+
if "render" in options:
66+
if not options["render"]:
67+
# Disable rendering of the example.
68+
code = None
69+
3570
return description, example, code
3671

3772

@@ -68,7 +103,9 @@ def shortcode_doc(fn):
68103
# We use an extra backtick here so code blocks embedded in the
69104
# examples work correctly.
70105
print(f"````\n{example}\n````")
71-
print("This example renders as:")
72-
print("___")
73-
print(code)
74-
print("___")
106+
107+
if code:
108+
print("This example renders as:")
109+
print("___")
110+
print(code)
111+
print("___")

0 commit comments

Comments
 (0)