Skip to content

Commit bc09538

Browse files
committed
feature: enable static site generation for GitHub Pages
1 parent 92147b6 commit bc09538

8 files changed

Lines changed: 102 additions & 13 deletions

File tree

.github/workflows/deploy_pages.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ jobs:
1616
- name: Checkout Code
1717
uses: actions/checkout@v4
1818

19-
# Step 2: Deploy ONLY the 'static' folder
19+
# Step 2: Set up Python
20+
- name: Set up Python
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.11'
24+
25+
# Step 3: Install Dependencies (Jinja2)
26+
- name: Install Dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install jinja2
30+
31+
# Step 4: Build Static Site
32+
- name: Build Static Site
33+
run: python scripts/build_static.py
34+
35+
# Step 5: Deploy the '_site' folder
2036
- name: Deploy to GitHub Pages
2137
uses: peaceiris/actions-gh-pages@v3
2238
with:
2339
github_token: ${{ secrets.GITHUB_TOKEN }}
24-
publish_dir: ./static
40+
publish_dir: ./_site
2541
force_orphan: true # Keeps the deployment branch clean and history-free
2642
user_name: 'github-actions[bot]'
2743
user_email: 'github-actions[bot]@users.noreply.github.com'

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,7 @@ cython_debug/
184184
/prompt
185185

186186
# Temp files
187-
/data/temp
187+
/data/temp
188+
189+
# Static Site Generation
190+
_site/

scripts/build_static.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import shutil
3+
from jinja2 import Environment, FileSystemLoader
4+
5+
# Configuration
6+
TEMPLATE_DIR = "templates"
7+
STATIC_DIR = "static"
8+
OUTPUT_DIR = "_site"
9+
10+
11+
def build_static_site():
12+
"""Builds the static site from Jinja2 templates."""
13+
print("Starting Static Site Build...")
14+
15+
# 1. Prepare Output Directory
16+
if os.path.exists(OUTPUT_DIR):
17+
shutil.rmtree(OUTPUT_DIR)
18+
os.makedirs(OUTPUT_DIR)
19+
print(f"Created output directory: {OUTPUT_DIR}")
20+
21+
# 2. Copy Static Assets
22+
# Copy contents of 'static' folder to '_site' root (so css/style.css works as expected)
23+
# The template expects 'css/' and 'js/' to be at the root relative to the HTML file
24+
25+
# Copy individual subdirectories to maintain structure
26+
if os.path.exists(STATIC_DIR):
27+
for item in os.listdir(STATIC_DIR):
28+
s = os.path.join(STATIC_DIR, item)
29+
d = os.path.join(OUTPUT_DIR, item)
30+
if os.path.isdir(s):
31+
shutil.copytree(s, d)
32+
else:
33+
shutil.copy2(s, d)
34+
print(f"Copied static assets from {STATIC_DIR} to {OUTPUT_DIR}")
35+
else:
36+
print(f"Warning: Static directory '{STATIC_DIR}' not found.")
37+
38+
# 3. Render Templates
39+
env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))
40+
41+
# List of pages to render
42+
pages = [
43+
{"template": "index.html", "output": "index.html"},
44+
{"template": "history.html", "output": "history.html"},
45+
{"template": "help.html", "output": "help.html"},
46+
]
47+
48+
for page in pages:
49+
try:
50+
template = env.get_template(page["template"])
51+
output_content = template.render(request=None, is_static=True)
52+
53+
output_path = os.path.join(OUTPUT_DIR, page["output"])
54+
with open(output_path, "w", encoding="utf-8") as f:
55+
f.write(output_content)
56+
57+
print(f"Rendered: {page['template']} -> {page['output']}")
58+
except Exception as e:
59+
print(f"Error rendering {page['template']}: {e}")
60+
61+
print("Build Complete!")
62+
63+
64+
if __name__ == "__main__":
65+
build_static_site()

static/js/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tailwind.config = {
3939
*/
4040
const CONFIG = Object.freeze({
4141
env: {
42-
isGithubPages: window.location.hostname.includes('github.io')
42+
isGithubPages: window.IS_STATIC || window.location.hostname.includes('github.io') || window.location.protocol === 'file:'
4343
},
4444
endpoints: {
4545
preview: '/api/preview',

templates/base.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
2323

2424
{# Application CSS #}
25-
<link rel="stylesheet" href="/css/style.css">
25+
<link rel="stylesheet" href="css/style.css">
2626
{% block extra_css %}{% endblock %}
2727

2828
{# Core JavaScript Dependencies #}
29-
<script src="/js/locales.js"></script>
30-
<script src="/js/i18n.js"></script>
31-
<script src="/js/config.js"></script>
32-
<script src="/js/utils.js"></script>
29+
<script src="js/locales.js"></script>
30+
<script src="js/i18n.js"></script>
31+
<script src="js/config.js"></script>
32+
<script src="js/utils.js"></script>
33+
34+
<script>
35+
// Injected by build_static.py or defaults to false
36+
window.IS_STATIC = {{ is_static | default('false') | lower }};
37+
</script>
3338

3439
{% block extra_head %}{% endblock %}
3540
</head>

templates/components/navbar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<div id="navbar-mount" class="w-full flex justify-center"></div>
2-
<script src="/js/navbar.js"></script>
2+
<script src="js/navbar.js"></script>

templates/history.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ <h2 class="text-xl font-bold text-gray-100 flex items-center gap-3">
4646
{% endblock %}
4747

4848
{% block extra_scripts %}
49-
<script src="/js/history.js"></script>
49+
<script src="js/history.js"></script>
5050
{% endblock %}

templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ <h2 class="text-sm font-bold text-blue-400 uppercase tracking-wider mb-4" data-i
124124
</div>
125125
</div>
126126
<div class="lg:col-span-7 flex flex-col gap-6">
127-
<div class="glass-panel rounded-2xl p-1 flex-1 min-h-[200px] flex flex-col">
127+
<div class="glass-panel rounded-2xl p-1 h-[400px] flex flex-col relative overflow-hidden shadow-2xl">
128128
<div class="bg-black/40 px-4 py-2 rounded-t-xl border-b border-white/5 flex justify-between items-center">
129129
<span class="text-xs font-mono text-gray-400" data-i18n="dashboard.preview.title">DATA_SOURCE_PREVIEW.JSON</span>
130130
<span class="text-[10px] bg-gray-800 px-2 py-0.5 rounded text-gray-300" data-i18n="dashboard.preview.badge">READ-ONLY</span>
@@ -154,5 +154,5 @@ <h2 class="text-sm font-bold text-blue-400 uppercase tracking-wider mb-4" data-i
154154
{% endblock %}
155155

156156
{% block extra_scripts %}
157-
<script src="/js/app.js"></script>
157+
<script src="js/app.js"></script>
158158
{% endblock %}

0 commit comments

Comments
 (0)