diff --git a/src/formatitko/formatitko.py b/src/formatitko/formatitko.py index a872fda..9b3b942 100755 --- a/src/formatitko/formatitko.py +++ b/src/formatitko/formatitko.py @@ -2,6 +2,9 @@ import argparse import sys +import tempfile +import subprocess +import shutil # Import local files from .transform import transform @@ -30,6 +33,7 @@ def main(): parser.add_argument("-t", "--output-tex", help="The TEX file to write into.") parser.add_argument("-m", "--output-md", help="The Markdown file to write into. (Uses pandoc to generate markdown)") parser.add_argument("-j", "--output-json", help="The JSON file to dump the pandoc-compatible AST into.") + parser.add_argument("-P", "--output-pdf", help="The PDF file to save the PDF generated by TeX.") parser.add_argument("--katex-server", action='store_true', help="Starts a KaTeX server and prints the socket filename onto stdout. Useful for running formatitko many times without starting the KaTeX server each time.") parser.add_argument("-k", "--katex-socket", help="The KaTeX server socket filename obtained by running with `--katex-server`.") parser.add_argument("input_filename", help="The markdown file to process.", nargs="?" if "--katex-server" in sys.argv else None) @@ -75,6 +79,18 @@ def main(): with open(args.output_json, "w") as file: file.write(convert_text(PandocProcessor().transform(doc), input_format="panflute", output_format="json")) + if args.output_pdf is not None: + if args.output_tex is None: + fd = tempfile.NamedTemporaryFile(dir=".", suffix=".tex") + with open(fd.name, "w") as file: + UCWTexGenerator(file, imageProcessor).generate(doc) + filename = fd.name + else: + filename = args.output_tex + outdir = tempfile.TemporaryDirectory(prefix="formatitko") + subprocess.run(["pdfcsplain", "-halt-on-error", "-output-directory="+outdir.name, "-jobname=formatitko", filename], check=True) + shutil.move(outdir.name+"/formatitko.pdf", args.output_pdf) + if args.debug: print("-----------------------------------") OutputGenerator(sys.stdout).generate(doc)