-
Notifications
You must be signed in to change notification settings - Fork 23
/
generate-tags
executable file
·40 lines (33 loc) · 1.09 KB
/
generate-tags
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
#!/usr/bin/env python
import glob
import os
import re
post_dir = '_posts/'
tag_dir = 'tag/'
total_tags = []
for filename in glob.glob(post_dir + '*.md'):
matcher = r'^tags:$'
with open(filename, 'r') as fd:
tagged_line = False
for line in fd:
if tagged_line:
if line.startswith('---'):
tagged_line = False
elif not line.startswith('-'):
tagged_line = False
else:
total_tags.append(line[1:].strip())
if re.match(matcher, line):
tagged_line = True
total_tags = set(total_tags)
for tag in glob.glob(tag_dir + '*.md'):
os.remove(tag)
if not os.path.exists(tag_dir):
os.makedirs(tag_dir)
for tag in total_tags:
td = os.path.sep.join([tag_dir, tag])
if not os.path.exists(td):
os.makedirs(td)
with open(os.path.sep.join([td, 'index.md']), 'w+') as fd:
fd.write('---\nlayout: tag_page\ntitle: \"Tag: ' + tag + '\"\ntag: ' + tag + '\nrobots: noindex\n---\n')
print("Tags generated, count", total_tags.__len__())