144 lines
3.6 KiB
Python
Executable File
144 lines
3.6 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 _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(command: list) -> None:
|
|
# 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 = "/snap/bin/typst"
|
|
executable_opts = ["compile", "--font-path", "static/font/"]
|
|
# executable_opts.append("--verbose")
|
|
|
|
# get input files
|
|
input_files = [str(Path(f"{base_dir}/promo/minizine/skorbut.typ"))]
|
|
output_file = [str(Path(f"{output_dir}/skorbut.pdf"))]
|
|
|
|
# put together command and run it
|
|
command = [executable] + executable_opts + args + input_files + output_file
|
|
print(f"command is {command}")
|
|
_run_command(command)
|
|
|
|
|
|
|
|
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 = "/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: list=[]) -> None:
|
|
# 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: list=[]) -> None:
|
|
# 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:])
|
|
elif args[0] == "minizine":
|
|
minizine(args[1:])
|
|
else:
|
|
print("Parameter not recognized")
|
|
exit(2)
|
|
|
|
|
|
|