website/tag-generator.py
2022-09-10 10:36:11 +02:00

72 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python
"""
tag_generator.py
Copyright 2022 Claudio Maradonna
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 = "_pages/"
tag_dir = "tag/"
filenames = glob.glob(post_dir + "*html")
total_tags = []
for filename in filenames:
f = open(filename, "r")
crawl = False
for line in f:
if crawl:
current_tags = line.strip().split()
if current_tags[0] == "tags:":
total_tags.extend(current_tags[1:])
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 + "*.html")
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 + ".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'
)
f.write(write_str)
f.close()
# print(f"{tag}: \"#{tag}\"")
print("Tags generated, count", total_tags.__len__())