-
Notifications
You must be signed in to change notification settings - Fork 3
/
generateTitle.py
41 lines (30 loc) · 1.16 KB
/
generateTitle.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
import re
import glob
exclude = ['_collections/_extras/', '_collections/_episodes/',] # Don't generate titles for these folders
generateNav = True # Generate new navigation after generating titles
def generate(path):
file = open(path, 'r')
content = file.read()
p = re.compile(r'^# (.*)', re.M) # Find title based on #
title = re.findall(p, content)
if len(title) == 0: # kind of messy, refactor?
print("No title found for '{}'".format(path))
return
else:
title = title[0]
p = re.compile(r'^---\ntitle(.*?)\n---', re.MULTILINE) # Remove old title
content = re.sub(p, "", content)
newTitle = '---\ntitle: "{}"\n---'.format(title)
content = newTitle + content
file = open(path, 'w') # Replace old file with new
file.write(content)
file.truncate()
file.close()
for filename in glob.iglob('_collections/**/0**.md', recursive=True):
if all(s not in filename for s in exclude):
generate(filename)
for filename in glob.iglob('_collections/**/1**.md', recursive=True):
if all(s not in filename for s in exclude):
generate(filename)
# if generateNav:
# import makeNavigationList