meta/tools/build
2025-09-26 12:00:23 +02:00

129 lines
2.7 KiB
Python
Executable file

#!/usr/bin/env python3
import sys as _sys
import os as _os
import argparse as _argparse
def string_coin(
template,
arguments
):
result = template
for (key, value, ) in arguments.items():
result = result.replace("{{%s}}" % key, value)
return result
def file_read(path):
handle = open(path, "r")
content = handle.read()
handle.close()
return content
def file_write(path, content):
handle = open(path, "w")
handle.write(content)
handle.close()
def main():
## consts
conf = {
"title": "Espe",
"source_directory": "source"
}
## args
argument_parser = _argparse.ArgumentParser()
argument_parser.add_argument(
"-t",
"--target-directory",
type = str,
default = "build",
)
argument_parser.add_argument(
"-f",
"--format",
type = str,
action = "append",
choices = ["markdown", "html", "pdf"],
default = [],
)
args = argument_parser.parse_args()
## vars
target_directory = _os.path.abspath(args.target_directory)
formats = (["html"] if (len(args.format) <= 0) else args.format)
## exec
_os.makedirs(args.target_directory, exist_ok = True)
for format_ in formats:
if (format_ == "markdown"):
target_path = string_coin(
"{{target_directory}}/espe.md",
{
"target_directory": target_directory,
}
)
file_write(
target_path,
string_coin(
"# {{title}}\n\n{{content}}",
{
"title": conf["title"],
"content": file_read(
string_coin(
"{{source_directory}}/notizen.md",
{
"source_directory": conf["source_directory"],
}
)
),
}
)
)
_sys.stdout.write("%s\n" % target_path)
elif (format_ == "html"):
target_path = string_coin(
"{{target_directory}}/espe.html",
{
"target_directory": target_directory,
}
)
_os.system(
string_coin(
"pandoc {{source_directory}}/notizen.md --to=html --title={{title}} --metadata='title:{{title}}' --output={{target_path}}",
{
"source_directory": conf["source_directory"],
"target_path": target_path,
"title": conf["title"],
}
)
)
_sys.stdout.write("%s\n" % target_path)
elif (format_ == "pdf"):
target_path = string_coin(
"{{target_directory}}/espe.pdf",
{
"target_directory": target_directory,
}
)
_os.system(
string_coin(
"pandoc {{source_directory}}/notizen.md --to=pdf --title={{title}} --metadata='title:{{title}}' --output={{target_path}}",
{
"source_directory": conf["source_directory"],
"target_path": target_path,
"title": conf["title"],
}
)
)
_sys.stdout.write("%s\n" % target_path)
else:
raise ValueError("invalid format: %s" % format_)
main()