Skip to content

Commit

Permalink
Merge pull request #21 from Krishn1412/check_solution_failure
Browse files Browse the repository at this point in the history
  • Loading branch information
lgabs authored Nov 27, 2024
2 parents caf2c84 + 49bac69 commit 37c03a3
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 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,12 +83,28 @@ 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:
content = sol_file.read_text(encoding="utf-8")
if not validate_latex_content(content):
sol_files_with_issues.append(str(sol_file))
continue
doc.append(NoEscape(content))
doc.append(NoEscape(r"\newpage"))

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."
)

# Generate PDF in the exam directory
doc.generate_pdf(str(exam_path / "solutions/solutions_compiled"), clean_tex=True)

0 comments on commit 37c03a3

Please sign in to comment.