#!/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 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") # take specific content file as input input_paths = [Path(content_dir) / "01 - frontmatter" / "30 - widmung.md", Path(content_dir) / "01 - frontmatter" / "50 - vorwort.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] # 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 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_paths = sorted(Path(content_dir).glob('03 - backmatter/*.md')) #DEBUG 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] == "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)