1818from 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
2424def 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""
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 ):
0 commit comments