forked from zacjones93/maximevaillancourt.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
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
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() |