Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a default width for rendering images so they don't get raw wdith #119

Closed
wants to merge 3 commits into from
Closed
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
19 changes: 16 additions & 3 deletions md2cf/confluence_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=768, 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 768 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
Expand All @@ -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()
8 changes: 4 additions & 4 deletions test_package/unit/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
'<ac:image ac:alt=""><ri:url ri:value="{}"></ri:url>\n'
'<ac:image ac:alt="" ac:width="768"><ri:url ri:value="{}"></ri:url>\n'
"</ac:image>\n".format(test_image_src)
)

Expand All @@ -220,7 +220,7 @@ def test_renderer_image_external_alt_and_title():
test_image_alt = "alt text"
test_image_title = "title"
test_image_markup = (
'<ac:image ac:alt="{}" ac:title="{}"><ri:url ri:value="{}"></ri:url>\n'
'<ac:image ac:alt="{}" ac:title="{}" ac:width="768"><ri:url ri:value="{}"></ri:url>\n'
"</ac:image>\n".format(test_image_alt, test_image_title, test_image_src)
)

Expand All @@ -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 = (
'<ac:image ac:alt=""><ri:attachment ri:filename="{}"></ri:attachment>\n'
'<ac:image ac:alt="" ac:width="768"><ri:attachment ri:filename="{}"></ri:attachment>\n'
"</ac:image>\n".format(test_image_file)
)

Expand All @@ -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 = (
'<ac:image ac:alt=""><ri:attachment ri:filename="{}"></ri:attachment>\n'
'<ac:image ac:alt="" ac:width="768"><ri:attachment ri:filename="{}"></ri:attachment>\n'
"</ac:image>\n".format(test_image_file)
)

Expand Down
Loading