2026-03-15 11:55:14 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import os
|
|
|
|
|
from io import TextIOWrapper
|
2026-11-23 01:38:31 -06:00
|
|
|
import shutil
|
2026-03-15 11:55:14 -05:00
|
|
|
import sys
|
2026-11-23 01:38:31 -06:00
|
|
|
import re
|
2026-03-15 11:55:14 -05:00
|
|
|
|
|
|
|
|
NAVBAR_FMT = '''
|
|
|
|
|
<ul>
|
2026-11-23 01:38:31 -06:00
|
|
|
<li><a href="/index.html">
|
2026-03-15 11:55:14 -05:00
|
|
|
Home{}
|
|
|
|
|
</a></li>
|
2026-11-23 01:38:31 -06:00
|
|
|
<li><a href="/projects.html">
|
2026-03-15 11:55:14 -05:00
|
|
|
Projects{}
|
|
|
|
|
</a></li>
|
2026-11-23 01:38:31 -06:00
|
|
|
<details BLOG_OPEN>
|
|
|
|
|
<summary>Ramblings</summary>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="/blog/2026-11-23.html">
|
|
|
|
|
2026-11-22{}
|
|
|
|
|
</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</details>
|
|
|
|
|
<li><a href="/about.html">
|
2026-03-15 11:55:14 -05:00
|
|
|
About & Contact{}
|
|
|
|
|
</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
'''
|
2026-11-23 01:38:31 -06:00
|
|
|
NAVBAR_LINES = NAVBAR_FMT.split('\n')
|
2026-03-15 11:55:14 -05:00
|
|
|
SELECT_FMT = ' <'
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
def token_to_str(token: str, current_source: str) -> str:
|
2026-03-15 11:55:14 -05:00
|
|
|
if token == 'NAVBAR':
|
2026-11-23 01:38:31 -06:00
|
|
|
fmt_strs = []
|
|
|
|
|
href_re = re.compile('href\\="(.+)"')
|
|
|
|
|
current_href = ''
|
2026-03-15 11:55:14 -05:00
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
for line in NAVBAR_LINES:
|
|
|
|
|
m = href_re.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
current_href = m.group(1)
|
2026-03-15 11:55:14 -05:00
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
if '{}' in line:
|
|
|
|
|
if (f'src{current_href}' == current_source):
|
|
|
|
|
fmt_strs.append(SELECT_FMT)
|
|
|
|
|
else:
|
|
|
|
|
fmt_strs.append('')
|
|
|
|
|
|
|
|
|
|
if current_source.startswith('src/blog'):
|
|
|
|
|
navbar_fmt = NAVBAR_FMT.replace('BLOG_OPEN', 'open')
|
|
|
|
|
else:
|
|
|
|
|
navbar_fmt = NAVBAR_FMT.replace('BLOG_OPEN', '')
|
|
|
|
|
|
|
|
|
|
print(fmt_strs)
|
|
|
|
|
return navbar_fmt.format(*fmt_strs)
|
2026-03-15 11:55:14 -05:00
|
|
|
elif token.endswith('.html'):
|
|
|
|
|
return open('includes/' + token).read()
|
|
|
|
|
else:
|
|
|
|
|
return '{{' + token + '}}'
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
def replace_line(line: str, current_source: str) -> (str, int):
|
2026-03-15 11:55:14 -05:00
|
|
|
ret = ''
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
|
|
replacing = False
|
|
|
|
|
token = ''
|
|
|
|
|
i = 0
|
|
|
|
|
while i < (len(line) - 2):
|
|
|
|
|
if replacing:
|
|
|
|
|
if line[i:i+2] == r'}}':
|
|
|
|
|
replacing = False
|
2026-11-23 01:38:31 -06:00
|
|
|
ret += token_to_str(token, current_source)
|
2026-03-15 11:55:14 -05:00
|
|
|
i += 2
|
|
|
|
|
token = ''
|
|
|
|
|
count += 1
|
|
|
|
|
else:
|
|
|
|
|
token += line[i]
|
|
|
|
|
i += 1
|
|
|
|
|
else:
|
|
|
|
|
if line[i:i+2] == r'{{':
|
|
|
|
|
replacing = True
|
|
|
|
|
i += 2
|
|
|
|
|
else:
|
|
|
|
|
ret += line[i]
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
if replacing:
|
|
|
|
|
ret += '{{' + token
|
|
|
|
|
ret += line[i:]
|
|
|
|
|
|
|
|
|
|
return (ret, count)
|
|
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
def stream_template(in_fd: TextIOWrapper, out_fd: TextIOWrapper, current_source: str) -> int:
|
2026-03-15 11:55:14 -05:00
|
|
|
replacements = 0
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
line = in_fd.readline()
|
|
|
|
|
if line == '':
|
|
|
|
|
break
|
|
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
new_line, count = replace_line(line, current_source)
|
2026-03-15 11:55:14 -05:00
|
|
|
out_fd.write(new_line)
|
|
|
|
|
replacements += count
|
|
|
|
|
|
|
|
|
|
return replacements
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
def source_dir_to_output(source_dir: str, output_dir: str):
|
|
|
|
|
sources = os.listdir(source_dir)
|
2026-03-15 11:55:14 -05:00
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
if not os.path.isdir(output_dir):
|
|
|
|
|
os.mkdir(output_dir)
|
|
|
|
|
|
|
|
|
|
for source in sources:
|
|
|
|
|
source_full = f'{source_dir}/{source}'
|
|
|
|
|
output_full = f'{output_dir}/{source}'
|
|
|
|
|
|
|
|
|
|
if os.path.isdir(source_full):
|
|
|
|
|
source_dir_to_output(source_full, output_full)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if source_full.endswith('.html'):
|
|
|
|
|
in_fd = open(source_full, 'r')
|
|
|
|
|
out_fd = open(output_full, 'w')
|
|
|
|
|
stream_template(in_fd, out_fd, source_full)
|
|
|
|
|
else:
|
|
|
|
|
shutil.copy(source_full, output_full)
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
print(NAVBAR_FMT)
|
2026-03-15 11:55:14 -05:00
|
|
|
includes = os.listdir('includes')
|
|
|
|
|
sources = os.listdir('src')
|
|
|
|
|
|
|
|
|
|
print(includes)
|
|
|
|
|
print(sources)
|
|
|
|
|
|
2026-11-23 01:38:31 -06:00
|
|
|
source_dir_to_output('src', 'public')
|
2026-03-15 11:55:14 -05:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|