-
Notifications
You must be signed in to change notification settings - Fork 18
/
format.py
115 lines (79 loc) · 3.08 KB
/
format.py
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
import os
import json
import shutil
title, section = '', ''
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
files = ["index", "next", "prev"]
templates = {}
# load in the templates
for file in files :
with open(f"templates/{file}.html") as f:
templates[file] = f.read()
# file structure: sike.pona.la/jan/kala-pona/embed.html
if os.path.exists("jan"):
shutil.rmtree("jan")
os.makedirs("jan")
# load in the jans
with open('jan.json') as json_file:
jan_data = json.load(json_file)
# load in our main page
content = ""
with open("index_template.html") as f:
content = f.read()
with open("index.html", 'w') as f:
jan_list = ""
for jan in jan_data:
jan_list += f'<a href="{jan["url"]}">{jan["name"]}</a>'
content = content.replace("{{ jan }}", jan_list)
f.write(content)
# sike.pona.la/jan
with cd("jan"):
for nanpa in range(0, len(jan_data)):
# make their folder
if os.path.exists(jan_data[nanpa]["name"]):
shutil.rmtree(jan_data[nanpa]["name"])
os.makedirs(jan_data[nanpa]["name"])
# sike.pona.la/jan/name
with cd(jan_data[nanpa]["name"]):
# index of previous person and next person
nanpa_next = nanpa + 1 if nanpa < len(jan_data) - 1 else 0
nanpa_prev = nanpa - 1 if nanpa > 0 else len(jan_data) - 1
# links to go to the previous and next person's profiles
nextlink = jan_data[nanpa_next]["url"]
prevlink = jan_data[nanpa_prev]["url"]
# create the files
for file in files:
# embed.html, prev.html, next.html
destination = f"{file}.html"
# write the html and save
with open(destination, 'w') as f:
output = templates[file]
output = output.replace("{{ prevlink }}", prevlink)
output = output.replace("{{ nextlink }}", nextlink)
f.write(output)
# # update data json dict
#
# if title not in article_data.keys():
# article_data[title] = {}
# article_data[title]["title"] = title
# article_data[title]["index"] = int(os.path.splitext(file_name)[0].split('_')[0])
# article_data[title]["tags"] = tags
#
#
#
#
# json_string = json.dumps(article_data)
#
# with open('scripts/articles.json', 'w') as outfile:
# outfile.write(json_string)
# rebuild the article pages
# rebuild tag pages
# rebuild user pages