#!/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 _get_input_files(format): # read filenames from file if such a file exists list_file = Path(config_dir) / f"source-files.{format}.txt" if os.path.isfile(list_file): with open(list_file, "r") as file: return [line.strip() for line in file] # use all files otherwise else: input_paths = sorted(Path(content_dir).glob('**/*.md')) return [str(p) for p in input_paths] def _run_command(command): # run shell command stdout = subprocess.check_output(command) print(stdout) def clean(args): pass def epub(args): # define pandoc defaults file pandoc_defaults_file = f"{config_dir}/pandoc-defaults.epub.yaml" # define executable and options executable = "/usr/bin/pandoc" executable_opts = ["-d", pandoc_defaults_file] executable_opts.append("--verbose") # get input files input_files = _get_input_files("epub") # put together command and run it command = [executable] + executable_opts + args + input_files print(f"command is {command}") _run_command(command) def cover(args): # define executable and options executable = "/usr/bin/pdflatex" executable_opts = [f"-output-directory={output_dir}"] # get input files 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 input_files = _get_input_files("pdf") # 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] == "clean": clean(args[1:]) elif args[0] == "pdf": pdf(args[1:]) elif args[0] == "cover": cover(args[1:]) elif args[0] == "epub": epub(args[1:]) elif args[0] == "all": pdf(args[1:]) cover(args[1:]) epub(args[1:]) else: print("Parameter not recognized") exit(2)