Skip to content
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ CLI Utilities for Confluence.
### System Requirements

1. Python 3.7+
2. [Cairo](https://cairographics.org/)

#### Installing Cairo

For mac systems you can use homebrew and run `brew install cairo`

For alpine linux run
`apk add --no-cache build-base cairo-dev cairo cairo-tools jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev`

For all other systems follow the instructions on [cairo's download page](https://cairographics.org/download/)

### Install with `pipx` (recommended)

Expand Down
48 changes: 43 additions & 5 deletions confluence_utils/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import sys
import xml.etree.ElementTree as ET
from typing import List, Optional

import cairosvg
import click
from atlassian import Confluence
from tabulate import tabulate
Expand Down Expand Up @@ -212,17 +214,53 @@ def publish_files(

if markdown_file.get_page_id_for_space(space):
for attachment in markdown_file.attachments:
confluence.attach_file(
filename=attachment,
name=os.path.basename(attachment),
page_id=markdown_file.get_page_id_for_space(space),
space=space,
full_filename = os.path.splitext(
os.path.basename(attachment)
)
file_ext = full_filename[1]
filename = full_filename[0]
if file_ext.lower() != ".svg":
confluence.attach_file(
filename=attachment,
name=filename + file_ext,
page_id=markdown_file.get_page_id_for_space(space),
space=space,
)
else:

imagestring = open(attachment).read().encode("utf-8")

# for some reason we sometimes get back bad svgs
# so we need to handle these bad tags and remove them
root = ET.fromstring(imagestring)
cleanup_svg(root)
imagestring = ET.tostring(
root, encoding="utf-8", method="xml"
)

bytestring = cairosvg.svg2png(imagestring)
confluence.attach_content(
content=bytestring,
name=filename + "_svg" + ".png",
page_id=markdown_file.get_page_id_for_space(space),
space=space,
)

except FileNotFoundError as e:
raise click.FileError(path, hint=str(e))


def cleanup_svg(root: ET.Element) -> None:
for child_with_image in root.findall(
".//{http://www.w3.org/2000/svg}image/.."
):
for image in child_with_image.findall(
"{http://www.w3.org/2000/svg}image"
):
if image.attrib.get("{http://www.w3.org/1999/xlink}href") is None:
child_with_image.remove(image)


def get_files(
path: str,
extension_filter: Optional[str] = None,
Expand Down
8 changes: 7 additions & 1 deletion confluence_utils/confluence_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ def image(
image_tag = '<ri:url ri:value="{}" />'.format(src)
else:
self.attachments.append(src)
full_filename = os.path.splitext(os.path.basename(src))
file_ext = full_filename[1]
filename = full_filename[0]
if file_ext.lower() == ".svg":
filename = full_filename[0] + "_svg"
file_ext = ".png"
image_tag = '<ri:attachment ri:filename="{}" />\n'.format(
os.path.basename(src)
filename + file_ext
)

html += image_tag
Expand Down
153 changes: 149 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python-frontmatter = "^1.0.0"
atlassian-python-api = "^3.21.0"
tabulate = "^0.8.9"
click-log = "^0.4.0"
CairoSVG = "^2.5.2"

[tool.poetry.dev-dependencies]
black = "^22.3.0"
Expand Down