Separate out full_tex_to_svg from tex_to_svg

And only cache to disk the results of full_tex_to_svg.  Otherwise, making edits to the tex_templates would not show up without clearing the cache.
This commit is contained in:
Grant Sanderson 2024-12-12 14:27:23 -06:00
parent 5eac0933df
commit 576ecd2e0e

View file

@ -31,22 +31,13 @@ def get_tex_template_config(template_name: str) -> dict[str, str]:
@lru_cache @lru_cache
def get_tex_config(template: str = "") -> dict[str, str]: def get_tex_config(template: str = "") -> tuple[str, str]:
""" """
Returns a dict which should look something like this: Returns a compiler and preamble to use for rendering LaTeX
{
"template": "default",
"compiler": "latex",
"preamble": "..."
}
""" """
template = template or manim_config.tex.template template = template or manim_config.tex.template
template_config = get_tex_template_config(template) config = get_tex_template_config(template)
return { return config["compiler"], config["preamble"]
"template": template,
"compiler": template_config["compiler"],
"preamble": template_config["preamble"]
}
def get_full_tex(content: str, preamble: str = ""): def get_full_tex(content: str, preamble: str = ""):
@ -60,7 +51,6 @@ def get_full_tex(content: str, preamble: str = ""):
@lru_cache(maxsize=128) @lru_cache(maxsize=128)
@cache_on_disk
def latex_to_svg( def latex_to_svg(
latex: str, latex: str,
template: str = "", template: str = "",
@ -83,14 +73,21 @@ def latex_to_svg(
NotImplementedError: If compiler is not supported NotImplementedError: If compiler is not supported
""" """
if show_message_during_execution: if show_message_during_execution:
max_message_len = 80 message = f"Writing {(short_tex or latex)[:70]}..."
message = f"Writing {short_tex or latex}" else:
if len(message) > max_message_len: message = ""
message = message[:max_message_len - 3] + "..."
print(message, end="\r")
tex_config = get_tex_config(template) compiler, preamble = get_tex_config(template)
compiler = tex_config["compiler"]
preamble = "\n".join([additional_preamble, preamble])
full_tex = get_full_tex(latex, preamble)
return full_tex_to_svg(full_tex, compiler, message)
@cache_on_disk
def full_tex_to_svg(full_tex: str, compiler: str = "latex", message: str = ""):
if message:
print(message, end="\r")
if compiler == "latex": if compiler == "latex":
dvi_ext = ".dvi" dvi_ext = ".dvi"
@ -99,17 +96,13 @@ def latex_to_svg(
else: else:
raise NotImplementedError(f"Compiler '{compiler}' is not implemented") raise NotImplementedError(f"Compiler '{compiler}' is not implemented")
preamble = tex_config["preamble"] + "\n" + additional_preamble
full_tex = get_full_tex(latex, preamble)
# Write intermediate files to a temporary directory # Write intermediate files to a temporary directory
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
base_path = os.path.join(temp_dir, "working") tex_path = Path(temp_dir, "working").with_suffix(".tex")
tex_path = base_path + ".tex" dvi_path = tex_path.with_suffix(dvi_ext)
dvi_path = base_path + dvi_ext
# Write tex file # Write tex file
Path(tex_path).write_text(full_tex) tex_path.write_text(full_tex)
# Run latex compiler # Run latex compiler
process = subprocess.run( process = subprocess.run(
@ -128,10 +121,9 @@ def latex_to_svg(
if process.returncode != 0: if process.returncode != 0:
# Handle error # Handle error
error_str = "" error_str = ""
log_path = base_path + ".log" log_path = tex_path.with_suffix(".log")
if os.path.exists(log_path): if log_path.exists():
with open(log_path, "r", encoding="utf-8") as log_file: content = log_path.read_text()
content = log_file.read()
error_match = re.search(r"(?<=\n! ).*\n.*\n", content) error_match = re.search(r"(?<=\n! ).*\n.*\n", content)
if error_match: if error_match:
error_str = error_match.group() error_str = error_match.group()
@ -152,7 +144,7 @@ def latex_to_svg(
# Return SVG string # Return SVG string
result = process.stdout.decode('utf-8') result = process.stdout.decode('utf-8')
if show_message_during_execution: if message:
print(" " * len(message), end="\r") print(" " * len(message), end="\r")
return result return result