-
Notifications
You must be signed in to change notification settings - Fork 115
/
generate_showcase_pages.py
57 lines (44 loc) · 2.09 KB
/
generate_showcase_pages.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
import os
import yaml
from jinja2 import Environment, FileSystemLoader
# Get script's directory
script_dir = os.path.dirname(os.path.abspath(__file__))
# Define the absolute path to the templates directory
templates_dir = os.path.join(script_dir, '../docs/showcase')
templates_dir = os.path.abspath(templates_dir)
print(f"Templates directory: {templates_dir}")
# Use templates_dir as the base for your FileSystemLoader
env = Environment(loader=FileSystemLoader(templates_dir))
# Then you can access your templates like so
project_template = env.get_template('_project_template.md')
index_template = env.get_template('_showcase_page_template.md')
# Loop over all .md files in /docs/showcase directory and delete them unless they start with "_"
for filename in os.listdir(templates_dir):
if filename.endswith(".md") and not filename.startswith("_"):
os.remove(os.path.join(templates_dir, filename))
# Define directories for .yaml files and output
root_dir = os.path.join(script_dir, '../showcase')
output_dir = templates_dir
index_links = []
for filename in os.listdir(root_dir):
if filename.startswith("_") or not filename.endswith(".yaml"):
continue
with open(os.path.join(root_dir, filename), 'r') as stream:
try:
# Load yaml file
print(f"Processing {filename}")
data = yaml.safe_load(stream)
# Generate markdown content using the template
md_content = project_template.render(data)
# Write markdown content to file
output_filename = filename.replace(".yaml", ".md")
with open(os.path.join(output_dir, output_filename), 'w') as md_file:
md_file.write(md_content)
# Store link to this project for the index file
index_links.append({'name': data.get("name", ""), 'filename': output_filename})
except yaml.YAMLError as exc:
print(exc)
# Create index.md
with open(os.path.join(output_dir, 'index.md'), 'w') as index_file:
index_file.write(index_template.render(projects=index_links))
print(f"Generated {len(index_links)} project pages.")