Skip to content

Commit c3dc3c9

Browse files
committed
Relax image checking (implied by global regex on filenames)
1 parent 608fe13 commit c3dc3c9

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

problemtools/md2html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def convert(problem: str, options: argparse.Namespace) -> bool:
3131
raise Exception(f"Error! {statement_path} is not a file")
3232

3333

34-
statement_common.check_images_are_valid(statement_path)
34+
statement_common.assert_images_are_valid_md(statement_path)
3535
statement_common.foreach_image(statement_path,
3636
lambda img_name: copy_image(problem, img_name))
3737

problemtools/problem2pdf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def md2pdf(options: argparse.Namespace) -> bool:
2929
if not os.path.isfile(statement_path):
3030
raise Exception(f"Error! {statement_path} is not a file")
3131

32-
statement_common.check_images_are_valid(statement_path)
32+
statement_common.assert_images_are_valid_md(statement_path)
3333

3434
templatepaths = [os.path.join(os.path.dirname(__file__), 'templates/markdown_pdf'),
3535
os.path.join(os.path.dirname(__file__), '../templates/markdown_pdf'),

problemtools/statement_common.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import re
77
import json
8+
from pathlib import Path
89

910
from . import verifyproblem
1011

@@ -69,20 +70,20 @@ def foreach_image(statement_path, callback):
6970

7071

7172
def assert_image_is_valid(problem_root: str, img_src: str) -> None:
72-
# Check that the source is a legal image source
73-
src_pattern = r'^[a-zA-Z0-9._]+\.(png|jpg|jpeg)$'
74-
75-
if not re.match(src_pattern, img_src):
76-
raise Exception(r"Image source must match regex ^[a-zA-Z0-9._]+\.(png|jpg|jpeg)$")
73+
# Check that the image exists and uses an allowed extension
74+
extension = Path(img_src).suffix
75+
if extension not in (".png", ".jpg", ".jpeg", ".svg"):
76+
raise Exception(f"Unsupported image extension {extension} for image {img_src}")
7777

7878
source_name = os.path.join(problem_root, img_src)
79-
8079
if not os.path.isfile(source_name):
8180
print(source_name)
8281
raise Exception(f"File {img_src} not found in problem_statement")
8382

8483

85-
def check_images_are_valid(statement_path: str) -> None:
84+
def assert_images_are_valid_md(statement_path: str) -> None:
85+
# Find all images in the statement and assert that they exist and
86+
# use valid image extensions
8687
problem_root = os.path.dirname(statement_path)
8788
foreach_image(statement_path,
8889
lambda img_name: assert_image_is_valid(problem_root, img_name))
@@ -129,14 +130,9 @@ def inject_samples(html, samples, sample_separator):
129130
# (And also properly throw an error if {{nextsample}} is called with no samples left)
130131
html = html[:match.start()] + to_inject + html[match.end():]
131132

132-
#print(html)
133133
return html, samples
134134

135135

136-
def append_samples(html, samples):
137-
"""Appends all samples to the end of the html"""
138-
139-
140136
def format_samples(problem_root: str, to_pdf: bool = False) -> List[str]:
141137
"""Read all samples from the problem directory and convert them to pandoc-valid markdown
142138

0 commit comments

Comments
 (0)