From b553b761920d446949a5ec525d2cd591100fbe13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Skoglund?= Date: Tue, 21 Nov 2023 10:54:24 -0500 Subject: [PATCH 1/3] Set a default width for rendering images so they don't get raw wdith --- md2cf/confluence_renderer.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/md2cf/confluence_renderer.py b/md2cf/confluence_renderer.py index 74b3496..7682cf9 100644 --- a/md2cf/confluence_renderer.py +++ b/md2cf/confluence_renderer.py @@ -141,12 +141,26 @@ def block_code(self, code, lang=None): root_element.append(self.plain_text_body(code)) return root_element.render() - def image(self, src, title, text): + def image(self, src, title, text, width=764, height=None): + """ + Render an image as Confluence Storage Format. + See https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html + + :param src: The image source path + :param title: The title of the image + :param text: Used as alt (tooltip) text for the image + :param width: The rendered width of the image, defaults to 764 which seems to work well in Confluence. + :param height: Specify the height of the image, relative to width by default. + """ attributes = {"alt": text} if title: attributes["title"] = title + if width: + attributes["width"] = width + if height: + attributes["height"] = height - root_element = ConfluenceTag(name="image", attrib=attributes) + root_element = ConfluenceTag(name="image", attrib=attributes, namespace="ac") parsed_source = urlparse(src) if not parsed_source.netloc: # Local file, requires upload @@ -158,5 +172,4 @@ def image(self, src, title, text): else: url_tag = ConfluenceTag("url", attrib={"value": src}, namespace="ri") root_element.append(url_tag) - return root_element.render() From 03bda2239bc22a06d6606aee2659926a52b46edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Skoglund?= Date: Tue, 21 Nov 2023 11:01:32 -0500 Subject: [PATCH 2/3] Add width param to expected test output --- test_package/unit/test_renderer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test_package/unit/test_renderer.py b/test_package/unit/test_renderer.py index 1ab9596..16ff252 100644 --- a/test_package/unit/test_renderer.py +++ b/test_package/unit/test_renderer.py @@ -205,7 +205,7 @@ def test_renderer_header_only_sets_first_title(): def test_renderer_image_external(): test_image_src = "http://example.com/image.jpg" test_image_markup = ( - '\n' + '\n' "\n".format(test_image_src) ) @@ -220,7 +220,7 @@ def test_renderer_image_external_alt_and_title(): test_image_alt = "alt text" test_image_title = "title" test_image_markup = ( - '\n' + '\n' "\n".format(test_image_alt, test_image_title, test_image_src) ) @@ -236,7 +236,7 @@ def test_renderer_image_internal_absolute(): test_image_file = "image.jpg" test_image_src = "/home/test/images/" + test_image_file test_image_markup = ( - '\n' + '\n' "\n".format(test_image_file) ) @@ -250,7 +250,7 @@ def test_renderer_image_internal_relative(): test_image_file = "image.jpg" test_image_src = "test/images/" + test_image_file test_image_markup = ( - '\n' + '\n' "\n".format(test_image_file) ) From 219b9cca46ce8ba43334f5751e0d4793f7c248df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Skoglund?= Date: Tue, 21 Nov 2023 11:08:05 -0500 Subject: [PATCH 3/3] Switch default to even 768 --- md2cf/confluence_renderer.py | 4 ++-- test_package/unit/test_renderer.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/md2cf/confluence_renderer.py b/md2cf/confluence_renderer.py index 7682cf9..c664b96 100644 --- a/md2cf/confluence_renderer.py +++ b/md2cf/confluence_renderer.py @@ -141,7 +141,7 @@ def block_code(self, code, lang=None): root_element.append(self.plain_text_body(code)) return root_element.render() - def image(self, src, title, text, width=764, height=None): + def image(self, src, title, text, width=768, height=None): """ Render an image as Confluence Storage Format. See https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html @@ -149,7 +149,7 @@ def image(self, src, title, text, width=764, height=None): :param src: The image source path :param title: The title of the image :param text: Used as alt (tooltip) text for the image - :param width: The rendered width of the image, defaults to 764 which seems to work well in Confluence. + :param width: The rendered width of the image, defaults to 768 which seems to work well in Confluence. :param height: Specify the height of the image, relative to width by default. """ attributes = {"alt": text} diff --git a/test_package/unit/test_renderer.py b/test_package/unit/test_renderer.py index 16ff252..396131f 100644 --- a/test_package/unit/test_renderer.py +++ b/test_package/unit/test_renderer.py @@ -205,7 +205,7 @@ def test_renderer_header_only_sets_first_title(): def test_renderer_image_external(): test_image_src = "http://example.com/image.jpg" test_image_markup = ( - '\n' + '\n' "\n".format(test_image_src) ) @@ -220,7 +220,7 @@ def test_renderer_image_external_alt_and_title(): test_image_alt = "alt text" test_image_title = "title" test_image_markup = ( - '\n' + '\n' "\n".format(test_image_alt, test_image_title, test_image_src) ) @@ -236,7 +236,7 @@ def test_renderer_image_internal_absolute(): test_image_file = "image.jpg" test_image_src = "/home/test/images/" + test_image_file test_image_markup = ( - '\n' + '\n' "\n".format(test_image_file) ) @@ -250,7 +250,7 @@ def test_renderer_image_internal_relative(): test_image_file = "image.jpg" test_image_src = "test/images/" + test_image_file test_image_markup = ( - '\n' + '\n' "\n".format(test_image_file) )