-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_pages.py
More file actions
156 lines (134 loc) · 5.24 KB
/
build_pages.py
File metadata and controls
156 lines (134 loc) · 5.24 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
import os
import re
from pathlib import Path
import markdown
# Define paths
en_dir = Path("en")
pt_dir = Path("pt")
site_en_dir = Path("_site/en")
site_pt_dir = Path("_site/pt")
# Create directories if they don't exist
site_en_dir.mkdir(parents=True, exist_ok=True)
site_pt_dir.mkdir(parents=True, exist_ok=True)
def extract_frontmatter(content):
"""Extract YAML frontmatter from markdown"""
match = re.match(r'^---\n(.*?)\n---\n(.*)', content, re.DOTALL)
if match:
frontmatter = match.group(1)
body = match.group(2)
return frontmatter, body
return None, content
def parse_frontmatter(frontmatter):
"""Parse YAML frontmatter into a dictionary"""
if not frontmatter:
return {}
data = {}
for line in frontmatter.split('\n'):
if ':' in line:
key, value = line.split(':', 1)
data[key.strip()] = value.strip().strip('\'"')
return data
def markdown_to_html(md_content):
"""Convert markdown to HTML"""
return markdown.markdown(md_content, extensions=['extra', 'codehilite'])
def create_html_page(title, content, lang='en'):
"""Create a complete HTML page"""
nav_links = {
'en': '''
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="/">PlayData</a>
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="/editor">Try it</a></li>
<li class="nav-item"><a class="nav-link" href="/en/getting-started">Getting started</a></li>
<li class="nav-item"><a class="nav-link" href="/en/resource-cards">Resources</a></li>
<li class="nav-item"><a class="nav-link" href="/en/publications">Publications</a></li>
<li class="nav-item"><a class="nav-link" href="/en/about">About</a></li>
</ul>
</nav>
''',
'pt': '''
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="/">PlayData</a>
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="/editor">Teste</a></li>
<li class="nav-item"><a class="nav-link" href="/pt/como-comecar">Começar</a></li>
<li class="nav-item"><a class="nav-link" href="/pt/resource-cards">Recursos</a></li>
<li class="nav-item"><a class="nav-link" href="/pt/publicacoes">Publicações</a></li>
<li class="nav-item"><a class="nav-link" href="/pt/sobre">Sobre</a></li>
</ul>
</nav>
'''
}
html = f"""<!DOCTYPE html>
<html lang="{lang}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title} - PlayData</title>
<link rel="stylesheet" href="/assets/css/bootstrap.min.css">
<link rel="stylesheet" href="/assets/css/beautifuljekyll.css">
<link rel="stylesheet" href="/assets/css/bootstrap-social.css">
<link rel="stylesheet" href="/assets/css/pygment_highlights.css">
</head>
<body>
{nav_links.get(lang, nav_links['en'])}
<div class="container" style="margin-top: 100px;">
<div class="row">
<div class="col-md-8 offset-md-2">
<h1>{title}</h1>
{content}
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="text-muted">PlayData © 2024</p>
</div>
</footer>
<script src="/assets/js/bootstrap.min.js"></script>
<script src="/assets/js/beautifuljekyll.js"></script>
</body>
</html>
"""
return html
def process_markdown_file(filepath, site_dir, lang):
"""Process a markdown file and create HTML in _site"""
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Extract frontmatter
frontmatter, body = extract_frontmatter(content)
fm_data = parse_frontmatter(frontmatter)
# Get title
title = fm_data.get('title', filepath.stem)
# Convert markdown to HTML
html_content = markdown_to_html(body)
# Create full HTML page
full_html = create_html_page(title, html_content, lang)
# Create output path
filename = filepath.stem
output_dir = site_dir / filename
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / "index.html"
# Write HTML file
with open(output_file, 'w', encoding='utf-8') as f:
f.write(full_html)
return str(output_file)
# Process English files
print("Building English pages...\n")
en_files = sorted(en_dir.glob('*.md'))
for en_file in en_files:
try:
output_path = process_markdown_file(en_file, site_en_dir, 'en')
print(f"✓ Created: {output_path}")
except Exception as e:
print(f"✗ Error processing {en_file.name}: {e}")
# Process Portuguese files
print("\nBuilding Portuguese pages...\n")
pt_files = sorted(pt_dir.glob('*.md'))
for pt_file in pt_files:
try:
output_path = process_markdown_file(pt_file, site_pt_dir, 'pt')
print(f"✓ Created: {output_path}")
except Exception as e:
print(f"✗ Error processing {pt_file.name}: {e}")
print("\nDone! Pages have been built and added to _site/")