You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.6 KiB
Python

import json
import frontmatter
import os
import string
def dump():
# list all the folders
path = "postit/static/contents"
folders = [f.name for f in os.scandir(path) if f.is_dir()]
contents = []
for folder in folders:
with open(f"{path}/{folder}/contents.md", "r", encoding="utf8") as f:
metadata, body = frontmatter.parse(f.read())
print(folder)
for content in metadata["contents"]:
if type(content) == dict:
if "img" in content:
postit = {
"title": metadata["title"],
"description": content["alt"],
"img": content["img"],
"slug": folder,
}
elif "card" in content:
postit = {
"title": metadata["title"],
"card": content["card"],
"quote": content["quote"],
"motivation": content["motivation"],
"vision": content["vision"],
"empathy": content["empathy"],
"positivity": content["positivity"],
"slug": folder,
}
elif "word" in content:
postit = {
"title": metadata["title"],
"definition": content['definition'],
"category": content['category'],
"start": content['start'],
"word": content['word'],
"direction": content['direction'],
"slug": folder
}
if content['word']:
start = (string.ascii_uppercase.index(content['start'][0]),int(content['start'][1:]))
print(start)
for i in range(len(content['word'])):
if content['direction'] == 'H':
# increase number
start_f = f"{string.ascii_uppercase[start[0]]}{start[1] + i}"
# print(start_f)
pass
else:
# increase letter
start_f = f"{string.ascii_uppercase[start[0] + i]}{start[1]}"
# print(start_f)
pass
contents.append({
"title": metadata["title"],
"definition": " ",
"category": content['category'],
"start": start_f,
"word": content['word'],
"direction": content['direction'],
"slug": folder
})
else:
postit = {
"title": metadata["title"],
"description": content,
"slug": folder,
}
contents.append(postit)
with open("postit/contents.json", "w") as f:
f.write(json.dumps(contents))