-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_generator.py
60 lines (52 loc) · 1.71 KB
/
tag_generator.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
#!/usr/bin/env python3
'''
tag_generator.py
Copyright 2017 Long Qian
Contact: lqian8@jhu.edu
This script creates tags for your Jekyll blog hosted by Github page.
No plugins required.
'''
import glob
import os
post_dir = '_posts/'
draft_dir = '_drafts/'
tag_dir = 'tag/'
filenames = glob.glob(post_dir + '*md')
filenames = filenames + glob.glob(draft_dir + '*md')
total_tags = []
for filename in filenames:
f = open(filename, 'r', encoding='utf8')
crawl = False
for line in f:
if crawl:
current_tags = line.strip().split(':')
if current_tags[0] == 'tags':
if (current_tags[1].strip().startswith('[')):
clean_tag = ''.join(c for c in current_tags[1] if c not in '[]')
list_tags = map(str.strip, clean_tag.split(','))
total_tags.extend(list_tags)
else:
list_tags = map(str.strip, current_tags[1].strip().split())
total_tags.extend(list_tags)
crawl = False
break
if line.strip() == '---':
if not crawl:
crawl = True
else:
crawl = False
break
f.close()
total_tags = set(total_tags)
old_tags = glob.glob(tag_dir + '*.md')
for tag in old_tags:
os.remove(tag)
if not os.path.exists(tag_dir):
os.makedirs(tag_dir)
for tag in total_tags:
tag_filename = tag_dir + tag.replace(' ', '_') + '.md'
f = open(tag_filename, 'a')
write_str = '---\nlayout: tagpage\ntitle: \"Tag: ' + tag + '\"\ntag: ' + tag + '\nrobots: noindex\nexclude: true\n---\n'
f.write(write_str)
f.close()
print("Tags generated, count", total_tags.__len__())