Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,15 @@ def _test_image_resizing_in_field(self, field_type):
"Different sized images should have different file sizes",
)

# Verify that the dimensions have been stripped from the markdown
for file_name in unique_image_files:
# Because we can't predict the set ordering, just confirm that
# neither dimension descriptor is applied.
first_file = f"{file_name} =200x150"
self.assertNotIn(first_file, content)
second_file = f"{file_name} =100x75"
self.assertNotIn(second_file, content)

def test_image_resizing_in_question(self):
"""Test image resizing functionality in question content"""
self._test_image_resizing_in_field("question")
Expand Down
48 changes: 11 additions & 37 deletions contentcuration/contentcuration/utils/assessment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from contentcuration import models


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


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

@property
@abstractmethod
Expand Down Expand Up @@ -199,20 +197,6 @@ def _process_single_image(
)
return resized_image or filename

def _replace_filename_in_match(
self, content, img_match, old_filename, new_filename
):
"""Extract filename replacement logic"""
start, end = img_match.span()
old_match = content[start:end]
new_match = old_match.replace(old_filename, new_filename)
if not self.RETAIN_IMAGE_DIMENSIONS:
# Remove dimensions from image ref
new_match = re.sub(
rf"{new_filename}\s=([0-9\.]+)x([0-9\.]+)", new_filename, new_match
)
return content[:start] + new_match + content[end:]

def _is_valid_image_filename(self, filename):
checksum, ext = os.path.splitext(filename)

Expand Down Expand Up @@ -241,35 +225,25 @@ def process_image_strings(self, content):
new_file_path = self.get_image_file_path()
new_image_path = self.get_image_ref_prefix()
image_list = []
processed_files = []
for img_match in re.finditer(image_pattern, content):

def _replace_image(img_match):
# Add any image files that haven't been written to the zipfile
filename = img_match.group(1)
width = float(img_match.group(2)) if img_match.group(2) else None
height = float(img_match.group(3)) if img_match.group(3) else None
filename = img_match.group(2)
width = float(img_match.group(3)) if img_match.group(3) else None
height = float(img_match.group(4)) if img_match.group(4) else None
checksum, ext = os.path.splitext(filename)

if not self._is_valid_image_filename(filename):
continue
return ""

if width == 0 or height == 0:
# Can't resize an image to 0 width or height, so just ignore.
continue
return ""

processed_filename = self._process_single_image(
filename, checksum, ext, width, height, new_file_path
)
processed_files.append(
(img_match, filename, processed_filename, width, height)
)

# Process matches in reverse order to avoid index mismatch when modifying content
for img_match, filename, processed_filename, width, height in reversed(
processed_files
):
content = self._replace_filename_in_match(
content, img_match, filename, processed_filename
)
if width is not None and height is not None:
image_list.append(
{
Expand All @@ -278,10 +252,10 @@ def process_image_strings(self, content):
"height": height,
}
)
return f"![{img_match.group(1)}]({new_image_path}/{processed_filename})"

content = re.sub(image_pattern, _replace_image, content)

content = content.replace(
f"${exercises.CONTENT_STORAGE_PLACEHOLDER}", new_image_path
)
return content, image_list

def _process_content(self, content):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class QTIExerciseGenerator(ExerciseArchiveGenerator):

file_format = "zip"
preset = format_presets.QTI_ZIP
# Our markdown parser does not handle width/height in image refs
RETAIN_IMAGE_DIMENSIONS = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down