Skip to content

Commit

Permalink
Create process_tags.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ibbsbbr authored Jun 24, 2024
1 parent 18aa857 commit 86fd47a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions process_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import glob
import yaml
from collections import defaultdict

def get_posts():
posts = []
for filename in glob.glob('_posts/*.md'):
with open(filename, 'r') as file:
content = file.read()
front_matter, _ = content.split('---', 2)[1:]
post = yaml.safe_load(front_matter)
post['filename'] = filename
posts.append(post)
return posts

def generate_tag_pages(posts):
tags = defaultdict(list)
for post in posts:
for tag in post.get('tags', []):
tags[tag].append(post)

if not os.path.exists('tags'):
os.makedirs('tags')

for tag, posts in tags.items():
with open(f'tags/{tag}.html', 'w') as file:
file.write(f'---\nlayout: tag\ntag: {tag}\n---\n')
for post in posts:
file.write(f'- [{post["title"]}]({post["slug"]})\n')

def main():
posts = get_posts()
generate_tag_pages(posts)

if __name__ == '__main__':
main()

0 comments on commit 86fd47a

Please sign in to comment.