From 91521588e57db9d1bd61d04080d78587a70eafcd Mon Sep 17 00:00:00 2001 From: Martin Bierbaum Date: Mon, 17 Feb 2020 23:17:59 +0100 Subject: [PATCH] rename helper functions --- README.md | 15 ++++++++------- flask_tex/__init__.py | 8 ++++---- tests/test_tex.py | 12 ++++++------ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6e9ccc2..c9a1776 100644 --- a/README.md +++ b/README.md @@ -39,23 +39,24 @@ can find it. \end{document} ``` -For lower level access to LaTeX, use `flask.run_tex`: +For lower level access to LaTeX, use `flask.compile_source`: ```python -from flask_tex import run_tex +from flask_tex import compile_source -pdf = run_tex(source, command="pdflatex") +pdf = compile_source(source, command="pdflatex") ``` where `source` is a string containing a `tex` document and command is a string defining -the LaTeX command to use for compiling the source. The default `command` is `pdflatex`, but +the LaTeX command to use for compiling the source. The string should contain any optional +flags you would like to pass to the LaTeX executable. The default `command` is `pdflatex`, but you can supply others, e.g. `latexmk -pdf`. `pdf` is a bytes object containing the pdf file. -`run_tex` runs LaTeX inside a temporary directory. Use `flask_tex.compile_tex` +`compile_source` runs LaTeX inside a temporary directory. Use `flask_tex.run_tex` to circumvent this and run LaTeX in a directory of your choice: ```python -from flask_tex import compile_tex +from flask_tex import run_tex -pdf = compile_tex(source, command="pdflatex", directory="/foo/bar/") +pdf = run_tex(source, command="pdflatex", directory="/foo/bar/") ``` diff --git a/flask_tex/__init__.py b/flask_tex/__init__.py index 86520a4..3fb585e 100644 --- a/flask_tex/__init__.py +++ b/flask_tex/__init__.py @@ -7,7 +7,7 @@ def render_to_pdf(template_name, filename="flask.pdf", **kwargs): source = render_template(template_name, **kwargs) - pdf = run_tex(source) + pdf = compile_source(source) response = make_response(pdf) response.headers["Content-Type"] = "application/pdf" response.headers["Content-Disposition"] = f'filename="{filename}"' @@ -18,12 +18,12 @@ class TexError(Exception): pass -def run_tex(source, command="pdflatex"): +def compile_source(source, command="pdflatex"): with tempfile.TemporaryDirectory() as tempdir: - return compile_source(source, command, tempdir) + return run_tex(source, command, tempdir) -def compile_source(source, command, directory): +def run_tex(source, command, directory): filename = "texput.tex" with open(os.path.join(directory, filename), "x", encoding="utf-8") as f: f.write(source) diff --git a/tests/test_tex.py b/tests/test_tex.py index 1d4288b..a70dd64 100644 --- a/tests/test_tex.py +++ b/tests/test_tex.py @@ -3,7 +3,7 @@ from flask import Flask -from flask_tex import run_tex, TexError, render_to_pdf +from flask_tex import compile_source, TexError, render_to_pdf class TestRunTex: @@ -16,28 +16,28 @@ class TestRunTex: ) def test_run_tex(self): - pdf = run_tex(self.source) + pdf = compile_source(self.source) assert pdf is not None def test_run_tex_custom_latex_command(self): command = "lualatex" - pdf = run_tex(self.source, command=command) + pdf = compile_source(self.source, command=command) assert pdf is not None def test_run_latexmk(self): command = "latexmk -pdf" - pdf = run_tex(self.source, command=command) + pdf = compile_source(self.source, command=command) assert pdf is not None def test_run_unkown_command(self): command = "unknown" with pytest.raises(CalledProcessError): - run_tex(self.source, command=command) + compile_source(self.source, command=command) def test_run_incorrect_source(self): source = "\\documentclass{article}\n" "\\begin{document}\n" "This is a test.\n" with pytest.raises(TexError): - run_tex(source) + compile_source(source) class TestApp: