Skip to content

Commit

Permalink
rename helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
weinbusch committed Feb 17, 2020
1 parent f937f99 commit 9152158
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
```
8 changes: 4 additions & 4 deletions flask_tex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"'
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 9152158

Please sign in to comment.