-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
242 lines (207 loc) Β· 7.43 KB
/
app.py
File metadata and controls
242 lines (207 loc) Β· 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python3
"""
Flask App for Community Pages
This Flask app serves HTML pages dynamically from JSON data and templates
for live preview. Static HTML generation is handled by GitHub Actions.
"""
import json
import click
from pathlib import Path
from flask import Flask, render_template, abort, redirect, url_for
from jinja2 import TemplateNotFound
# Import helper functions for live preview
from generate import (
load_data, load_css_content, load_js_content,
find_page_folders
)
app = Flask(__name__)
@app.route('/')
def index():
"""Redirect to first available page."""
page_folders = find_page_folders()
if page_folders:
return redirect(url_for('serve_page', page_name=page_folders[0]))
return "No pages found. Please create a page folder with content.json", 404
@app.route('/<page_name>')
def serve_page(page_name):
"""Serve a page dynamically from its folder contents."""
page_folder = Path(page_name)
# Check if page folder exists
if not page_folder.exists():
abort(404)
# Check if content.json exists
if not (page_folder / "content.json").exists():
abort(404)
try:
# Load page data and assets
data = load_data(page_folder)
css_content = load_css_content(page_folder)
js_content = load_js_content(page_folder)
# Prepare template data
template_data = {
**data,
'page_name': page_name,
'css_content': css_content,
'js_content': js_content
}
return render_template(f'{page_name}/index.html', **template_data)
except FileNotFoundError as e:
return f"Missing file: {e}", 404
except TemplateNotFound as e:
return f"Missing template: {e}", 404
except json.JSONDecodeError as e:
return f"Invalid JSON in content.json: {e}", 400
except Exception as e:
return f"Error loading page: {e}", 500
@app.route('/pages')
def list_pages():
"""List all available pages."""
page_folders = find_page_folders()
if not page_folders:
return "No pages found", 404
page_links = []
for page_name in page_folders:
page_folder = Path(page_name)
try:
data = load_data(page_folder)
title = data.get('site', {}).get('title', page_name)
page_links.append({
'name': page_name,
'title': title,
'url': url_for('serve_page', page_name=page_name)
})
except:
page_links.append({
'name': page_name,
'title': page_name,
'url': url_for('serve_page', page_name=page_name)
})
# Simple HTML page listing
html = '''
<!DOCTYPE html>
<html>
<head>
<title>Available Pages</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
h1 { color: #333; }
.page-list { list-style: none; padding: 0; }
.page-item { margin: 15px 0; padding: 15px; border: 1px solid #ddd; border-radius: 6px; }
.page-item a { text-decoration: none; color: #0969da; font-weight: 600; }
.page-item a:hover { text-decoration: underline; }
.page-name { font-size: 12px; color: #666; margin-top: 5px; }
</style>
</head>
<body>
<h1>π Available Pages</h1>
<ul class="page-list">
'''
for page in page_links:
html += f'''
<li class="page-item">
<a href="{page['url']}">{page['title']}</a>
<div class="page-name">/{page['name']}</div>
</li>
'''
html += '''
</ul>
<p><small>π‘ Add new pages by creating folders with content.json, style.css, and script.js files.</small></p>
</body>
</html>
'''
return html
# CLI Command for creating new pages
@app.cli.command()
@click.argument('page_name')
def new_page(page_name):
"""Create a new page folder with template files.
Usage:
flask new-page blog
"""
page_folder = Path(page_name)
if page_folder.exists():
click.echo(f"β Page folder '{page_name}' already exists")
return
# Create page folder
page_folder.mkdir()
# Create basic content.json
content = {
"site": {
"title": page_name.title(),
"logo_url": "https://example.com/logo.png",
"home_url": "https://example.com",
"contact_email": "contact@example.com"
},
"readme": {
"en": {
"title": page_name.title(),
"sections": [
{
"type": "highlight_box",
"content": f"Welcome to <strong>{page_name.title()}</strong> page."
}
]
}
},
"ui_text": {
"en": {
"theme_toggle": "π",
"language_switch": "δΈζ",
"readme_tab": "README",
"conduct_tab": "Code of Conduct"
}
}
}
with open(page_folder / "content.json", 'w', encoding='utf-8') as f:
json.dump(content, f, indent=2, ensure_ascii=False)
# Create basic CSS
css_content = """/* Page-specific styles */
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
margin: 0;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
}
"""
with open(page_folder / "style.css", 'w', encoding='utf-8') as f:
f.write(css_content)
# Create basic JavaScript
js_content = """// Page-specific JavaScript
console.log('Page loaded successfully');
"""
with open(page_folder / "script.js", 'w', encoding='utf-8') as f:
f.write(js_content)
# Create templates folder if it doesn't exist
templates_folder = Path("templates") / page_name
templates_folder.mkdir(parents=True, exist_ok=True)
# Create basic template
template_content = """<h1>{{ page_name.title() }}</h1>
<p>This is a basic template for the {{ page_name }} page.</p>
<p>Edit this template in templates/{{ page_name }}/content.html</p>
"""
with open(templates_folder / "content.html", 'w', encoding='utf-8') as f:
f.write(template_content)
click.echo(f"β
Created new page: {page_name}")
click.echo(f"π Files created:")
click.echo(f" β’ {page_name}/content.json")
click.echo(f" β’ {page_name}/style.css")
click.echo(f" β’ {page_name}/script.js")
click.echo(f" β’ templates/{page_name}/content.html")
click.echo(f"π Preview at: http://localhost:8000/{page_name}")
if __name__ == '__main__':
# Run the Flask dev server for live preview
click.echo("π Starting Flask development server for live preview...")
click.echo("π Available endpoints:")
click.echo(" β’ http://localhost:8000/ - Redirect to first page")
click.echo(" β’ http://localhost:8000/pages - List all pages")
click.echo(" β’ http://localhost:8000/<page_name> - View specific page")
click.echo("")
click.echo("π§ Available command:")
click.echo(" β’ flask new-page <name> - Create new page")
click.echo("")
click.echo("π‘ Static HTML generation is handled by GitHub Actions")
click.echo("")
app.run(debug=True, host='127.0.0.1', port=8000)