Skip to content

Commit 31b52fb

Browse files
committed
Add regression text and fix for image dimension handling.
1 parent b223a55 commit 31b52fb

File tree

3 files changed

+20
-39
lines changed

3 files changed

+20
-39
lines changed

contentcuration/contentcuration/tests/utils/test_exercise_creation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,15 @@ def _test_image_resizing_in_field(self, field_type):
984984
"Different sized images should have different file sizes",
985985
)
986986

987+
# Verify that the dimensions have been stripped from the markdown
988+
for file_name in unique_image_files:
989+
# Because we can't predict the set ordering, just confirm that
990+
# neither dimension descriptor is applied.
991+
first_file = f"{file_name} =200x150"
992+
self.assertNotIn(first_file, content)
993+
second_file = f"{file_name} =100x75"
994+
self.assertNotIn(second_file, content)
995+
987996
def test_image_resizing_in_question(self):
988997
"""Test image resizing functionality in question content"""
989998
self._test_image_resizing_in_field("question")

contentcuration/contentcuration/utils/assessment/base.py

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from contentcuration import models
1919

2020

21-
image_pattern = rf"!\[(?:[^\]]*)]\(\${exercises.CONTENT_STORAGE_PLACEHOLDER}/([^\s)]+)(?:\s=([0-9\.]+)x([0-9\.]+))*[^)]*\)"
21+
image_pattern = rf"!\[([^\]]*)]\(\${exercises.CONTENT_STORAGE_PLACEHOLDER}/([^\s)]+)(?:\s=([0-9\.]+)x([0-9\.]+))*[^)]*\)"
2222

2323

2424
def resize_image(image_content, width, height):
@@ -47,8 +47,6 @@ class ExerciseArchiveGenerator(ABC):
4747
ZIP_DATE_TIME = (2015, 10, 21, 7, 28, 0)
4848
ZIP_COMPRESS_TYPE = zipfile.ZIP_DEFLATED
4949
ZIP_COMMENT = "".encode()
50-
# Whether to keep width/height in image refs
51-
RETAIN_IMAGE_DIMENSIONS = True
5250

5351
@property
5452
@abstractmethod
@@ -199,20 +197,6 @@ def _process_single_image(
199197
)
200198
return resized_image or filename
201199

202-
def _replace_filename_in_match(
203-
self, content, img_match, old_filename, new_filename
204-
):
205-
"""Extract filename replacement logic"""
206-
start, end = img_match.span()
207-
old_match = content[start:end]
208-
new_match = old_match.replace(old_filename, new_filename)
209-
if not self.RETAIN_IMAGE_DIMENSIONS:
210-
# Remove dimensions from image ref
211-
new_match = re.sub(
212-
rf"{new_filename}\s=([0-9\.]+)x([0-9\.]+)", new_filename, new_match
213-
)
214-
return content[:start] + new_match + content[end:]
215-
216200
def _is_valid_image_filename(self, filename):
217201
checksum, ext = os.path.splitext(filename)
218202

@@ -241,35 +225,25 @@ def process_image_strings(self, content):
241225
new_file_path = self.get_image_file_path()
242226
new_image_path = self.get_image_ref_prefix()
243227
image_list = []
244-
processed_files = []
245-
for img_match in re.finditer(image_pattern, content):
228+
229+
def _replace_image(img_match):
246230
# Add any image files that haven't been written to the zipfile
247-
filename = img_match.group(1)
248-
width = float(img_match.group(2)) if img_match.group(2) else None
249-
height = float(img_match.group(3)) if img_match.group(3) else None
231+
filename = img_match.group(2)
232+
width = float(img_match.group(3)) if img_match.group(3) else None
233+
height = float(img_match.group(4)) if img_match.group(4) else None
250234
checksum, ext = os.path.splitext(filename)
251235

252236
if not self._is_valid_image_filename(filename):
253-
continue
237+
return ""
254238

255239
if width == 0 or height == 0:
256240
# Can't resize an image to 0 width or height, so just ignore.
257-
continue
241+
return ""
258242

259243
processed_filename = self._process_single_image(
260244
filename, checksum, ext, width, height, new_file_path
261245
)
262-
processed_files.append(
263-
(img_match, filename, processed_filename, width, height)
264-
)
265246

266-
# Process matches in reverse order to avoid index mismatch when modifying content
267-
for img_match, filename, processed_filename, width, height in reversed(
268-
processed_files
269-
):
270-
content = self._replace_filename_in_match(
271-
content, img_match, filename, processed_filename
272-
)
273247
if width is not None and height is not None:
274248
image_list.append(
275249
{
@@ -278,10 +252,10 @@ def process_image_strings(self, content):
278252
"height": height,
279253
}
280254
)
255+
return f"![{img_match.group(1)}]({new_image_path}/{processed_filename})"
256+
257+
content = re.sub(image_pattern, _replace_image, content)
281258

282-
content = content.replace(
283-
f"${exercises.CONTENT_STORAGE_PLACEHOLDER}", new_image_path
284-
)
285259
return content, image_list
286260

287261
def _process_content(self, content):

contentcuration/contentcuration/utils/assessment/qti/archive.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class QTIExerciseGenerator(ExerciseArchiveGenerator):
6464

6565
file_format = "zip"
6666
preset = format_presets.QTI_ZIP
67-
# Our markdown parser does not handle width/height in image refs
68-
RETAIN_IMAGE_DIMENSIONS = False
6967

7068
def __init__(self, *args, **kwargs):
7169
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)