#!/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" pandoc = "/usr/bin/pandoc" typst = "/snap/bin/typst" pdflatex = "/usr/bin/pdflatex" def _get_input_files(format: str) -> list: # 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(executable: str, ex_opts: list=[], args: list=[], infiles: list=[], outfile: list=[]) -> None: command = [executable] + ex_opts + args + infiles + outfile print(f"command is {command}") # run shell command stdout = subprocess.check_output(command) print(stdout) def _clean_dir(path: Path) -> None: for file in path.iterdir(): # if file: delete if file.is_file(): file.unlink() # if dir: else: # clean dir recursively _clean_dir(file) # delete dir unless it's the output_dir if file != Path(output_dir): file.rmdir() def minizine(args: list=[]) -> None: # define executable and options executable = typst executable_opts = ["compile", "--font-path", "static/font/"] # executable_opts.append("--verbose") # get input files input_files = [str(Path(f"{base_dir}/promo/minizine/minizine.typ"))] output_file = [str(Path(f"{output_dir}/minizine.pdf"))] # put together command and run it _run_command(executable, ex_opts=executable_opts, args=args, infiles=input_files, outfile=output_file) def clean(args: list=[]) -> None: _clean_dir(Path(output_dir)) def epub(args: list=[]) -> None: # define pandoc defaults file pandoc_defaults_file = f"{config_dir}/pandoc-defaults.epub.yaml" # define executable and options executable = 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 _run_command(executable, ex_opts=executable_opts, args=args, infiles=input_files) # validate epub _run_command("/usr/bin/epubcheck", infiles=[f"{output_dir}/papa-lach-doch-mal.epub"]) def cover(args: list=[]) -> None: # define executable and options executable = pdflatex executable_opts = [f"-output-directory={output_dir}"] ## Print cover input_files = [f"{base_dir}/cover/cover.print.tex"] _run_command(executable, ex_opts=executable_opts, args=args, infiles=input_files) ## EPUB cover # define executable and options executable = typst executable_opts = ["compile", "--font-path", "static/font"] # get input files input_files = [f"{base_dir}/cover/cover.epub.typ"] output_file = [f"{output_dir}/cover.epub.pdf"] # put together command and run it _run_command(executable, ex_opts=executable_opts, args=args, infiles=input_files, outfile=output_file) def _print(args: list=[]) -> None: # define pandoc defaults file pandoc_defaults_file = f"{config_dir}/pandoc-defaults.print.yaml" # define executable and options executable = pandoc executable_opts = ["-d", pandoc_defaults_file] #executable_opts.append("--verbose") # get input files input_files = _get_input_files("print") # put together command and run it _run_command(executable, ex_opts=executable_opts, args=args, infiles=input_files) 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] == "print": _print(args[1:]) elif args[0] == "cover": cover(args[1:]) elif args[0] == "epub": epub(args[1:]) elif args[0] == "all": _print(args[1:]) cover(args[1:]) epub(args[1:]) elif args[0] == "minizine": minizine(args[1:]) else: print("Parameter not recognized") exit(2)