Skip to content

Instantly share code, notes, and snippets.

@nogajun
Last active March 8, 2025 12:41
Show Gist options
  • Save nogajun/23e8b30e321a1a3c6a81b9b4fc787f96 to your computer and use it in GitHub Desktop.
Save nogajun/23e8b30e321a1a3c6a81b9b4fc787f96 to your computer and use it in GitHub Desktop.
bl2md.py - Convert Bludit CMS data files to Markdown files
"""
bl2md.py - Convert Bludit data files to Markdown files
Synopsis:
bl2md.py <Bludit metadata file> <Bludit markdown directory> <Markdown output directory>
Example:
python bl2md.py databases/pages.php pages/ output/
Description:
This script converts Bludit data files to Markdown files. The script reads a JSON file containing metadata for each Bludit page and a directory containing Bludit markdown files. For each page, the script reads the metadata and adds it as front matter to the corresponding markdown file. The script then writes the modified markdown file to an output directory.
"""
import json
import sys
from pathlib import Path
def load_metadata(json_file):
with open(json_file, "r", encoding="utf-8") as f:
# Skip the first line (PHP code)
f.readline()
# Load the JSON data
data = json.load(f)
return data
def add_front_matter(fname, metadata, md_file, output_file):
with open(md_file, "r", encoding="utf-8") as f:
content = f.read()
front_matter = "---\n"
for key, value in metadata.items():
if (
value == ""
or key == "position"
or key == "md5file"
or key == "uuid"
or key == "allowComments"
or key == "noindex"
or key == "nofollow"
or key == "noarchive"
or key == "custom"
):
continue
else:
if key == "tags":
if isinstance(value, dict):
front_matter += "tags: " + ", ".join(value.keys()) + "\n"
else:
continue
elif key == "type":
front_matter += f"status: {value}\n"
elif key == "dateModified":
front_matter += f"modified: {value}\n"
elif key == "description":
front_matter += f"summary: {value}\n"
elif key == "coverImage":
front_matter += f"cover: {value}\n"
else:
front_matter += f"{key}: {value}\n"
front_matter += f"slug: {fname}\n"
front_matter += "---\n\n"
# Ensure the output directory exists
output_file.parent.mkdir(parents=True, exist_ok=True)
with open(output_file, "w", encoding="utf-8") as f:
f.write(front_matter + content)
def main():
if len(sys.argv) < 4:
print("Usage: bl2md.py <Bludit metadata file> <Bludit markdown directory> <Markdown output directory>")
sys.exit(1)
json_file = Path(sys.argv[1])
md_dir = Path(sys.argv[2])
output_dir = Path(sys.argv[3])
metadata = load_metadata(json_file)
for key in metadata.keys():
md_file = md_dir / key / "index.txt"
output_file = output_dir / f"{key}.md"
add_front_matter(key, metadata[key], md_file, output_file)
print(f"Processed {md_file} -> {output_file}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment