-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontentify.py
More file actions
226 lines (165 loc) · 5.99 KB
/
contentify.py
File metadata and controls
226 lines (165 loc) · 5.99 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
from json import dump
from datetime import datetime
from os import mkdir, walk, popen, remove
from os.path import join, isdir, dirname, basename, getmtime, isfile
from re import compile, split
from shutil import copy, copytree, rmtree
from time import time
from obsidian_to_hugo import ObsidianToHugo
contentdir = './content'
wikidir = './wiki'
tmpdir = './tmp'
staticdir = './static'
changespath = './wiki/최근 편집.md'
connectionpath = './static/connection.json'
def addfrontmatter(wfile, tfile):
with open(tfile, 'w') as tmpfile:
if wfile.endswith('_index.md'):
title = '대문'
else:
title = basename(wfile)[:-3]
date = popen(f"cd wiki; git log -1 --pretty=format:%ci \"{wfile[7:]}\"").read()
if not date:
date = datetime.now().astimezone()
else:
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S %z')
date = date.strftime('%Y-%m-%dT%H:%M:%S%z')
date = date[:-2] + ':' + date[-2:]
tmpfile.write('---\n')
tmpfile.write(f'title: {title}\n')
tmpfile.write(f'date: {date}\n')
tmpfile.write('---\n')
with open(wfile, 'r') as wikifile:
while buffer := wikifile.read(1024):
tmpfile.write(buffer)
def fillfrontmatter(path):
title = basename(dirname(path))
date = datetime.now().astimezone()
date = date.strftime('%Y-%m-%dT%H:%M:%S%z')
date = date[:-2] + ':' + date[-2:]
with open(path, 'w') as file:
file.write("---\n")
file.write(f"title: {title}\n")
file.write(f"date: {date}\n")
file.write("---\n")
refre = compile(r"\!\[([^\{\}]*)\]\(\{\{< ref \"([^\{\}]+)\" >\}\}\)")
def imager(content):
new_content = ""
while match := refre.search(content):
s, e = match.span()
src = match.groups()[1][7:]
new_content += content[:s]
new_content += f"{{{{< figure src=\"/{src}\" >}}}}"
content = content[e:]
new_content += content
return new_content
def write_recent_changes():
changesraw = popen(
'git config core.quotepath false ; '
'cd wiki ; '
'git log --pretty=format:"%h %s - %an" --name-only -n 50 -z'
).read()
# changesraw = changesraw.split('\n')
changesraw = split(r'\n|\0', changesraw)
changes = list()
for i in range(len(changesraw)):
line = changesraw[i].strip()
iscommit = iscommitre.match(changesraw[i])
if not line:
continue
if iscommit:
prefix = '- '
line = line[8:]
else:
if line.endswith(".md"):
line = line[:-3]
if isdir(join(wikidir, line)) or isfile(join(wikidir, line) + ".md"):
line = f'[[{line}]]'
prefix = ' - '
changesraw[i] = prefix + line
with open(changespath, 'w') as file:
file.write('\n'.join(changesraw))
iscommitre = compile(r'^[0-9a-f]{7} .*')
def preprocess_filetree():
for path, directories, filenames in walk(wikidir):
if '/.git' in path:
continue
tpath = join(tmpdir, path[7:])
isdir(tpath) or mkdir(tpath)
isfile(join(tpath, '_index.md')) or fillfrontmatter(join(tpath, '_index.md'))
bn = basename(path)
if '_index.md' in filenames:
addfrontmatter(join(path, '_index.md'), join(tpath, '_index.md'))
for filename in filenames:
if not filename.endswith('.md'):
continue
if filename == 'README.md':
continue
if filename == '_index.md':
continue
fbn = filename[:-3]
isdir(join(tpath, fbn)) or mkdir(join(tpath, fbn))
addfrontmatter(join(path, filename), join(tpath, fbn, '_index.md'))
def oth():
rmtree(join(tmpdir, 'static'))
isdir(contentdir) or mkdir(contentdir)
obsidian_to_hugo = ObsidianToHugo(
obsidian_vault_dir=tmpdir,
hugo_content_dir=contentdir,
processors=[imager]
)
obsidian_to_hugo.run()
def write_connections():
connections = list()
ids = set()
realids = set()
link = compile(r"\[\[(([^|\]]+)|([^|\]]+)\|([^|\]]+))\]\]")
for path, directories, filenames in walk(tmpdir):
name = basename(path)
name = "대문" if basename(path) == "tmp" else basename(path)
connections.append({"data": {"id": path, "name": name}})
ids.add(path)
if name in ["최근 편집"]:
continue
for directory in directories:
connections.append({"data": {
"source": path,
"target": join(path, directory)
}})
with open(join(path, "_index.md"), "r") as file:
content = file.read()
for links in link.finditer(content):
group = links.groups()
nid = "./tmp/" + (group[1] if group[1] else group[2])
connections.append({"data": {
"source": path,
"target": nid
}})
realids.add(path)
realids.add(nid)
for path in realids - ids:
name = basename(path)
connections.append({"data": {"id": path, "name": name}})
with open(connectionpath, "w") as file:
dump(connections, file, ensure_ascii=False)
def main():
starttime = time()
isdir(staticdir) and rmtree(staticdir)
copytree(join(wikidir, "static"), staticdir)
isdir(tmpdir) and rmtree(tmpdir)
write_recent_changes()
et = (time() - starttime) * 1000
print(f"recent changes gotten: {int(et):,} ms")
preprocess_filetree()
et = (time() - starttime) * 1000
print(f"preprocess filetree done: {int(et):,} ms")
write_connections()
et = (time() - starttime) * 1000
print(f"write connections done: {int(et):,} ms")
oth()
et = (time() - starttime) * 1000
print(f"obsidian to hugo done: {int(et):,} ms")
et = (time() - starttime) * 1000
print(f'Total in {int(et):,} ms')
if __name__ == '__main__':
main()