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.

108 lines
4.3 KiB
Python

import json
import frontmatter
import os
from . import crossword
def dump(order=None):
# The function can receive an optional order for the contents
# in the form of a list of folder like
# ['intro','what-is-a-loot-box', 'crosswords', 'one-sentence-game-ideas', 'nim', 'mimic', 'unfinished-thoughts', 'the-leader', 'connect-less', 'xquisite', 'katamari', 'life-hacks', 'karaoke', 'outro']
folders = order
path = "postit/static/contents"
if folders == None:
# If no order is passed, list all the folders in the contents directory
folders = [f.name for f in os.scandir(path) if f.is_dir()]
# Total number of postit
total = 0
# Dictionary to fill with the contributions
contents = {}
for folder in folders:
# Initialize the contribution as an empty list of postit
contribution = []
try:
# Load the file from a contents.md file
# (we are intrested only in the yaml metadata tho)
with open(f"{path}/{folder}/contents.md", "r", encoding="utf8") as f:
metadata, body = frontmatter.parse(f.read())
for content in metadata["contents"]:
# For each entry in the contents list create a default postit dictionary
# with the title of the contribution
postit = {
'title' : metadata['title']
}
# We have basically 2 types of postit:
# 1. Simple postit with just a string of text
# 2. Complex postit with different informations
# ii) We start from this second kind, that is loaded as a dictionary
if type(content) == dict:
# Pass all the properties from the yaml object to the post-it dictionary
for key in content:
postit[key] = content[key]
# --------------------------
# Custom post-it generation
# Add here the function for generate dynamic postit
# (such as the crossword imaginary grid)
# Generate the Imaginary Grid for the Crossword game
# (Only for the Loot Box category)
if "word" in content and content['category'] != 'Loot Box':
continue
elif "word" in content:
if content["word"]:
# Generate a postit for each letter in the word
for cell in crossword.generate_grid(content['word'], content["start"], content["direction"]):
contribution.append(
{
"title": metadata["title"],
"definition": " ",
"category": content["category"],
"start": cell,
})
#----------------------------
else:
# i) Simple string postit
postit["description"] = content
# Add the postit to the contribution
contribution.append(postit)
# Log the amount of postit for the current contribution
amount = len(contribution)
print(f"{amount:03} - {folder}")
# Add the contribution to the total amount
total = total + amount
# Add the contribution to the contents object
contents[folder] = contribution
except Exception as e:
# If a markdown file is broken print the error and move on
# without breaking everything
print(f"{folder} has an error!")
print(e)
print(f'Total: {total}')
# Store the postit in a JSON file
# in order to avoid repeating this process everytime we want the postit.
with open("postit/contents.json", "w") as f:
f.write(json.dumps(contents))