Created
March 26, 2020 01:03
-
-
Save junhan-z/021b210dcbba63c8408bf9ca0953249b to your computer and use it in GitHub Desktop.
Generate tag directories for Jekyll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
import re | |
import glob | |
import os | |
TAG_TEMPLATE = '''--- | |
layout: tag_pages | |
title: "{tag}" | |
tag: {tag} | |
--- | |
''' | |
def get_tags(line): | |
tag_list = re.search("\[.*\]", line) \ | |
.group(0).replace('[', '').replace(']', '') | |
return list(filter( | |
lambda x: x, map(lambda x: x.strip(), tag_list.split(',')) | |
)) | |
def extract_tags(file_name): | |
f = open(file_name, 'r', encoding='utf8') | |
front_matter = '---' | |
fm_count = 0 | |
tags = [] | |
for line in f.readlines(): | |
if fm_count == 2: | |
break | |
if line == front_matter: | |
fm_count += 1 | |
if line[:4] == 'tags': | |
tags.extend(get_tags(line)) | |
return tags | |
def _summary(op, file_names): | |
print(op) | |
print("----") | |
for f in file_names: | |
print(f) | |
else: | |
print('None') | |
print("----\n") | |
def update_tag_files(): | |
# create tag directory | |
tag_dir = 'tag' | |
if not os.path.exists(tag_dir): | |
os.makedirs(tag_dir) | |
# collect all the tags | |
tags = [] | |
file_names = glob.glob('_posts/*') | |
for f in file_names: | |
tags.extend(extract_tags(f)) | |
tags = set(tags) | |
removed = [] | |
skipped = [] | |
created = [] | |
# remove non-existing tags | |
tag_files = glob.glob('{}/*'.format(tag_dir)) | |
for file_name in tag_files: | |
# let's hack :) | |
# it's always {tag_dir}/{tag}{.md} | |
tag = file_name[len(tag_dir) + 1:-3] | |
if tag not in tags: | |
# outdated tag file, remove | |
os.remove(file_name) | |
removed.append(file_name) | |
for tag in tags: | |
file_name = '{}/{}.md'.format(tag_dir, tag) | |
if os.path.exists(file_name): | |
skipped.append(file_name) | |
else: | |
f = open(file_name, 'a') | |
f.write(TAG_TEMPLATE.format(tag=tag)) | |
f.close() | |
created.append(file_name) | |
_summary('Created', created) | |
_summary('Skipped', skipped) | |
_summary('Removed', removed) | |
if __name__ == "__main__": | |
update_tag_files() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment