mirror of
https://github.com/Dictionarry-Hub/database.git
synced 2025-12-11 16:26:58 +00:00
add(script): support for bundling markdown files with frontmatter and custom JSON encoder
This commit is contained in:
@@ -1,20 +1,72 @@
|
|||||||
import yaml
|
import yaml
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone, date
|
||||||
|
|
||||||
|
|
||||||
|
# Add this class at the top with the imports
|
||||||
|
class DateTimeEncoder(json.JSONEncoder):
|
||||||
|
|
||||||
|
def default(self, obj):
|
||||||
|
if isinstance(obj, (date, datetime)):
|
||||||
|
return obj.isoformat()
|
||||||
|
return super().default(obj)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_frontmatter(content):
|
||||||
|
"""Parse YAML frontmatter from markdown"""
|
||||||
|
if content.startswith("---"):
|
||||||
|
try:
|
||||||
|
second_sep = content[3:].find("---")
|
||||||
|
if second_sep != -1:
|
||||||
|
yaml_text = content[3:second_sep + 3]
|
||||||
|
meta = yaml.safe_load(yaml_text)
|
||||||
|
content = content[second_sep + 6:].strip()
|
||||||
|
return meta, content
|
||||||
|
except yaml.YAMLError:
|
||||||
|
pass
|
||||||
|
return {}, content
|
||||||
|
|
||||||
|
|
||||||
|
def bundle_markdown(folder_name):
|
||||||
|
"""Bundle markdown files with frontmatter"""
|
||||||
|
data = []
|
||||||
|
folder_path = Path(folder_name)
|
||||||
|
|
||||||
|
if folder_path.exists():
|
||||||
|
for md_file in folder_path.glob("*.md"):
|
||||||
|
with open(md_file) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
meta, content = parse_frontmatter(content)
|
||||||
|
item = {
|
||||||
|
"_id":
|
||||||
|
md_file.stem,
|
||||||
|
"content":
|
||||||
|
content,
|
||||||
|
"last_modified":
|
||||||
|
datetime.fromtimestamp(md_file.stat().st_mtime,
|
||||||
|
tz=timezone.utc).isoformat(),
|
||||||
|
**meta
|
||||||
|
}
|
||||||
|
data.append(item)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
def bundle_folder(folder_name):
|
def bundle_folder(folder_name):
|
||||||
"""Bundle all YML files from a folder into a list"""
|
"""Bundle files based on type"""
|
||||||
data = []
|
if folder_name == "wiki":
|
||||||
folder_path = Path(folder_name)
|
return bundle_markdown(folder_name)
|
||||||
if folder_path.exists():
|
else:
|
||||||
for yml_file in folder_path.glob("*.yml"):
|
data = []
|
||||||
with open(yml_file) as f:
|
folder_path = Path(folder_name)
|
||||||
item = yaml.safe_load(f)
|
if folder_path.exists():
|
||||||
item["_id"] = yml_file.stem
|
for yml_file in folder_path.glob("*.yml"):
|
||||||
data.append(item)
|
with open(yml_file) as f:
|
||||||
return data
|
item = yaml.safe_load(f)
|
||||||
|
item["_id"] = yml_file.stem
|
||||||
|
data.append(item)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
# Create bundles directory
|
# Create bundles directory
|
||||||
@@ -22,15 +74,17 @@ Path("bundles").mkdir(exist_ok=True)
|
|||||||
|
|
||||||
# Define folders to bundle
|
# Define folders to bundle
|
||||||
folders = [
|
folders = [
|
||||||
"custom_formats", "profiles", "regex_patterns", "group_tiers", "dev_logs"
|
"custom_formats", "profiles", "regex_patterns", "group_tiers", "dev_logs",
|
||||||
|
"wiki"
|
||||||
]
|
]
|
||||||
|
|
||||||
# Bundle each folder
|
# Bundle each folder
|
||||||
for folder in folders:
|
for folder in folders:
|
||||||
data = bundle_folder(folder)
|
data = bundle_folder(folder)
|
||||||
bundle_path = f"bundles/{folder}.json"
|
bundle_path = f"bundles/{folder}.json"
|
||||||
|
# Here's where we use the encoder
|
||||||
with open(bundle_path, "w") as f:
|
with open(bundle_path, "w") as f:
|
||||||
json.dump(data, f, indent=2)
|
json.dump(data, f, indent=2, cls=DateTimeEncoder)
|
||||||
|
|
||||||
# Create version file
|
# Create version file
|
||||||
version = {
|
version = {
|
||||||
@@ -38,4 +92,4 @@ version = {
|
|||||||
"folders": folders
|
"folders": folders
|
||||||
}
|
}
|
||||||
with open("bundles/version.json", "w") as f:
|
with open("bundles/version.json", "w") as f:
|
||||||
json.dump(version, f, indent=2)
|
json.dump(version, f, indent=2, cls=DateTimeEncoder)
|
||||||
|
|||||||
Reference in New Issue
Block a user