[O] Separate tags from categories
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from collections import Counter
|
||||
from datetime import datetime, date
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class EnhancedJSONEncoder(json.JSONEncoder):
|
||||
class Encoder(json.JSONEncoder):
|
||||
def default(self, o: object) -> object:
|
||||
# Support encoding datetime
|
||||
if isinstance(o, (datetime, date)):
|
||||
@@ -17,8 +18,9 @@ class EnhancedJSONEncoder(json.JSONEncoder):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
posts = []
|
||||
|
||||
# Loop through all blog posts
|
||||
metas = []
|
||||
for b in os.listdir('content/posts'):
|
||||
if not b.endswith('.md'):
|
||||
continue
|
||||
@@ -27,27 +29,31 @@ if __name__ == '__main__':
|
||||
file = 'content/posts/' + b
|
||||
with open(file, 'r', encoding='utf-8') as f:
|
||||
yml, md = re.split('---+\n', f.read().strip())[1:]
|
||||
meta = {**yaml.safe_load(yml), 'file': file}
|
||||
metas.append(meta)
|
||||
meta.setdefault('tags', [])
|
||||
post = {**yaml.safe_load(yml), 'file': file}
|
||||
posts.append(post)
|
||||
post.setdefault('tags', [])
|
||||
|
||||
# Parse date
|
||||
if 'date' not in meta:
|
||||
if 'date' not in post:
|
||||
try:
|
||||
meta['date'] = date(int(b[:4]), int(b[5:7]), int(b[8:10]))
|
||||
post['date'] = date(int(b[:4]), int(b[5:7]), int(b[8:10]))
|
||||
except:
|
||||
pass
|
||||
|
||||
# Convert image path
|
||||
if 'title_image' in meta and '/' not in meta['title_image']:
|
||||
meta['title_image'] = 'content/images/' + meta['title_image']
|
||||
if 'title_image' in post and '/' not in post['title_image']:
|
||||
post['title_image'] = 'content/images/' + post['title_image']
|
||||
|
||||
meta['content'] = md.strip()
|
||||
post['content'] = md.strip()
|
||||
|
||||
metas.sort(key=lambda x: x['date'], reverse=True)
|
||||
posts.sort(key=lambda x: x['date'], reverse=True)
|
||||
|
||||
metas_path = Path('content/generated/metas.json')
|
||||
metas_path.parent.mkdir(exist_ok=True, parents=True)
|
||||
with open(metas_path, 'w', encoding='utf-8') as f:
|
||||
f.write('[\n ' + ',\n '.join(
|
||||
json.dumps(m, cls=EnhancedJSONEncoder, ensure_ascii=False) for m in metas) + '\n]')
|
||||
# Count tags and categories
|
||||
tags = Counter([t for p in posts for t in p['tags']])
|
||||
cats = Counter([p['category'] for p in posts if 'category' in p])
|
||||
tags, cats = [[(k, c[k]) for k in c] for c in [tags, cats]]
|
||||
print(tags, cats)
|
||||
|
||||
mp = Path('content/generated/metas.json')
|
||||
mp.parent.mkdir(exist_ok=True, parents=True)
|
||||
mp.write_text(json.dumps(posts, cls=Encoder, ensure_ascii=False, indent=2), 'utf-8')
|
||||
|
||||
@@ -3,6 +3,7 @@ title: Clive Wearing 的日记
|
||||
subtitle: 十五秒的记忆、开心又无意义的人生、人的自私愿望
|
||||
title_image: "2021-10-14 Clive Wearing journal.png"
|
||||
tags: [心理]
|
||||
category: 想想想
|
||||
---
|
||||
|
||||
这是一个海马体因为脑炎失效的人写的一段日记——他没办法形成新的长期记忆,只拥有十五秒的短期记忆,因此他的时间是静止的,一遍一遍重复地体验着康复后第一次苏醒的喜悦。
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
title: 651层
|
||||
title_image: "2021-12-07 earth.jpg"
|
||||
tags: [梦]
|
||||
category: 梦
|
||||
---
|
||||
|
||||
今天又做了一个特别特别奇怪的梦,试试把它写成一段故事w
|
||||
|
||||
Reference in New Issue
Block a user