From 7fd8af95f67fb7346101a8d5a57782a420d38e53 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Sun, 17 Jul 2022 23:29:21 +0200 Subject: [PATCH 1/4] Simplifie du code --- zds/tutorialv2/publish_container.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/zds/tutorialv2/publish_container.py b/zds/tutorialv2/publish_container.py index 05b101202a..28e2195bc0 100644 --- a/zds/tutorialv2/publish_container.py +++ b/zds/tutorialv2/publish_container.py @@ -165,8 +165,8 @@ def publish_container( makedirs(current_dir) img_relative_path = ".." if ctx["relative"] == "." else "../" + ctx["relative"] + wrapped_image_callback = image_callback(img_relative_path) if image_callback else None if container.has_extracts(): # the container can be rendered in one template - wrapped_image_callback = image_callback(img_relative_path) if image_callback else image_callback args = {"container": container, "is_js": is_js} args.update(ctx) args["relative"] = img_relative_path @@ -184,9 +184,7 @@ def publish_container( container.introduction = None container.conclusion = None - else: # separate render of introduction and conclusion - wrapped_image_callback_intro_ccl = image_callback(img_relative_path) if image_callback else image_callback # create subdirectory if not path.isdir(current_dir): makedirs(current_dir) @@ -201,9 +199,7 @@ def publish_container( else: parsed = emarkdown(container.get_introduction(), db_object.js_support) container.introduction = str(part_path) - write_chapter_file( - base_dir, container, part_path, parsed, path_to_title_dict, wrapped_image_callback_intro_ccl - ) + write_chapter_file(base_dir, container, part_path, parsed, path_to_title_dict, wrapped_image_callback) children = copy.copy(container.children) container.children = [] container.children_dict = {} @@ -234,9 +230,7 @@ def publish_container( else: parsed = emarkdown(container.get_conclusion(), db_object.js_support) container.conclusion = str(part_path) - write_chapter_file( - base_dir, container, part_path, parsed, path_to_title_dict, wrapped_image_callback_intro_ccl - ) + write_chapter_file(base_dir, container, part_path, parsed, path_to_title_dict, wrapped_image_callback) return path_to_title_dict From b778990c8e0d4ddbecfceeead774c221051d12d3 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Sun, 17 Jul 2022 23:32:09 +0200 Subject: [PATCH 2/4] Fait fonctionner les images dans les ePUBs si zds-site est dans /media/... --- zds/tutorialv2/epub_utils.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/zds/tutorialv2/epub_utils.py b/zds/tutorialv2/epub_utils.py index 538ea872c2..3f5e7be710 100644 --- a/zds/tutorialv2/epub_utils.py +++ b/zds/tutorialv2/epub_utils.py @@ -206,7 +206,14 @@ def handle_image_path_with_good_img_dir_path(html_code): if self.url_scheme_matcher.search(image_url): splitted = parse.urlsplit(image_url) final_path = splitted.path - elif image_url.startswith(settings.MEDIA_URL): + elif (not (Path(settings.MEDIA_URL).is_dir() and Path(image_url).exists())) and image_url.startswith( + settings.MEDIA_URL + ): + # do not go there if image_url is the path on the system + # and not a portion of web URL + # (image_url.startswith(settings.MEDIA_URL) can be True if + # zds-site is in a directory under /media (the default + # value of settings.MEDIA_URL)) final_path = Path(image_url).name elif Path(image_url).is_absolute() and "images" in image_url: root = Path(image_url) From 00f7d5c02182de1a5ec50146157a5dcb4b19229b Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Sun, 17 Jul 2022 23:33:41 +0200 Subject: [PATCH 3/4] Supporte plusieurs niveaux de dossiers pour les images des ePUBs Changement probablement introduit par zmd 11. --- zds/tutorialv2/epub_utils.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/zds/tutorialv2/epub_utils.py b/zds/tutorialv2/epub_utils.py index 3f5e7be710..82769479af 100644 --- a/zds/tutorialv2/epub_utils.py +++ b/zds/tutorialv2/epub_utils.py @@ -22,10 +22,12 @@ def __build_mime_type_conf(): return {"filename": "mimetype", "content": "application/epub+zip"} -def __traverse_and_identify_images(image_dir): +def __traverse_and_identify_images(root_image_dir, current_dir=None): """ - :param image_dir: - :type image_dir: pathlib.Path + :param root_image_dir: Root folder of the images + :type root_image_dir: pathlib.Path + :param current_dir: Folder currently explored + :type current_dir: pathlib.Path :return: """ media_type_map = { @@ -36,13 +38,16 @@ def __traverse_and_identify_images(image_dir): ".svg": "image/svg", } - for image_file_path in image_dir.iterdir(): + if current_dir is None: + current_dir = root_image_dir + + for image_file_path in current_dir.iterdir(): if image_file_path.is_dir(): - yield from __traverse_and_identify_images(image_file_path) + yield from __traverse_and_identify_images(root_image_dir, image_file_path) continue ext = path.splitext(image_file_path.name)[1] - identifier = f"image_{image_file_path.name}".lower().replace(".", "-").replace("@", "-") - ebook_image_path = Path("images", image_file_path.name) + ebook_image_path = Path("images", image_file_path.relative_to(root_image_dir)) + identifier = "image_" + str(ebook_image_path)[7:].lower().replace(".", "-").replace("@", "-").replace("/", "-") yield ebook_image_path, identifier, media_type_map.get(ext.lower(), "image/png") @@ -224,7 +229,7 @@ def handle_image_path_with_good_img_dir_path(html_code): final_path = Path(image_url).name image_path_in_ebook = relative_path + "/images/" + str(final_path).replace("%20", "_") image["src"] = str(image_path_in_ebook) - self.names.add(Path(image_path_in_ebook).name) + self.names.add(final_path) ids = {} for element in soup_parser.find_all(name=None, attrs={"id": (lambda s: True)}): while element.get("id", None) and element["id"] in ids: @@ -236,8 +241,13 @@ def handle_image_path_with_good_img_dir_path(html_code): return handle_image_path_with_good_img_dir_path def remove_unused_image(self, image_path: Path, imglist): - for image in image_path.iterdir(): - if image.name not in self.names and not image.is_dir(): + # Remove unused images: + for image in image_path.rglob("*"): + if str(Path(image).relative_to(image_path)) not in self.names and not image.is_dir(): os.remove(str(image)) imglist = [i for i in imglist if i[0].name.replace("%20", "_") != image.name] + # Remove empty folders: + for item in image_path.iterdir(): + if item.is_dir() and len(list(item.iterdir())) == 0: + os.rmdir(str(item)) return imglist From a56d2309a10526dbe163d3be98ea3b9669a7709a Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Sun, 17 Jul 2022 23:34:04 +0200 Subject: [PATCH 4/4] Supporte les images aux URLs relatives dans les ePUBs Fix #6319 --- zds/utils/templatetags/emarkdown.py | 1 - 1 file changed, 1 deletion(-) diff --git a/zds/utils/templatetags/emarkdown.py b/zds/utils/templatetags/emarkdown.py index f2bd45299c..6dbc382d56 100644 --- a/zds/utils/templatetags/emarkdown.py +++ b/zds/utils/templatetags/emarkdown.py @@ -158,7 +158,6 @@ def epub_markdown(md_input, image_directory): md_input, output_format="epub", images_download_dir=image_directory.absolute, - local_url_to_local_path=[settings.MEDIA_URL + "galleries/[0-9]+", image_directory.relative], ) .replace('src"/', f'src="{media_root}') .replace(f'src="{media_root}{replaced_media_url}', f'src="{media_root}')