|
5 | 5 | import subprocess
|
6 | 6 | import re
|
7 | 7 | import json
|
| 8 | +from pathlib import Path |
8 | 9 |
|
9 | 10 | from . import verifyproblem
|
10 | 11 |
|
@@ -69,20 +70,20 @@ def foreach_image(statement_path, callback):
|
69 | 70 |
|
70 | 71 |
|
71 | 72 | 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}") |
77 | 77 |
|
78 | 78 | source_name = os.path.join(problem_root, img_src)
|
79 |
| - |
80 | 79 | if not os.path.isfile(source_name):
|
81 | 80 | print(source_name)
|
82 | 81 | raise Exception(f"File {img_src} not found in problem_statement")
|
83 | 82 |
|
84 | 83 |
|
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 |
86 | 87 | problem_root = os.path.dirname(statement_path)
|
87 | 88 | foreach_image(statement_path,
|
88 | 89 | lambda img_name: assert_image_is_valid(problem_root, img_name))
|
@@ -129,14 +130,9 @@ def inject_samples(html, samples, sample_separator):
|
129 | 130 | # (And also properly throw an error if {{nextsample}} is called with no samples left)
|
130 | 131 | html = html[:match.start()] + to_inject + html[match.end():]
|
131 | 132 |
|
132 |
| - #print(html) |
133 | 133 | return html, samples
|
134 | 134 |
|
135 | 135 |
|
136 |
| -def append_samples(html, samples): |
137 |
| - """Appends all samples to the end of the html""" |
138 |
| - |
139 |
| - |
140 | 136 | def format_samples(problem_root: str, to_pdf: bool = False) -> List[str]:
|
141 | 137 | """Read all samples from the problem directory and convert them to pandoc-valid markdown
|
142 | 138 |
|
|
0 commit comments