112 lines
2.3 KiB
Python
Executable file
112 lines
2.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import os
|
|
from io import TextIOWrapper
|
|
import sys
|
|
|
|
NAVBAR_FMT = '''
|
|
<ul>
|
|
<li><a href="index.html">
|
|
Home{}
|
|
</a></li>
|
|
<li><a href="projects.html">
|
|
Projects{}
|
|
</a></li>
|
|
<li><a href="about.html">
|
|
About & Contact{}
|
|
</a></li>
|
|
</ul>
|
|
'''
|
|
SELECT_FMT = ' <'
|
|
current_source = ''
|
|
|
|
|
|
def token_to_str(token: str) -> str:
|
|
global current_source
|
|
|
|
if token == 'NAVBAR':
|
|
home_str = ''
|
|
projects_str = ''
|
|
about_str = ''
|
|
|
|
if current_source == 'index.html':
|
|
home_str = SELECT_FMT
|
|
elif current_source == 'projects.html':
|
|
projects_str = SELECT_FMT
|
|
elif current_source == 'about.html':
|
|
about_str = SELECT_FMT
|
|
|
|
return NAVBAR_FMT.format(home_str, projects_str, about_str)
|
|
elif token.endswith('.html'):
|
|
return open('includes/' + token).read()
|
|
else:
|
|
return '{{' + token + '}}'
|
|
|
|
|
|
def replace_line(line: str) -> (str, int):
|
|
ret = ''
|
|
count = 0
|
|
|
|
replacing = False
|
|
token = ''
|
|
i = 0
|
|
while i < (len(line) - 2):
|
|
if replacing:
|
|
if line[i:i+2] == r'}}':
|
|
replacing = False
|
|
ret += token_to_str(token)
|
|
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)
|
|
|
|
def stream_template(in_fd: TextIOWrapper, out_fd: TextIOWrapper) -> int:
|
|
replacements = 0
|
|
|
|
while True:
|
|
line = in_fd.readline()
|
|
if line == '':
|
|
break
|
|
|
|
new_line, count = replace_line(line)
|
|
out_fd.write(new_line)
|
|
replacements += count
|
|
|
|
return replacements
|
|
|
|
|
|
def main():
|
|
global current_source
|
|
|
|
includes = os.listdir('includes')
|
|
sources = os.listdir('src')
|
|
|
|
print(includes)
|
|
print(sources)
|
|
|
|
if not os.path.isdir('public'):
|
|
os.mkdir('public')
|
|
|
|
for source in sources:
|
|
current_source = source
|
|
|
|
in_fd = open('src/' + source, 'r')
|
|
out_fd = open('public/' + source, 'w')
|
|
stream_template(in_fd, out_fd)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|