aboutsummaryrefslogtreecommitdiffstats
path: root/docs/render_examples.py
blob: ad1566989e4a4e32120d0935de1d48cc22a2826a (plain)
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
#!/usr/bin/env python3

import os
import textwrap
from pathlib import Path

print("""
---
title: "Examples"
menu:
    addons:
        weight: 6
---

# Examples of Addons and Scripts

""")

base = os.path.dirname(os.path.realpath(__file__))
examples_path = os.path.join(base, '../src/examples/')
pathlist = Path(examples_path).glob('**/*.py')

examples = [os.path.relpath(str(p), examples_path) for p in sorted(pathlist)]
examples = [p for p in examples if not os.path.basename(p) == '__init__.py']
examples = [p for p in examples if not os.path.basename(p).startswith('test_')]

current_dir = None
current_level = 2
for ex in examples:
    if os.path.dirname(ex) != current_dir:
        current_dir = os.path.dirname(ex)
        sanitized = current_dir.replace('/', '').replace('.', '')
        print("  * [Examples: {}]({{{{< relref \"addons-examples#{}\">}}}})".format(current_dir, sanitized))

    sanitized = ex.replace('/', '').replace('.', '')
    print("    * [{}]({{{{< relref \"addons-examples#{}\">}}}})".format(ex, sanitized))

current_dir = None
current_level = 2
for ex in examples:
    if os.path.dirname(ex) != current_dir:
        current_dir = os.path.dirname(ex)
        print("#"*current_level, current_dir)

    print(textwrap.dedent("""
        {} {}
        {{{{< example src="{}" lang="py" >}}}}
    """.format("#"*(current_level+1), ex, "/examples/"+ex)))