162 lines
4.8 KiB
Python
162 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright 2026 Schwarz Digits Cloud GmbH & Co. KG
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""
|
|
Generate .mdx asset files for the STACKIT docs assetcontainer from stackit.docs.yaml metadata.
|
|
Each example becomes its own .mdx file with frameworkAsset frontmatter.
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
try:
|
|
import yaml
|
|
except ImportError:
|
|
print("ERROR: PyYAML not installed. Run: pip install pyyaml", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
GITHUB_REPO = "https://github.com/stackitcloud/professional-service/tree/main/examples"
|
|
|
|
ASSETCONTAINER_META = """# Professional Services Terraform Examples
|
|
customLabel: "PS Examples"
|
|
navigation:
|
|
hidden: true
|
|
renderAsLinkOnSlug: "cloud-framework/assets/professional-services"
|
|
renderAsDetailPageOnSlug: "cloud-framework/assets/professional-services"
|
|
"""
|
|
|
|
|
|
def slugify(name):
|
|
"""Convert a directory name to a valid mdx filename slug."""
|
|
slug = re.sub(r"[_\s]+", "-", name).lower()
|
|
return slug if slug.startswith("ps-") else f"ps-{slug}"
|
|
|
|
|
|
def find_example_dirs(examples_root):
|
|
entries = []
|
|
for entry in sorted(os.listdir(examples_root)):
|
|
full_path = os.path.join(examples_root, entry)
|
|
if os.path.isdir(full_path):
|
|
yaml_path = os.path.join(full_path, "stackit.docs.yaml")
|
|
if os.path.isfile(yaml_path):
|
|
entries.append((entry, yaml_path))
|
|
return entries
|
|
|
|
|
|
def load_yaml_file(filepath):
|
|
with open(filepath, "r") as f:
|
|
data = yaml.safe_load(f)
|
|
if not isinstance(data, dict):
|
|
print(
|
|
f"WARNING: {filepath} does not contain a YAML mapping, skipping.",
|
|
file=sys.stderr,
|
|
)
|
|
return None
|
|
return data
|
|
|
|
|
|
def generate_mdx(example_name, data, output_path):
|
|
"""Generate a single .mdx file with frameworkAsset frontmatter."""
|
|
headline = data.get("headline", example_name)
|
|
description = data.get("description", "")
|
|
tags = data.get("tags", [])
|
|
|
|
slug = slugify(example_name)
|
|
repo_link = f"{GITHUB_REPO}/{example_name}"
|
|
|
|
frontmatter = {
|
|
"title": headline,
|
|
"description": description,
|
|
"hideBreadcrumbs": True,
|
|
"sidebar": {"hidden": True},
|
|
"frameworkAsset": {
|
|
"owner": "Professional Services",
|
|
"managed": True,
|
|
"category": "guide",
|
|
"external": True,
|
|
"recommended": True,
|
|
"tags": tags,
|
|
},
|
|
}
|
|
|
|
content_lines = [
|
|
'import { Card } from "@astrojs/starlight/components";',
|
|
"",
|
|
f"# {headline}",
|
|
"",
|
|
f"{description}",
|
|
"",
|
|
f'<Card title="View Terraform Example" link="{repo_link}" icon="link">',
|
|
" Open the complete Terraform implementation on GitHub",
|
|
"</Card>",
|
|
"",
|
|
]
|
|
|
|
with open(output_path, "w") as f:
|
|
f.write("---\n")
|
|
yaml.dump(
|
|
frontmatter,
|
|
f,
|
|
default_flow_style=False,
|
|
sort_keys=False,
|
|
allow_unicode=True,
|
|
)
|
|
f.write("---\n\n")
|
|
f.write("\n".join(content_lines))
|
|
|
|
return slug
|
|
|
|
|
|
def main():
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
repo_root = os.path.dirname(script_dir)
|
|
examples_root = os.path.join(repo_root, "examples")
|
|
|
|
output_dir = os.environ.get("MDX_OUTPUT_DIR")
|
|
if not output_dir:
|
|
output_dir = os.path.join(repo_root, ".generated-mdx", "professional-services")
|
|
|
|
if not os.path.isdir(examples_root):
|
|
print(f"ERROR: Examples directory not found: {examples_root}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
meta_path = os.path.join(output_dir, "_meta.yml")
|
|
with open(meta_path, "w") as f:
|
|
f.write(ASSETCONTAINER_META)
|
|
|
|
examples = find_example_dirs(examples_root)
|
|
if not examples:
|
|
print("WARNING: No stackit.docs.yaml files found.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
count = 0
|
|
for example_name, yaml_path in examples:
|
|
data = load_yaml_file(yaml_path)
|
|
if data is None:
|
|
continue
|
|
slug = generate_mdx(
|
|
example_name, data, os.path.join(output_dir, f"{slugify(example_name)}.mdx")
|
|
)
|
|
print(f" generated: {slug}.mdx")
|
|
count += 1
|
|
|
|
print(f"Generated {count} .mdx files in {output_dir}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|