website/tag-generator.py

73 lines
1.5 KiB
Python
Raw Normal View History

2019-08-13 15:16:48 +02:00
#!/usr/bin/env python
2021-08-20 16:48:07 +02:00
"""
2019-08-13 15:16:48 +02:00
tag_generator.py
Copyright 2022 Claudio Maradonna
2019-08-13 15:16:48 +02:00
Copyright 2017 Long Qian
Contact: lqian8@jhu.edu
This script creates tags for your Jekyll blog hosted by Github page.
No plugins required.
2021-08-20 16:48:07 +02:00
"""
2019-08-13 15:16:48 +02:00
import glob
import os
2021-08-20 16:48:07 +02:00
post_dir = "_pages/"
tag_dir = "tag/"
2019-08-13 15:16:48 +02:00
2021-08-20 16:48:07 +02:00
filenames = glob.glob(post_dir + "*html")
2019-08-13 15:16:48 +02:00
total_tags = []
for filename in filenames:
2021-08-20 16:48:07 +02:00
f = open(filename, "r")
2019-08-13 15:16:48 +02:00
crawl = False
for line in f:
if crawl:
current_tags = line.strip().split()
2021-08-20 16:48:07 +02:00
if current_tags[0] == "tags:":
2019-08-13 15:16:48 +02:00
total_tags.extend(current_tags[1:])
crawl = False
break
2021-08-20 16:48:07 +02:00
if line.strip() == "---":
2019-08-13 15:16:48 +02:00
if not crawl:
crawl = True
else:
crawl = False
break
f.close()
total_tags = set(total_tags)
2021-08-20 16:48:07 +02:00
old_tags = glob.glob(tag_dir + "*.html")
2019-08-13 15:16:48 +02:00
for tag in old_tags:
os.remove(tag)
if not os.path.exists(tag_dir):
os.makedirs(tag_dir)
for tag in total_tags:
2021-08-20 16:48:07 +02:00
tag_filename = tag_dir + tag + ".html"
f = open(tag_filename, "a")
write_str = (
"---\nlayout: tagpage\ntitle: meta.titles.tags."
+ tag
+ '\ntag: "'
+ tag
+ '"\ndescription: meta.descriptions.tags.'
+ tag
+ '\nnamespace: tag-'
+ tag
+ '\npermalink: /tag/'
+ tag
+ '\npermalink_eu: /tag/'
+ tag
+ '\n---\n'
2021-08-20 16:48:07 +02:00
)
2019-08-13 15:16:48 +02:00
f.write(write_str)
f.close()
# print(f"{tag}: \"#{tag}\"")
2019-08-13 15:16:48 +02:00
print("Tags generated, count", total_tags.__len__())