Skip to content

Commit b915ca9

Browse files
committed
Rename build steps to compilation steps.
1 parent 7833c24 commit b915ca9

File tree

9 files changed

+131
-123
lines changed

9 files changed

+131
-123
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ all releases are available on `Anaconda.org
1111
------------------
1212

1313
- :pull:`32` implements a new interface to the compilation process which consists of
14-
composable build steps. (Many thanks to :user:`axtimhaus`!:tada:)
14+
composable compilation steps. (Many thanks to :user:`axtimhaus`!:tada:)
1515

1616

1717
0.1.1 - 2022-02-08

README.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,66 +149,66 @@ decorator.
149149

150150
.. code-block:: python
151151
152-
@pytask.mark.latex(build_steps="latexmk")
152+
@pytask.mark.latex(compilation_steps="latexmk")
153153
def task_compile_latex_document():
154154
...
155155
156-
The ``@pytask.mark.latex`` decorator has a keyword argument called ``build_steps`` which
157-
accepts which accepts strings or list of strings pointing to internally implemented
158-
build steps. Using strings will use the default configuration of this build step. It is
159-
equivalent to the following.
156+
The ``@pytask.mark.latex`` decorator has a keyword argument called ``compilation_steps``
157+
which accepts which accepts strings or list of strings pointing to internally
158+
implemented compilation steps. Using strings will use the default configuration of this
159+
compilation step. It is equivalent to the following.
160160

161161
.. code-block::
162162
163-
from pytask_latex import build_steps
163+
from pytask_latex import compilation_steps
164164
165165
166166
@pytask.mark.latex(
167-
build_steps=build_steps.latexmk(
167+
compilation_steps=compilation_steps.latexmk(
168168
options=("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")
169169
)
170170
)
171171
def task_compile_latex_document():
172172
...
173173
174-
In this example, ``build_steps.latexmk`` is a build step constructor which accepts a set
175-
of options and creates a build step function.
174+
In this example, ``compilation_steps.latexmk`` is a compilation step constructor which
175+
accepts a set of options and creates a compilation step function.
176176

177177
You can pass different options to change the compilation process with latexmk. Here is
178178
an example for generating a ``.dvi``.
179179

180180
.. code-block:: python
181181
182182
@pytask.mark.latex(
183-
build_steps=build_steps.latexmk(
183+
compilation_steps=compilation_steps.latexmk(
184184
options=("--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd")
185185
)
186186
)
187187
def task_compile_latex_document():
188188
...
189189
190-
``build_step.latexmk(options)`` generates a build step which is a function with the
191-
following signature:
190+
``compilation_step.latexmk(options)`` generates a compilation step which is a function
191+
with the following signature:
192192

193193
.. code-block::
194194
195195
from pathlib import Path
196196
import subprocess
197197
198198
199-
def custom_build_step(path_to_tex: Path, path_to_document: Path) -> None:
199+
def custom_compilation_step(path_to_tex: Path, path_to_document: Path) -> None:
200200
...
201201
subproces.run(..., check=True)
202202
203-
You can also pass your custom build step with the same signature to the ``build_steps``
204-
keyword argument of the decorator.
203+
You can also pass your custom compilation step with the same signature to the
204+
``compilation_steps`` keyword argument of the decorator.
205205

206-
Each build step receives the path to the LaTeX source file and the path to the final
207-
document which it uses to call some program on the command line to run another step in
208-
the compilation process.
206+
Each compilation step receives the path to the LaTeX source file and the path to the
207+
final document which it uses to call some program on the command line to run another
208+
step in the compilation process.
209209

210-
In the future, pytask-latex will provide more build steps for compiling bibliographies,
211-
glossaries and the like.
210+
In the future, pytask-latex will provide more compilation steps for compiling
211+
bibliographies, glossaries and the like.
212212

213213

214214
Parametrization
@@ -233,7 +233,7 @@ The following task compiles two latex documents.
233233
If you want to compile the same document with different command line options, you have
234234
to include the latex decorator in the parametrization just like with
235235
``@pytask.mark.depends_on`` and ``@pytask.mark.produces``. Pass a dictionary for
236-
possible build steps and their options.
236+
possible compilation steps and their options.
237237

238238
.. code-block:: python
239239
@@ -244,15 +244,15 @@ possible build steps and their options.
244244
(
245245
"document.pdf",
246246
{
247-
"build_steps": build_steps.latexmk(
247+
"compilation_steps": compilation_steps.latexmk(
248248
("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")
249249
)
250250
},
251251
),
252252
(
253253
"document.dvi",
254254
{
255-
"build_steps": build_steps.latexmk(
255+
"compilation_steps": compilation_steps.latexmk(
256256
("--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd")
257257
)
258258
},

src/pytask_latex/build_steps.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/pytask_latex/collect.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from _pytask.nodes import FilePathNode
2020
from _pytask.nodes import PythonFunctionTask
2121
from _pytask.parametrize import _copy_func
22-
from pytask_latex import build_steps as bs
22+
from pytask_latex import compilation_steps as cs
2323
from pytask_latex.utils import to_list
2424

2525

@@ -33,9 +33,9 @@ def task_func():
3333
3434
to
3535
36-
from pytask_latex import build_steps
36+
from pytask_latex import compilation_steps
3737
38-
@pytask.mark.latex(build_steps.latexmk(options))
38+
@pytask.mark.latex(compilation_steps.latexmk(options))
3939
def task_func():
4040
...
4141
@@ -45,48 +45,50 @@ def task_func():
4545
def latex(
4646
options: str | Iterable[str] | None = None,
4747
*,
48-
build_steps: str | Callable[..., Any] | Sequence[str | Callable[..., Any]] = None,
48+
compilation_steps: str
49+
| Callable[..., Any]
50+
| Sequence[str | Callable[..., Any]] = None,
4951
):
5052
"""Specify command line options for latexmk.
5153
5254
Parameters
5355
----------
5456
options
5557
One or multiple command line options passed to latexmk.
56-
build_steps
57-
Build steps to compile the document.
58+
compilation_steps
59+
Compilation steps to compile the document.
5860
5961
"""
60-
build_steps = ["latexmk"] if build_steps is None else build_steps
62+
compilation_steps = ["latexmk"] if compilation_steps is None else compilation_steps
6163

6264
if options is not None:
6365
warnings.warn(_DEPRECATION_WARNING, DeprecationWarning)
64-
out = [bs.latexmk(options)]
66+
out = [cs.latexmk(options)]
6567

6668
else:
6769
out = []
68-
for step in to_list(build_steps):
70+
for step in to_list(compilation_steps):
6971
if isinstance(step, str):
70-
parsed_step = getattr(bs, step)
72+
parsed_step = getattr(cs, step)
7173
if parsed_step is None:
72-
raise ValueError(f"Build step {step!r} is unknown.")
74+
raise ValueError(f"Compilation step {step!r} is unknown.")
7375
out.append(parsed_step())
7476
elif callable(step):
7577
out.append(step)
7678
else:
77-
raise ValueError(f"Build step {step!r} is not a valid step.")
79+
raise ValueError(f"Compilation step {step!r} is not a valid step.")
7880

7981
return out
8082

8183

82-
def compile_latex_document(build_steps, path_to_tex, path_to_document):
84+
def compile_latex_document(compilation_steps, path_to_tex, path_to_document):
8385
"""Replaces the dummy function provided by the user."""
8486

85-
for step in build_steps:
87+
for step in compilation_steps:
8688
try:
8789
step(path_to_tex=path_to_tex, path_to_document=path_to_document)
8890
except CalledProcessError as e:
89-
raise RuntimeError(f"Build step {step.__name__} failed.") from e
91+
raise RuntimeError(f"Compilation step {step.__name__} failed.") from e
9092

9193

9294
@hookimpl
@@ -136,8 +138,10 @@ def pytask_collect_task_teardown(session, task):
136138

137139
merged_mark = _merge_all_markers(task)
138140
steps = latex(*merged_mark.args, **merged_mark.kwargs)
139-
args = get_build_step_args(session, task)
140-
task_function = functools.partial(task_function, build_steps=steps, **args)
141+
args = get_compilation_step_args(session, task)
142+
task_function = functools.partial(
143+
task_function, compilation_steps=steps, **args
144+
)
141145

142146
task.function = task_function
143147

@@ -212,8 +216,8 @@ def _merge_all_markers(task):
212216
return mark
213217

214218

215-
def get_build_step_args(session, task):
216-
"""Prepare arguments passe to each build step."""
219+
def get_compilation_step_args(session, task):
220+
"""Prepare arguments passe to each compilation step."""
217221
latex_document = _get_node_from_dictionary(
218222
task.depends_on, session.config["latex_source_key"]
219223
).value

src/pytask_latex/compilation_steps.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""This module contains compilation steps for compiling a LaTeX document.
2+
3+
Each compilation step must have the following signature:
4+
5+
.. code-block::
6+
7+
def compilation_step(path_to_tex: Path, path_to_document: Path):
8+
...
9+
10+
A compilation step constructor must yield a function with this signature.
11+
12+
"""
13+
from __future__ import annotations
14+
15+
import subprocess
16+
17+
from pytask_latex.path import relative_to
18+
from pytask_latex.utils import to_list
19+
20+
21+
def latexmk(options=("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")):
22+
"""Compilation step that calls latexmk."""
23+
options = [str(i) for i in to_list(options)]
24+
25+
def run_latexmk(path_to_tex, path_to_document):
26+
job_name_opt = [f"--jobname={path_to_document.stem}"]
27+
out_dir_opt = [
28+
f"--output-directory={relative_to(path_to_tex, path_to_document.parent)}"
29+
]
30+
cmd = (
31+
["latexmk", *options]
32+
+ job_name_opt
33+
+ out_dir_opt
34+
+ [path_to_tex.as_posix()]
35+
)
36+
subprocess.run(cmd, check=True)
37+
38+
return run_latexmk

src/pytask_latex/path.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""This module contains functions related to handling paths."""
2+
from __future__ import annotations
3+
4+
import os
5+
from pathlib import Path
6+
7+
8+
def relative_to(path: Path, relative_to_: Path) -> str:
9+
"""Create a relative path from one path to another as a string.
10+
11+
The output folder needs to be declared as a relative path to the directory where
12+
the latex source lies.
13+
14+
1. It must be relative because bibtex / biber, which is necessary for
15+
bibliographies, does not accept full paths as a safety measure.
16+
2. Due to the ``--cd`` flag, latexmk will change the directory to the one where the
17+
source files are. Thus, relative to the latex sources.
18+
19+
See this `discussion on Github
20+
<https://github.com/James-Yu/LaTeX-Workshop/issues/1932#issuecomment-582416434>`_
21+
for additional information.
22+
23+
Example
24+
-------
25+
>>> import sys
26+
>>> from pathlib import Path
27+
>>> if sys.platform == "win32":
28+
... p = Path("C:/Users/user/documents/file.tex")
29+
... else:
30+
... p = Path("/home/user/documents/file.tex")
31+
>>> out = p.parents[2].joinpath("bld", "docs")
32+
>>> relative_to(p, out)
33+
'../../bld/docs'
34+
35+
"""
36+
return Path(os.path.relpath(relative_to_, path.parent)).as_posix()

0 commit comments

Comments
 (0)