allow to use local latex installation

This commit is contained in:
Sina Atalay 2024-02-20 20:12:48 +01:00
parent cce9e2db82
commit 0821ab9a94
2 changed files with 48 additions and 17 deletions

View File

@ -405,6 +405,11 @@ def cli_command_render(
str, str,
typer.Argument(help="Name of the YAML input file"), typer.Argument(help="Name of the YAML input file"),
], ],
use_local_latex: bool = typer.Option(
False,
"--use-local-latex",
help="Use the local LaTeX installation instead of the RenderCV's TinyTeX.",
),
): ):
"""Generate a LaTeX CV from a YAML input file. """Generate a LaTeX CV from a YAML input file.
@ -433,7 +438,7 @@ def cli_command_render(
progress.finish_the_current_step() progress.finish_the_current_step()
progress.start_a_step("Rendering the LaTeX file to a PDF") progress.start_a_step("Rendering the LaTeX file to a PDF")
r.latex_to_pdf(latex_file_path) r.latex_to_pdf(latex_file_path, use_local_latex)
progress.finish_the_current_step() progress.finish_the_current_step()
progress.start_a_step("Rendering the Markdown file to a HTML (for Grammarly)") progress.start_a_step("Rendering the Markdown file to a HTML (for Grammarly)")

View File

@ -497,7 +497,7 @@ def transform_markdown_sections_to_latex_sections(
def replace_placeholders_with_actual_values( def replace_placeholders_with_actual_values(
string: str, placeholders: dict[str, str] string: str, placeholders: dict[str, Optional[str]]
) -> str: ) -> str:
"""Replace the placeholders in a string with actual values. """Replace the placeholders in a string with actual values.
@ -510,7 +510,7 @@ def replace_placeholders_with_actual_values(
str: The string with actual values. str: The string with actual values.
""" """
for placeholder, value in placeholders.items(): for placeholder, value in placeholders.items():
string = string.replace(placeholder, value) string = string.replace(placeholder, str(value))
return string return string
@ -643,7 +643,7 @@ def make_matched_part_non_line_breakable(
return make_matched_part_something(value, "mbox", match_str) return make_matched_part_something(value, "mbox", match_str)
def abbreviate_name(name: str) -> str: def abbreviate_name(name: Optional[str]) -> str:
"""Abbreviate a name by keeping the first letters of the first names. """Abbreviate a name by keeping the first letters of the first names.
This function can be used as a Jinja2 filter in templates. This function can be used as a Jinja2 filter in templates.
@ -662,6 +662,9 @@ def abbreviate_name(name: str) -> str:
Returns: Returns:
str: The abbreviated name. str: The abbreviated name.
""" """
if name is None:
return ""
number_of_words = len(name.split(" ")) number_of_words = len(name.split(" "))
if number_of_words == 1: if number_of_words == 1:
@ -803,6 +806,9 @@ def generate_latex_file(
jinja2_environment, jinja2_environment,
) )
if rendercv_data_model.cv.name is None:
latex_file_name = "CV.tex"
else:
latex_file_name = f"{rendercv_data_model.cv.name.replace(' ', '_')}_CV.tex" latex_file_name = f"{rendercv_data_model.cv.name.replace(' ', '_')}_CV.tex"
latex_file_path = output_directory / latex_file_name latex_file_path = output_directory / latex_file_name
latex_file_object.generate_latex_file(latex_file_path) latex_file_object.generate_latex_file(latex_file_path)
@ -888,7 +894,9 @@ def generate_latex_file_and_copy_theme_files(
return latex_file_path return latex_file_path
def latex_to_pdf(latex_file_path: pathlib.Path) -> pathlib.Path: def latex_to_pdf(
latex_file_path: pathlib.Path, use_local_latex: bool = False
) -> pathlib.Path:
"""Run TinyTeX with the given $\\LaTeX$ file to generate the PDF. """Run TinyTeX with the given $\\LaTeX$ file to generate the PDF.
Args: Args:
@ -900,6 +908,23 @@ def latex_to_pdf(latex_file_path: pathlib.Path) -> pathlib.Path:
if not latex_file_path.is_file(): if not latex_file_path.is_file():
raise FileNotFoundError(f"The file {latex_file_path} doesn't exist!") raise FileNotFoundError(f"The file {latex_file_path} doesn't exist!")
if use_local_latex:
executable = "pdflatex"
# check if pdflatex is installed:
try:
subprocess.run(
[executable, "--version"],
stdout=subprocess.DEVNULL, # don't capture the output
stderr=subprocess.DEVNULL, # don't capture the error
)
except FileNotFoundError:
raise FileNotFoundError(
"[blue]pdflatex[/blue] isn't installed! Please install LaTeX and try"
" again (or don't use the"
" [bright_black]--use_local_latex[/bright_black] option)."
)
else:
tinytex_binaries_directory = ( tinytex_binaries_directory = (
pathlib.Path(__file__).parent / "tinytex-release" / "TinyTeX" / "bin" pathlib.Path(__file__).parent / "tinytex-release" / "TinyTeX" / "bin"
) )
@ -913,9 +938,10 @@ def latex_to_pdf(latex_file_path: pathlib.Path) -> pathlib.Path:
if sys.platform not in executables: if sys.platform not in executables:
raise OSError(f"TinyTeX doesn't support the platform {sys.platform}!") raise OSError(f"TinyTeX doesn't support the platform {sys.platform}!")
executable = executables[sys.platform]
# Run TinyTeX: # Run TinyTeX:
command = [ command = [
executables[sys.platform], executable,
str(latex_file_path.absolute()), str(latex_file_path.absolute()),
] ]
with subprocess.Popen( with subprocess.Popen(