1111from django .conf import settings
1212from django .contrib .sites .models import Site
1313from django .core .files import File
14+ from django .core .files .storage import default_storage
1415from django .core .management .base import BaseCommand , CommandError
1516from django .db import transaction
1617from django .urls import reverse
@@ -255,23 +256,39 @@ def parse_statements(problem_meta, root, package):
255256 problem_meta ['translations' ] = []
256257 problem_meta ['tutorial' ] = ''
257258
258- image_cache = {}
259+ def process_images (text ):
260+ image_cache = problem_meta ['image_cache' ]
259261
260- def save_image (image_path ):
261- norm_path = os .path .normpath (os .path .join (statement_folder , image_path ))
262- sha1 = hashlib .sha1 ()
263- sha1 .update (package .open (norm_path , 'r' ).read ())
264- sha1 = sha1 .hexdigest ()
262+ def save_image (image_path ):
263+ norm_path = os .path .normpath (os .path .join (statement_folder , image_path ))
264+ sha1 = hashlib .sha1 ()
265+ sha1 .update (package .open (norm_path , 'r' ).read ())
266+ sha1 = sha1 .hexdigest ()
265267
266- if sha1 not in image_cache :
267- image = File (
268- file = package .open (norm_path , 'r' ),
269- name = os .path .basename (image_path ),
268+ if sha1 not in image_cache :
269+ image = File (
270+ file = package .open (norm_path , 'r' ),
271+ name = os .path .basename (image_path ),
272+ )
273+ data = json .loads (django_uploader (image ))
274+ image_cache [sha1 ] = data ['link' ]
275+
276+ return image_cache [sha1 ]
277+
278+ for image_path in set (re .findall (r'!\[image\]\((.+?)\)' , text )):
279+ text = text .replace (
280+ f'' ,
281+ f'} )' ,
270282 )
271- data = json .loads (django_uploader (image ))
272- image_cache [sha1 ] = data ['link' ]
273283
274- return image_cache [sha1 ]
284+ for img_tag in set (re .findall (r'<\s*img[^>]*>' , text )):
285+ image_path = re .search (r'<\s*img[^>]+src\s*=\s*(["\'])(.*?)\1[^>]*>' , text ).group (2 )
286+ text = text .replace (
287+ img_tag ,
288+ img_tag .replace (image_path , save_image (image_path )),
289+ )
290+
291+ return text
275292
276293 def parse_problem_properties (problem_properties ):
277294 description = ''
@@ -304,20 +321,6 @@ def parse_problem_properties(problem_properties):
304321 description += '\n ## Notes\n \n '
305322 description += pandoc_tex_to_markdown (problem_properties ['notes' ])
306323
307- # Images
308- for image_path in set (re .findall (r'!\[image\]\((.+?)\)' , description )):
309- description = description .replace (
310- f'' ,
311- f'} )' ,
312- )
313-
314- for img_tag in set (re .findall (r'<\s*img[^>]*>' , description )):
315- image_path = re .search (r'<\s*img[^>]+src\s*=\s*(["\'])(.*?)\1[^>]*>' , description ).group (2 )
316- description = description .replace (
317- img_tag ,
318- img_tag .replace (image_path , save_image (image_path )),
319- )
320-
321324 return description
322325
323326 def input_choice (prompt , choices ):
@@ -351,7 +354,7 @@ def input_choice(prompt, choices):
351354 description = parse_problem_properties (problem_properties )
352355 translations .append ({
353356 'language' : language ,
354- 'description' : description ,
357+ 'description' : process_images ( description ) ,
355358 })
356359
357360 tutorial = problem_properties ['tutorial' ]
@@ -378,6 +381,9 @@ def input_choice(prompt, choices):
378381 elif len (tutorials ) > 0 :
379382 problem_meta ['tutorial' ] = tutorials [0 ]['tutorial' ]
380383
384+ # Process images for only the selected tutorial
385+ problem_meta ['tutorial' ] = process_images (problem_meta ['tutorial' ])
386+
381387 for t in translations :
382388 language = t ['language' ]
383389 description = t ['description' ]
@@ -545,6 +551,7 @@ def handle(self, *args, **options):
545551
546552 # A dictionary to hold all problem information.
547553 problem_meta = {}
554+ problem_meta ['image_cache' ] = {}
548555 problem_meta ['code' ] = problem_code
549556 problem_meta ['tmp_dir' ] = tempfile .TemporaryDirectory ()
550557 problem_meta ['authors' ] = problem_authors
@@ -555,6 +562,11 @@ def handle(self, *args, **options):
555562 parse_statements (problem_meta , root , package )
556563 create_problem (problem_meta )
557564 except Exception :
565+ # Remove imported images
566+ for image_url in problem_meta ['image_cache' ].values ():
567+ path = default_storage .path (os .path .join (settings .MARTOR_UPLOAD_MEDIA_DIR , os .path .basename (image_url )))
568+ os .remove (path )
569+
558570 raise
559571 finally :
560572 problem_meta ['tmp_dir' ].cleanup ()
0 commit comments