|
| 1 | +""" |
| 2 | +Render HTML resume templates and copy static files to output directory. |
| 3 | +""" |
| 4 | + |
| 5 | +import jinja2 |
| 6 | +import tomllib |
| 7 | +import shutil |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def mock_url_for(endpoint, **kwargs): |
| 12 | + """ |
| 13 | + Mock url_for function for Jinja2 templates. |
| 14 | + Returns static file paths or endpoint routes. |
| 15 | + """ |
| 16 | + if endpoint == "static": |
| 17 | + filename = kwargs.get("filename", "") |
| 18 | + return f"/resume/static/{filename}" |
| 19 | + return f"/{endpoint}" |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + # Set up output directory |
| 24 | + output_dir = Path("rendered_artifacts") |
| 25 | + output_dir.mkdir(exist_ok=True) |
| 26 | + |
| 27 | + # Set up static output directory |
| 28 | + static_output_dir = output_dir / "static" |
| 29 | + static_output_dir.mkdir(exist_ok=True) |
| 30 | + |
| 31 | + # Copy static files to output directory |
| 32 | + static_dir = Path("static") |
| 33 | + if static_dir.exists(): |
| 34 | + shutil.copytree(static_dir, static_output_dir, dirs_exist_ok=True) |
| 35 | + |
| 36 | + # Set up Jinja2 environment |
| 37 | + template_dir = Path("templates/home") |
| 38 | + env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir)) |
| 39 | + env.globals["url_for"] = mock_url_for # Add mock url_for to Jinja2 globals |
| 40 | + |
| 41 | + # Load the template |
| 42 | + template = env.get_template("index_template.html") |
| 43 | + |
| 44 | + # Load data from TOML file |
| 45 | + with open("resume_config.toml", "rb") as file: |
| 46 | + resume_data = tomllib.load(file) |
| 47 | + |
| 48 | + # Render the template with the data |
| 49 | + rendered_html = template.render(**resume_data) |
| 50 | + |
| 51 | + # Save the rendered HTML to a file in output directory |
| 52 | + with open(output_dir / "index.html", "w") as outfile: |
| 53 | + outfile.write(rendered_html) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments