Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check syntax of each solution #21

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/gpt_resolve/pdf_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
from pylatex import Document, Command
from pylatex.utils import NoEscape
import datetime
import tempfile
import subprocess

def validate_latex_content(content: str) -> bool:
"""
Validate a LaTeX document by attempting to compile it, checking for syntax validity.

Args:
content (str): The LaTeX content to check.

Returns:
bool: True if compilation is successful, False otherwise.
"""
with tempfile.TemporaryDirectory() as temp_dir:
tex_file = Path(temp_dir) / "temp_solution"
doc = Document()
doc.append(NoEscape(content))
try:
# Attempt to compile the LaTeX document to PDF
doc.generate_pdf(str(tex_file), clean_tex=True)
return True
except Exception as e:
print(f"Error compiling LaTeX content: {e}")
return False


def generate_solutions_pdf(
Expand Down Expand Up @@ -59,10 +83,22 @@ def generate_solutions_pdf(
solutions_dir.glob("*_solution.txt"),
key=lambda x: int(x.stem.split("_")[0][1:]),
)
# Get all solution files sorted
solution_files = sorted(
solutions_dir.glob("*_solution.txt"),
key=lambda x: int(x.stem.split("_")[0][1:])
)

sol_files_with_issues = []
# Add each solution to document
for sol_file in solution_files:
print(f"Validating solution: {sol_file.name}")
content = sol_file.read_text(encoding="utf-8")

if not validate_latex_content(content):
print(f"Error: Solution '{sol_file.name}' has LaTeX syntax errors. Please fix it.")
sol_files_with_issues.append(sol_file)
continue
Copy link
Owner

@lgabs lgabs Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! The only thing left here is that any errors will count in sol_files_with_issues and loop will continue, but in the end the code will try to compile the whole pdf again, generating the same error that currently happens (for many questions, the traceback will be big).

So if you print errors only outside the loop in case of sol_files_with_issues not empty, it'd be solved:

for sol_file in solution_files:
    ...
    if not validate_latex_content(content):
        sol_files_with_issues.append(sol_file)
        continue

if sol_files_with_issues:
    raise ValueError("Errors in one or more solutions:\n' + '\n'.join(sol_files_with_issues) + '\n\nPlease check for syntax errors.")
    
...

doc.append(NoEscape(content))
doc.append(NoEscape(r"\newpage"))

Expand Down