-
Notifications
You must be signed in to change notification settings - Fork 2
/
tag_generator.py
69 lines (58 loc) · 2.07 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
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
'''
REQUIRES PYTHON 3.5+
Razvi on September 2018
Contact: Twitter @Razvieu
This script creates tags for your Jekyll blog hosted by Github page.
No plugins required. If you want to use it, make sure you edit post_dirs
to fit your needs.
Modified on 20th January 2020. Now the script recursively traverses all
the directories and subdirectories starting from "./" and checks every ".md"
file looking for tags. It now requires Python 3.5+
'''
import glob
import os
#post_dirs = ['./','./_microcorruption/','./_ctfwriteups/']
tag_dir = './tags/'
total_tags = []
# Checking dir existence
if not os.path.exists(tag_dir):
os.makedirs(tag_dir)
# Deleting old tags
old_tags = glob.glob(tag_dir + '*.md')
for tag in old_tags:
os.remove(tag)
#for post_dir in post_dirs:
#print("The directory being read: " + post_dir)
for filename in glob.iglob("./" + '**/*', recursive=True):
if filename.endswith(".md"):
print(filename)
f = open(filename, 'r', encoding="utf8")
crawl = False
for line in f:
print(line)
if crawl:
#current_tags = line.strip().split()
if line.startswith("tags:"):
auxTags = line.replace("tags:", "")
auxTagsBrackets = auxTags.replace("[", "").replace("]", "")
myTags = auxTagsBrackets.strip().split(", ")
total_tags.extend(myTags[:])
crawl = False
break
if line.strip() == '---':
if not crawl:
crawl = True
else:
crawl = False
break
f.close()
total_tags = set(total_tags)
for tag in total_tags:
tag_filename = tag_dir + tag + '.md'
f = open(tag_filename, 'a')
write_str = '---\nlayout: tagpage\ntitle: \"Tag: ' + tag + '\"\ntag: ' + tag + '\nrobots: noindex\n---\n'
f.write(write_str)
f.close()
print("A total of ", total_tags.__len__(), " tags have been generated.\nThe generated tags are: ")
print(*list(total_tags), sep=", ")