bl2md.py
nogajun
JSONとMarkdownテキストに別れているBlueditのコンテンツをよくあるフロントマター付きのMarkdownテキストに変換するPythonスクリプト。
"""
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()