[O] Use regex to split markdown

This commit is contained in:
Hykilpikonna
2021-12-26 17:58:59 -05:00
parent e363e4e66c
commit 2bca3b95f0
+3 -4
View File
@@ -1,5 +1,6 @@
import json import json
import os import os
import re
from datetime import datetime, date from datetime import datetime, date
from pathlib import Path from pathlib import Path
@@ -16,6 +17,7 @@ class EnhancedJSONEncoder(json.JSONEncoder):
if __name__ == '__main__': if __name__ == '__main__':
# Loop through all blog posts
metas = [] metas = []
for b in os.listdir('content/posts'): for b in os.listdir('content/posts'):
if not b.endswith('.md'): if not b.endswith('.md'):
@@ -24,10 +26,7 @@ if __name__ == '__main__':
# Read blog posts # Read blog posts
file = 'content/posts/' + b file = 'content/posts/' + b
with open(file, 'r', encoding='utf-8') as f: with open(file, 'r', encoding='utf-8') as f:
md = f.read().strip() yml, md = re.split('---+\n', f.read().strip())[1:]
start = md.index('---\n')
stop = md.index('---\n', start + 1)
yml = md[start + 4:stop]
meta = {**yaml.safe_load(yml), 'file': file} meta = {**yaml.safe_load(yml), 'file': file}
metas.append(meta) metas.append(meta)
meta.setdefault('tags', []) meta.setdefault('tags', [])