|
1 | 1 | __author__ = "akeshavan" |
2 | | -from jinja2 import Environment, FileSystemLoader |
| 2 | +import glob |
3 | 3 | import json |
4 | 4 | import os |
5 | 5 |
|
| 6 | +from jinja2 import Environment, FileSystemLoader |
6 | 7 |
|
7 | | -def load_json(filename): |
8 | | - """Load data from a json file |
9 | | - Parameters |
10 | | - ---------- |
11 | | - filename : str |
12 | | - Filename to load data from. |
13 | | - Returns |
14 | | - ------- |
15 | | - data : dict |
16 | | - """ |
17 | 8 |
|
| 9 | +def load_json(filename): |
| 10 | + """Load data from a json file""" |
18 | 11 | with open(filename, "r") as fp: |
19 | 12 | data = json.load(fp) |
20 | 13 | return data |
21 | 14 |
|
22 | 15 |
|
| 16 | +def load_projects(directory): |
| 17 | + """ |
| 18 | + Scans the 'data/projects' directory for JSON files, |
| 19 | + loads them, and adds a link to the original GitHub issue. |
| 20 | + """ |
| 21 | + projects = [] |
| 22 | + # Check if directory exists to avoid errors on fresh clones |
| 23 | + if not os.path.exists(directory): |
| 24 | + print(f"Warning: Directory {directory} not found. No projects loaded.") |
| 25 | + return projects |
| 26 | + |
| 27 | + # Glob all json files |
| 28 | + for filename in glob.glob(os.path.join(directory, "*.json")): |
| 29 | + try: |
| 30 | + data = load_json(filename) |
| 31 | + |
| 32 | + # Construct the issue URL based on the repo name |
| 33 | + # You can also customize this if your repo changes |
| 34 | + if "issue_number" in data: |
| 35 | + data["issue_url"] = ( |
| 36 | + f"https://github.com/BrainhackMTL/winter2026/issues/{data['issue_number']}" |
| 37 | + ) |
| 38 | + |
| 39 | + projects.append(data) |
| 40 | + except Exception as e: |
| 41 | + print(f"Skipping {filename}: {e}") |
| 42 | + |
| 43 | + # Optional: Sort projects by issue number (earliest first) |
| 44 | + projects.sort(key=lambda x: int(x.get("issue_number", 0))) |
| 45 | + return projects |
| 46 | + |
| 47 | + |
23 | 48 | files_to_generate = [ |
24 | 49 | {"filename": "index.html.j2", "location": "./_site"}, |
| 50 | + {"filename": "projects.html.j2", "location": "./_site"}, # New projects page |
25 | 51 | {"filename": "css/stylish-portfolio.css.j2", "location": "./_site"}, |
26 | 52 | ] |
27 | 53 |
|
28 | 54 | env = Environment(loader=FileSystemLoader("./_site")) |
29 | 55 | info = load_json("data.json") |
30 | 56 |
|
| 57 | +# Load the project data and add it to the 'info' dictionary |
| 58 | +info["projects"] = load_projects("data/projects") |
| 59 | + |
31 | 60 | for f in files_to_generate: |
32 | | - template = env.get_template(f["filename"]) |
33 | | - outfile = os.path.join(f["location"], f["filename"].replace(".j2", "")) |
34 | | - print("writing", outfile) |
35 | | - with open(outfile, "w") as q: |
36 | | - q.write(template.render(**info)) |
| 61 | + try: |
| 62 | + template = env.get_template(f["filename"]) |
| 63 | + # Handle the output filename (remove .j2) |
| 64 | + outfile_name = f["filename"].replace(".j2", "") |
| 65 | + outfile = os.path.join(f["location"], outfile_name) |
| 66 | + |
| 67 | + print("writing", outfile) |
| 68 | + with open(outfile, "w", encoding="utf-8") as q: |
| 69 | + q.write(template.render(**info)) |
| 70 | + except Exception as e: |
| 71 | + print(f"Error generating {f['filename']}: {e}") |
0 commit comments