bookbuild/build.py

135 lines
3.7 KiB
Python
Executable File

#!/usr/bin/python
import os
from pathlib import Path
import subprocess
import sys
# define directories
base_dir = os.getcwd()
content_dir = f"{base_dir}/content"
config_dir = f"{base_dir}/config"
output_dir = f"{base_dir}/output"
def run_command(command):
# run shell command
stdout = subprocess.check_output(command)
print(stdout)
def epub(args):
# take specific content file as input
input_paths = [Path(content_dir) / "01 - frontmatter" / "30 - widmung.md"]
input_paths += sorted(Path(content_dir).glob('02 - mainmatter/*.md'))
input_paths += sorted(Path(content_dir).glob('03 - backmatter/*.md'))
input_files = [str(p) for p in input_paths]
print("\n".join(input_files))
####################################
# UNTESTED CODE FROM HERE ON DOWN #
####################################
# BUILD COMMAND FOR PANDOC-IFICATION MARKDOWN -> TEX
cmd_opts = []
# source format
cmd_opts.append("--from=markdown")
# target format
cmd_opts.append("--to=latex")
# output file
cmd_opts.append(f"--output={output_dir}/{output_filestub}.epub.tex")
# create standalone document
#cmd_opts.append("--standalone")
# header include
cmd_opts.append("--include-in-header=content/00 - header-includes.tex")
# metadata file location
cmd_opts.append("--metadata-file=metadata.yaml")
command = [pandoc] + pandoc_opts + cmd_opts + input_files + args
print(f"command is {command}")
# run command
stdout = subprocess.check_output(command)
print(stdout)
# BUILD COMMAND FOR PANDOC-IFICATION TEX -> EPUB
cmd_opts = []
# source format
cmd_opts.append("--from=latex")
# target format
cmd_opts.append("--to=epub")
# output file
cmd_opts.append(f"--output={output_dir}/{output_filestub}.epub")
# create standalone document
cmd_opts.append("--standalone")
# metadata file location
cmd_opts.append("--metadata-file=metadata_epub.yaml")
# CSS file location
cmd_opts.append("--css=epub/epub.css")
# embed fonts
cmd_opts.append("--epub-embed-font=epub/LibertinusSans-Regular.ttf")
cmd_opts.append("--epub-embed-font=epub/LibertinusSans-Regular.ttf")
cmd_opts.append("--epub-embed-font=epub/LibertinusSans-Regular.ttf")
command = [pandoc] + pandoc_opts + cmd_opts + args + [f"{output_dir}/{output_filestub}.epub.tex"]
print(f"command is {command}")
# run command
stdout = subprocess.check_output(command)
print(stdout)
def cover(args):
# define executable and options
executable = "/usr/bin/pdflatex"
executable_opts = [f"-output-directory={output_dir}"]
# get input files from content dir
cover_dir = f"{base_dir}/cover"
input_files = [f"{cover_dir}/cover.tex"]
# put together command and run it
command = [executable] + executable_opts + args + input_files
print(f"command is {command}")
run_command(command)
def pdf(args):
# define pandoc defaults file
pandoc_defaults_file = f"{config_dir}/pandoc-defaults.pdf.yaml"
# define executable and options
executable = "/usr/bin/pandoc"
executable_opts = ["-d", pandoc_defaults_file]
#executable_opts.append("--verbose")
# get input files from content dir
input_paths = sorted(Path(content_dir).glob('**/*.md'))
input_files = [str(p) for p in input_paths]
# put together command and run it
command = [executable] + executable_opts + args + input_files
print(f"command is {command}")
run_command(command)
if __name__ == "__main__":
# get command line parameters
args = sys.argv[1:]
if len(args) == 0:
print("Please specify at least one parameter")
exit(1)
# call function corresponding to first argument
if args[0] == "pdf":
pdf(args[1:])
elif args[0] == "cover":
cover(args[1:])
elif args[0] == "epub":
epub(args[1:])
else:
print("Parameter not recognized")
exit(2)