Escape some newlines in question extraction

This commit is contained in:
sebastien committed 2026-09-26 11:50:22 +02:00
1 parent e4e25eca55
commit 4c56b655fe
3 files changed
+42 -16

No files matched your search

+12 -6
View File
@@ -142,7 +142,7 @@ import subprocess
from .configuration import LATEX_AFTER, LATEX_BEFORE
def compile_to_pdf(text, output_pdf_path):
def compile_to_pdf(text, output_pdf_path, *, strict=False):
"""Wraps text in standalone header/footer templates and compiles it to PDF."""
latex_template = f"{LATEX_BEFORE}{text}{LATEX_AFTER}"
@@ -166,6 +166,7 @@ def compile_to_pdf(text, output_pdf_path):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8", errors="replace",
check=False
)
if "minted" in text:
@@ -175,6 +176,7 @@ def compile_to_pdf(text, output_pdf_path):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8", errors="replace",
check=False)
generated_pdf = os.path.join(temp_dir, pdf_filename)
@@ -183,17 +185,21 @@ def compile_to_pdf(text, output_pdf_path):
shutil.move(generated_pdf, output_pdf_path)
if result.returncode != 0:
error_lines = [
line.strip() for line in result.stdout.splitlines()
if line.lstrip().startswith("!")
]
detail = f": {error_lines[0]}" if error_lines else ""
lines = result.stdout.splitlines()
first = next((i for i, line in enumerate(lines) if line.lstrip().startswith("!")), None)
detail = "\n" + "\n".join(lines[first:first + 5]) if first is not None else ""
pdf_status = "a PDF was generated" if pdf_generated else "no PDF was generated"
print(
f"Warning: LaTeX compilation reported errors for {output_pdf_path} "
f"(exit code {result.returncode}; {pdf_status}){detail}"
)
if strict:
raise RuntimeError(f"LaTeX failed for {output_pdf_path}{detail}")
elif not pdf_generated:
print(f"Warning: LaTeX compilation produced no PDF for {output_pdf_path}")
if strict:
raise RuntimeError(f"No PDF generated for {output_pdf_path}")
except Exception as e:
if strict:
raise
print(f"Compilation error for {output_pdf_path}: {e}")