import json
import logging
from pathlib import Path
def render_template(template_path, context):
with open(template_path, encoding="utf-8") as f:
content = f.read()
for key, value in context.items():
placeholder = "{{ " + key + " }}"
content = content.replace(placeholder, str(value) if value is not None else "")
return content
def render_gallery_images(images):
html = ""
for img in images:
tags = " ".join(img.get("tags", []))
tag_html = "".join(f'#{t}' for t in img.get("tags", []))
html += f"""
{tag_html}
"""
return html
def generate_gallery_json_from_images(images, output_path):
try:
img_list = [img["src"] for img in images]
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(img_list, f, indent=2)
logging.info(f"[✓] Generated hero gallery JSON: {output_path}")
except Exception as e:
logging.error(f"[✗] Error generating gallery JSON: {e}")
def generate_robots_txt(canonical_url, allowed_paths):
robots_lines = ["User-agent: *"]
for path in allowed_paths:
robots_lines.append(f"Allow: {path}")
robots_lines.append("Disallow: /")
robots_lines.append("")
robots_lines.append(f"Sitemap: {canonical_url}/sitemap.xml")
content = "\n".join(robots_lines)
output_path = Path(".output/robots.txt")
with open(output_path, "w", encoding="utf-8") as f:
f.write(content)
logging.info(f"[✓] robots.txt generated at {output_path}")
def generate_sitemap_xml(canonical_url, allowed_paths):
urlset_start = '\n\n'
urlset_end = '\n'
urls = ""
for path in allowed_paths:
loc = canonical_url.rstrip("/") + path
urls += f" \n {loc}\n \n"
sitemap_content = urlset_start + urls + urlset_end
output_path = Path(".output/sitemap.xml")
with open(output_path, "w", encoding="utf-8") as f:
f.write(sitemap_content)
logging.info(f"[✓] sitemap.xml generated at {output_path}")