markdown-to-confluence 0.4.3__py3-none-any.whl → 0.4.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {markdown_to_confluence-0.4.3.dist-info → markdown_to_confluence-0.4.5.dist-info}/METADATA +104 -25
- markdown_to_confluence-0.4.5.dist-info/RECORD +33 -0
- {markdown_to_confluence-0.4.3.dist-info → markdown_to_confluence-0.4.5.dist-info}/licenses/LICENSE +1 -1
- md2conf/__init__.py +1 -1
- md2conf/__main__.py +19 -4
- md2conf/api.py +9 -1
- md2conf/application.py +16 -8
- md2conf/converter.py +835 -579
- md2conf/csf.py +217 -0
- md2conf/domain.py +2 -0
- md2conf/drawio.py +18 -14
- md2conf/latex.py +245 -0
- md2conf/local.py +2 -2
- md2conf/markdown.py +19 -11
- md2conf/mermaid.py +21 -27
- md2conf/text.py +54 -0
- md2conf/toc.py +89 -0
- md2conf/uri.py +46 -0
- md2conf/xml.py +84 -14
- markdown_to_confluence-0.4.3.dist-info/RECORD +0 -29
- md2conf/emoji.py +0 -83
- {markdown_to_confluence-0.4.3.dist-info → markdown_to_confluence-0.4.5.dist-info}/WHEEL +0 -0
- {markdown_to_confluence-0.4.3.dist-info → markdown_to_confluence-0.4.5.dist-info}/entry_points.txt +0 -0
- {markdown_to_confluence-0.4.3.dist-info → markdown_to_confluence-0.4.5.dist-info}/top_level.txt +0 -0
- {markdown_to_confluence-0.4.3.dist-info → markdown_to_confluence-0.4.5.dist-info}/zip-safe +0 -0
md2conf/csf.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Publish Markdown files to Confluence wiki.
|
|
3
|
+
|
|
4
|
+
Copyright 2022-2025, Levente Hunyadi
|
|
5
|
+
|
|
6
|
+
:see: https://github.com/hunyadi/md2conf
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import importlib.resources as resources
|
|
10
|
+
import re
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Callable, TypeVar
|
|
13
|
+
|
|
14
|
+
import lxml.etree as ET
|
|
15
|
+
from lxml.builder import ElementMaker
|
|
16
|
+
|
|
17
|
+
# XML namespaces typically associated with Confluence Storage Format documents
|
|
18
|
+
_namespaces = {
|
|
19
|
+
"ac": "http://atlassian.com/content",
|
|
20
|
+
"ri": "http://atlassian.com/resource/identifier",
|
|
21
|
+
}
|
|
22
|
+
for key, value in _namespaces.items():
|
|
23
|
+
ET.register_namespace(key, value)
|
|
24
|
+
|
|
25
|
+
HTML = ElementMaker()
|
|
26
|
+
AC_ELEM = ElementMaker(namespace=_namespaces["ac"])
|
|
27
|
+
RI_ELEM = ElementMaker(namespace=_namespaces["ri"])
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ParseError(RuntimeError):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _qname(namespace_uri: str, name: str) -> str:
|
|
35
|
+
return ET.QName(namespace_uri, name).text
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def AC_ATTR(name: str) -> str:
|
|
39
|
+
return _qname(_namespaces["ac"], name)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def RI_ATTR(name: str) -> str:
|
|
43
|
+
return _qname(_namespaces["ri"], name)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
R = TypeVar("R")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def with_entities(func: Callable[[Path], R]) -> R:
|
|
50
|
+
"Invokes a callable in the context of an entity definition file."
|
|
51
|
+
|
|
52
|
+
resource_path = resources.files(__package__).joinpath("entities.dtd")
|
|
53
|
+
with resources.as_file(resource_path) as dtd_path:
|
|
54
|
+
return func(dtd_path)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _elements_from_strings(dtd_path: Path, items: list[str]) -> ET._Element:
|
|
58
|
+
"""
|
|
59
|
+
Creates an XML document tree from XML fragment strings.
|
|
60
|
+
|
|
61
|
+
This function
|
|
62
|
+
* adds an XML declaration,
|
|
63
|
+
* wraps the content in a root element,
|
|
64
|
+
* adds namespace declarations associated with Confluence documents.
|
|
65
|
+
|
|
66
|
+
:param dtd_path: Path to a DTD document that defines entities like `¢` or `©`.
|
|
67
|
+
:param items: Strings to parse into XML fragments.
|
|
68
|
+
:returns: An XML document as an element tree.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
parser = ET.XMLParser(
|
|
72
|
+
remove_blank_text=True,
|
|
73
|
+
remove_comments=True,
|
|
74
|
+
strip_cdata=False,
|
|
75
|
+
load_dtd=True,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
ns_attr_list = "".join(f' xmlns:{key}="{value}"' for key, value in _namespaces.items())
|
|
79
|
+
|
|
80
|
+
data = [
|
|
81
|
+
'<?xml version="1.0"?>',
|
|
82
|
+
f'<!DOCTYPE ac:confluence PUBLIC "-//Atlassian//Confluence 4 Page//EN" "{dtd_path.as_posix()}"><root{ns_attr_list}>',
|
|
83
|
+
]
|
|
84
|
+
data.extend(items)
|
|
85
|
+
data.append("</root>")
|
|
86
|
+
|
|
87
|
+
try:
|
|
88
|
+
return ET.fromstringlist(data, parser=parser)
|
|
89
|
+
except ET.XMLSyntaxError as ex:
|
|
90
|
+
raise ParseError() from ex
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def elements_from_strings(items: list[str]) -> ET._Element:
|
|
94
|
+
"""
|
|
95
|
+
Creates a Confluence Storage Format XML document tree from XML fragment strings.
|
|
96
|
+
|
|
97
|
+
A root element is created to hold several XML fragments.
|
|
98
|
+
|
|
99
|
+
:param items: Strings to parse into XML fragments.
|
|
100
|
+
:returns: An XML document as an element tree.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
return with_entities(lambda dtd_path: _elements_from_strings(dtd_path, items))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def elements_from_string(content: str) -> ET._Element:
|
|
107
|
+
"""
|
|
108
|
+
Creates a Confluence Storage Format XML document tree from an XML string.
|
|
109
|
+
|
|
110
|
+
:param content: String to parse into XML.
|
|
111
|
+
:returns: An XML document as an element tree.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
return elements_from_strings([content])
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _content_to_string(dtd_path: Path, content: str) -> str:
|
|
118
|
+
tree = _elements_from_strings(dtd_path, [content])
|
|
119
|
+
return ET.tostring(tree, pretty_print=True).decode("utf-8")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def content_to_string(content: str) -> str:
|
|
123
|
+
"""
|
|
124
|
+
Converts a Confluence Storage Format document returned by the Confluence REST API into a readable XML document.
|
|
125
|
+
|
|
126
|
+
This function
|
|
127
|
+
* adds an XML declaration,
|
|
128
|
+
* wraps the content in a root element,
|
|
129
|
+
* adds namespace declarations associated with Confluence documents.
|
|
130
|
+
|
|
131
|
+
:param content: Confluence Storage Format content as a string.
|
|
132
|
+
:returns: XML as a string.
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
return with_entities(lambda dtd_path: _content_to_string(dtd_path, content))
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def elements_to_string(root: ET._Element) -> str:
|
|
139
|
+
"""
|
|
140
|
+
Converts a Confluence Storage Format element tree into an XML string to push to Confluence REST API.
|
|
141
|
+
|
|
142
|
+
:param root: Synthesized XML element tree of a Confluence Storage Format document.
|
|
143
|
+
:returns: XML as a string.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
xml = ET.tostring(root, encoding="utf8", method="xml").decode("utf8")
|
|
147
|
+
m = re.match(r"^<root\s+[^>]*>(.*)</root>\s*$", xml, re.DOTALL)
|
|
148
|
+
if m:
|
|
149
|
+
return m.group(1)
|
|
150
|
+
else:
|
|
151
|
+
raise ValueError("expected: Confluence content")
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def is_block_like(elem: ET._Element) -> bool:
|
|
155
|
+
return elem.tag in ["div", "li", "ol", "p", "pre", "ul"]
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def normalize_inline(elem: ET._Element) -> None:
|
|
159
|
+
"""
|
|
160
|
+
Ensures that inline elements are direct children of an eligible block element.
|
|
161
|
+
|
|
162
|
+
The following transformations are applied:
|
|
163
|
+
|
|
164
|
+
* consecutive inline elements and text nodes that are the direct children of the parent element are wrapped into a `<p>`,
|
|
165
|
+
* block elements are left intact,
|
|
166
|
+
* leading and trailing whitespace in each block element is removed.
|
|
167
|
+
|
|
168
|
+
The above steps transform an element tree such as
|
|
169
|
+
```
|
|
170
|
+
<li> to <em>be</em>, <ol/> not to <em>be</em> </li>
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
into another element tree such as
|
|
174
|
+
```
|
|
175
|
+
<li><p>to <em>be</em>,</p><ol/><p>not to <em>be</em></p></li>
|
|
176
|
+
```
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
if not is_block_like(elem):
|
|
180
|
+
raise ValueError(f"expected: block element; got: {elem.tag!s}")
|
|
181
|
+
|
|
182
|
+
contents: list[ET._Element] = []
|
|
183
|
+
|
|
184
|
+
paragraph = HTML.p()
|
|
185
|
+
contents.append(paragraph)
|
|
186
|
+
if elem.text:
|
|
187
|
+
paragraph.text = elem.text
|
|
188
|
+
elem.text = None
|
|
189
|
+
|
|
190
|
+
for child in elem:
|
|
191
|
+
if is_block_like(child):
|
|
192
|
+
contents.append(child)
|
|
193
|
+
paragraph = HTML.p()
|
|
194
|
+
contents.append(paragraph)
|
|
195
|
+
if child.tail:
|
|
196
|
+
paragraph.text = child.tail
|
|
197
|
+
child.tail = None
|
|
198
|
+
else:
|
|
199
|
+
paragraph.append(child)
|
|
200
|
+
|
|
201
|
+
for item in contents:
|
|
202
|
+
# remove lead whitespace in the block element
|
|
203
|
+
if item.text:
|
|
204
|
+
item.text = item.text.lstrip()
|
|
205
|
+
if len(item) > 0:
|
|
206
|
+
# remove tail whitespace in the last child of the block element
|
|
207
|
+
last = item[-1]
|
|
208
|
+
if last.tail:
|
|
209
|
+
last.tail = last.tail.rstrip()
|
|
210
|
+
else:
|
|
211
|
+
# remove tail whitespace directly in the block element content
|
|
212
|
+
if item.text:
|
|
213
|
+
item.text = item.text.rstrip()
|
|
214
|
+
|
|
215
|
+
# ignore empty elements
|
|
216
|
+
if item.tag != "p" or len(item) > 0 or item.text:
|
|
217
|
+
elem.append(item)
|
md2conf/domain.py
CHANGED
|
@@ -30,6 +30,7 @@ class ConfluenceDocumentOptions:
|
|
|
30
30
|
:param prefer_raster: Whether to choose PNG files over SVG files when available.
|
|
31
31
|
:param render_drawio: Whether to pre-render (or use the pre-rendered version of) draw.io diagrams.
|
|
32
32
|
:param render_mermaid: Whether to pre-render Mermaid diagrams into PNG/SVG images.
|
|
33
|
+
:param render_latex: Whether to pre-render LaTeX formulas into PNG/SVG images.
|
|
33
34
|
:param diagram_output_format: Target image format for diagrams.
|
|
34
35
|
:param webui_links: When true, convert relative URLs to Confluence Web UI links.
|
|
35
36
|
"""
|
|
@@ -42,5 +43,6 @@ class ConfluenceDocumentOptions:
|
|
|
42
43
|
prefer_raster: bool = True
|
|
43
44
|
render_drawio: bool = False
|
|
44
45
|
render_mermaid: bool = False
|
|
46
|
+
render_latex: bool = False
|
|
45
47
|
diagram_output_format: Literal["png", "svg"] = "png"
|
|
46
48
|
webui_links: bool = False
|
md2conf/drawio.py
CHANGED
|
@@ -9,9 +9,9 @@ Copyright 2022-2025, Levente Hunyadi
|
|
|
9
9
|
import base64
|
|
10
10
|
import logging
|
|
11
11
|
import os
|
|
12
|
-
import os.path
|
|
13
12
|
import shutil
|
|
14
13
|
import subprocess
|
|
14
|
+
import tempfile
|
|
15
15
|
import typing
|
|
16
16
|
import zlib
|
|
17
17
|
from pathlib import Path
|
|
@@ -153,7 +153,8 @@ def extract_xml_from_png(png_data: bytes) -> ET._Element:
|
|
|
153
153
|
offset += 8
|
|
154
154
|
|
|
155
155
|
if offset + length + 4 > len(png_data):
|
|
156
|
-
|
|
156
|
+
chunk_name = chunk_type.decode("ascii", errors="replace")
|
|
157
|
+
raise DrawioError(f"corrupted PNG: incomplete data for chunk {chunk_name}")
|
|
157
158
|
|
|
158
159
|
# read chunk data
|
|
159
160
|
chunk_data = png_data[offset : offset + length]
|
|
@@ -169,7 +170,7 @@ def extract_xml_from_png(png_data: bytes) -> ET._Element:
|
|
|
169
170
|
# format: keyword\0text
|
|
170
171
|
null_pos = chunk_data.find(b"\x00")
|
|
171
172
|
if null_pos < 0:
|
|
172
|
-
raise DrawioError("corrupted PNG: tEXt chunk missing keyword")
|
|
173
|
+
raise DrawioError("corrupted PNG: `tEXt` chunk missing keyword or data")
|
|
173
174
|
|
|
174
175
|
keyword = chunk_data[:null_pos].decode("latin1")
|
|
175
176
|
if keyword != "mxfile":
|
|
@@ -236,17 +237,21 @@ def render_diagram(source: Path, output_format: typing.Literal["png", "svg"] = "
|
|
|
236
237
|
if executable is None:
|
|
237
238
|
raise DrawioError("draw.io executable not found")
|
|
238
239
|
|
|
239
|
-
|
|
240
|
+
# create a temporary file and get its file descriptor and path
|
|
241
|
+
fd, target = tempfile.mkstemp(prefix="drawio_", suffix=f".{output_format}")
|
|
240
242
|
|
|
241
|
-
cmd = [executable, "--export", "--format", output_format, "--output", target]
|
|
242
|
-
if output_format == "png":
|
|
243
|
-
cmd.extend(["--scale", "2", "--transparent"])
|
|
244
|
-
elif output_format == "svg":
|
|
245
|
-
cmd.append("--embed-svg-images")
|
|
246
|
-
cmd.append(str(source))
|
|
247
|
-
|
|
248
|
-
LOGGER.debug("Executing: %s", " ".join(cmd))
|
|
249
243
|
try:
|
|
244
|
+
# close the descriptor, just use the filename
|
|
245
|
+
os.close(fd)
|
|
246
|
+
|
|
247
|
+
cmd = [executable, "--export", "--format", output_format, "--output", target]
|
|
248
|
+
if output_format == "png":
|
|
249
|
+
cmd.extend(["--scale", "2", "--transparent"])
|
|
250
|
+
elif output_format == "svg":
|
|
251
|
+
cmd.append("--embed-svg-images")
|
|
252
|
+
cmd.append(str(source))
|
|
253
|
+
|
|
254
|
+
LOGGER.debug("Executing: %s", " ".join(cmd))
|
|
250
255
|
proc = subprocess.Popen(
|
|
251
256
|
cmd,
|
|
252
257
|
stdout=subprocess.PIPE,
|
|
@@ -267,5 +272,4 @@ def render_diagram(source: Path, output_format: typing.Literal["png", "svg"] = "
|
|
|
267
272
|
return f.read()
|
|
268
273
|
|
|
269
274
|
finally:
|
|
270
|
-
|
|
271
|
-
os.remove(target)
|
|
275
|
+
os.remove(target)
|
md2conf/latex.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Publish Markdown files to Confluence wiki.
|
|
3
|
+
|
|
4
|
+
Copyright 2022-2025, Levente Hunyadi
|
|
5
|
+
|
|
6
|
+
:see: https://github.com/hunyadi/md2conf
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import importlib.util
|
|
10
|
+
from io import BytesIO
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from struct import unpack
|
|
13
|
+
from typing import BinaryIO, Iterable, Literal, Optional, Union, overload
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def render_latex(expression: str, *, format: Literal["png", "svg"] = "png", dpi: int = 100, font_size: int = 12) -> bytes:
|
|
17
|
+
"""
|
|
18
|
+
Generates a PNG or SVG image of a LaTeX math expression using `matplotlib` for rendering.
|
|
19
|
+
|
|
20
|
+
:param expression: A LaTeX math expression, e.g., r'\frac{a}{b}'.
|
|
21
|
+
:param format: Output image format.
|
|
22
|
+
:param dpi: Output image resolution (if applicable).
|
|
23
|
+
:param font_size: Font size of the LaTeX text (if applicable).
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
with BytesIO() as f:
|
|
27
|
+
_render_latex(expression, f, format=format, dpi=dpi, font_size=font_size)
|
|
28
|
+
return f.getvalue()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if importlib.util.find_spec("matplotlib") is None:
|
|
32
|
+
LATEX_ENABLED = False
|
|
33
|
+
|
|
34
|
+
def _render_latex(expression: str, f: BinaryIO, *, format: Literal["png", "svg"], dpi: int, font_size: int) -> None:
|
|
35
|
+
raise RuntimeError("matplotlib not installed; run: `pip install matplotlib`")
|
|
36
|
+
|
|
37
|
+
else:
|
|
38
|
+
import matplotlib
|
|
39
|
+
import matplotlib.pyplot as plt
|
|
40
|
+
|
|
41
|
+
matplotlib.rcParams["mathtext.fontset"] = "cm" # change font to "Computer Modern"
|
|
42
|
+
|
|
43
|
+
LATEX_ENABLED = True
|
|
44
|
+
|
|
45
|
+
def _render_latex(expression: str, f: BinaryIO, *, format: Literal["png", "svg"], dpi: int, font_size: int) -> None:
|
|
46
|
+
# create a figure with no axis
|
|
47
|
+
fig = plt.figure(dpi=dpi)
|
|
48
|
+
|
|
49
|
+
# transparent background
|
|
50
|
+
fig.patch.set_alpha(0)
|
|
51
|
+
|
|
52
|
+
# add LaTeX text
|
|
53
|
+
fig.text(x=0, y=0, s=f"${expression}$", fontsize=font_size)
|
|
54
|
+
|
|
55
|
+
# save the image
|
|
56
|
+
fig.savefig(
|
|
57
|
+
f,
|
|
58
|
+
transparent=True,
|
|
59
|
+
format=format,
|
|
60
|
+
bbox_inches="tight",
|
|
61
|
+
pad_inches=0.0,
|
|
62
|
+
metadata={"Title": expression} if format == "png" else None,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
# close the figure to free memory
|
|
66
|
+
plt.close(fig)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@overload
|
|
70
|
+
def get_png_dimensions(*, data: bytes) -> tuple[int, int]: ...
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@overload
|
|
74
|
+
def get_png_dimensions(*, path: Union[str, Path]) -> tuple[int, int]: ...
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def get_png_dimensions(*, data: Optional[bytes] = None, path: Union[str, Path, None] = None) -> tuple[int, int]:
|
|
78
|
+
"""
|
|
79
|
+
Returns the width and height of a PNG image inspecting its header.
|
|
80
|
+
|
|
81
|
+
:param data: PNG image data.
|
|
82
|
+
:param path: Path to the PNG image file.
|
|
83
|
+
:returns: A tuple of the image's width and height in pixels.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
if data is not None and path is not None:
|
|
87
|
+
raise TypeError("expected: either `data` or `path`; got: both")
|
|
88
|
+
elif data is not None:
|
|
89
|
+
with BytesIO(data) as f:
|
|
90
|
+
return _get_png_dimensions(f)
|
|
91
|
+
elif path is not None:
|
|
92
|
+
with open(path, "rb") as f:
|
|
93
|
+
return _get_png_dimensions(f)
|
|
94
|
+
else:
|
|
95
|
+
raise TypeError("expected: either `data` or `path`; got: neither")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@overload
|
|
99
|
+
def remove_png_chunks(names: Iterable[str], *, source_data: bytes) -> bytes: ...
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@overload
|
|
103
|
+
def remove_png_chunks(names: Iterable[str], *, source_path: Union[str, Path]) -> bytes: ...
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@overload
|
|
107
|
+
def remove_png_chunks(names: Iterable[str], *, source_data: bytes, target_path: Union[str, Path]) -> None: ...
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@overload
|
|
111
|
+
def remove_png_chunks(names: Iterable[str], *, source_path: Union[str, Path], target_path: Union[str, Path]) -> None: ...
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def remove_png_chunks(
|
|
115
|
+
names: Iterable[str], *, source_data: Optional[bytes] = None, source_path: Union[str, Path, None] = None, target_path: Union[str, Path, None] = None
|
|
116
|
+
) -> Optional[bytes]:
|
|
117
|
+
"""
|
|
118
|
+
Rewrites a PNG file by removing chunks with the specified names.
|
|
119
|
+
|
|
120
|
+
:param source_data: PNG image data.
|
|
121
|
+
:param source_path: Path to the file to read from.
|
|
122
|
+
:param target_path: Path to the file to write to.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
if source_data is not None and source_path is not None:
|
|
126
|
+
raise TypeError("expected: either `source_data` or `source_path`; got: both")
|
|
127
|
+
elif source_data is not None:
|
|
128
|
+
|
|
129
|
+
def source_reader() -> BinaryIO:
|
|
130
|
+
return BytesIO(source_data)
|
|
131
|
+
elif source_path is not None:
|
|
132
|
+
|
|
133
|
+
def source_reader() -> BinaryIO:
|
|
134
|
+
return open(source_path, "rb")
|
|
135
|
+
else:
|
|
136
|
+
raise TypeError("expected: either `source_data` or `source_path`; got: neither")
|
|
137
|
+
|
|
138
|
+
if target_path is None:
|
|
139
|
+
with source_reader() as source_file, BytesIO() as memory_file:
|
|
140
|
+
_remove_png_chunks(names, source_file, memory_file)
|
|
141
|
+
return memory_file.getvalue()
|
|
142
|
+
else:
|
|
143
|
+
with source_reader() as source_file, open(target_path, "wb") as target_file:
|
|
144
|
+
_remove_png_chunks(names, source_file, target_file)
|
|
145
|
+
return None
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class _Chunk:
|
|
149
|
+
__slots__ = ("length", "name", "data", "crc")
|
|
150
|
+
|
|
151
|
+
length: int
|
|
152
|
+
name: bytes
|
|
153
|
+
data: bytes
|
|
154
|
+
crc: bytes
|
|
155
|
+
|
|
156
|
+
def __init__(self, length: int, name: bytes, data: bytes, crc: bytes):
|
|
157
|
+
self.length = length
|
|
158
|
+
self.name = name
|
|
159
|
+
self.data = data
|
|
160
|
+
self.crc = crc
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _read_signature(f: BinaryIO) -> None:
|
|
164
|
+
"Reads and checks PNG signature (first 8 bytes)."
|
|
165
|
+
|
|
166
|
+
signature = f.read(8)
|
|
167
|
+
if signature != b"\x89PNG\r\n\x1a\n":
|
|
168
|
+
raise ValueError("not a valid PNG file")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _read_chunk(f: BinaryIO) -> Optional[_Chunk]:
|
|
172
|
+
"Reads and parses a PNG chunk such as `IHDR` or `tEXt`."
|
|
173
|
+
|
|
174
|
+
length_bytes = f.read(4)
|
|
175
|
+
if not length_bytes:
|
|
176
|
+
return None
|
|
177
|
+
|
|
178
|
+
if len(length_bytes) != 4:
|
|
179
|
+
raise ValueError("insufficient bytes to read chunk length")
|
|
180
|
+
|
|
181
|
+
length = int.from_bytes(length_bytes, "big")
|
|
182
|
+
|
|
183
|
+
data_length = 4 + length + 4
|
|
184
|
+
data_bytes = f.read(data_length)
|
|
185
|
+
if len(data_bytes) != data_length:
|
|
186
|
+
raise ValueError(f"insufficient bytes to read chunk data of length {length}")
|
|
187
|
+
|
|
188
|
+
chunk_type = data_bytes[0:4]
|
|
189
|
+
chunk_data = data_bytes[4:-4]
|
|
190
|
+
crc = data_bytes[-4:]
|
|
191
|
+
|
|
192
|
+
return _Chunk(length, chunk_type, chunk_data, crc)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _write_chunk(f: BinaryIO, chunk: _Chunk) -> None:
|
|
196
|
+
f.write(chunk.length.to_bytes(4, "big"))
|
|
197
|
+
f.write(chunk.name)
|
|
198
|
+
f.write(chunk.data)
|
|
199
|
+
f.write(chunk.crc)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _get_png_dimensions(source_file: BinaryIO) -> tuple[int, int]:
|
|
203
|
+
"""
|
|
204
|
+
Returns the width and height of a PNG image inspecting its header.
|
|
205
|
+
|
|
206
|
+
:param source_file: A binary file opened for reading that contains PNG image data.
|
|
207
|
+
:returns: A tuple of the image's width and height in pixels.
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
_read_signature(source_file)
|
|
211
|
+
|
|
212
|
+
# validate IHDR chunk
|
|
213
|
+
ihdr = _read_chunk(source_file)
|
|
214
|
+
if ihdr is None:
|
|
215
|
+
raise ValueError("missing IHDR chunk")
|
|
216
|
+
|
|
217
|
+
if ihdr.length != 13:
|
|
218
|
+
raise ValueError("invalid chunk length")
|
|
219
|
+
if ihdr.name != b"IHDR":
|
|
220
|
+
raise ValueError(f"expected: IHDR chunk; got: {ihdr.name!r}")
|
|
221
|
+
|
|
222
|
+
(width, height, bit_depth, color_type, compression, filter, interlace) = unpack(">IIBBBBB", ihdr.data)
|
|
223
|
+
return width, height
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _remove_png_chunks(names: Iterable[str], source_file: BinaryIO, target_file: BinaryIO) -> None:
|
|
227
|
+
"""
|
|
228
|
+
Rewrites a PNG file by removing chunks with the specified names.
|
|
229
|
+
|
|
230
|
+
:param source_file: A binary file opened for reading that contains PNG image data.
|
|
231
|
+
:param target_file: A binary file opened for writing to receive PNG image data.
|
|
232
|
+
"""
|
|
233
|
+
|
|
234
|
+
exclude_set = set(name.encode("ascii") for name in names)
|
|
235
|
+
|
|
236
|
+
_read_signature(source_file)
|
|
237
|
+
target_file.write(b"\x89PNG\r\n\x1a\n")
|
|
238
|
+
|
|
239
|
+
while True:
|
|
240
|
+
chunk = _read_chunk(source_file)
|
|
241
|
+
if chunk is None:
|
|
242
|
+
break
|
|
243
|
+
|
|
244
|
+
if chunk.name not in exclude_set:
|
|
245
|
+
_write_chunk(target_file, chunk)
|
md2conf/local.py
CHANGED
|
@@ -83,9 +83,9 @@ class LocalProcessor(Processor):
|
|
|
83
83
|
os.makedirs(csf_dir, exist_ok=True)
|
|
84
84
|
with open(csf_path, "w", encoding="utf-8") as f:
|
|
85
85
|
f.write(content)
|
|
86
|
-
for name,
|
|
86
|
+
for name, file_data in document.embedded_files.items():
|
|
87
87
|
with open(csf_dir / name, "wb") as f:
|
|
88
|
-
f.write(data)
|
|
88
|
+
f.write(file_data.data)
|
|
89
89
|
|
|
90
90
|
|
|
91
91
|
class LocalProcessorFactory(ProcessorFactory):
|
md2conf/markdown.py
CHANGED
|
@@ -28,18 +28,19 @@ def _emoji_generator(
|
|
|
28
28
|
"""
|
|
29
29
|
|
|
30
30
|
name = (alias or shortname).strip(":")
|
|
31
|
-
|
|
31
|
+
emoji = xml.etree.ElementTree.Element("x-emoji", {"data-shortname": name})
|
|
32
32
|
if uc is not None:
|
|
33
|
-
|
|
33
|
+
emoji.attrib["data-unicode"] = uc
|
|
34
34
|
|
|
35
35
|
# convert series of Unicode code point hexadecimal values into characters
|
|
36
|
-
|
|
36
|
+
emoji.text = "".join(chr(int(item, base=16)) for item in uc.split("-"))
|
|
37
37
|
else:
|
|
38
|
-
|
|
39
|
-
return span
|
|
38
|
+
emoji.text = alt
|
|
40
39
|
|
|
40
|
+
return emoji
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
def _verbatim_formatter(
|
|
43
44
|
source: str,
|
|
44
45
|
language: str,
|
|
45
46
|
css_class: str,
|
|
@@ -51,7 +52,9 @@ def _math_formatter(
|
|
|
51
52
|
**kwargs: Any,
|
|
52
53
|
) -> str:
|
|
53
54
|
"""
|
|
54
|
-
Custom formatter for
|
|
55
|
+
Custom formatter for `pymdownx.superfences`.
|
|
56
|
+
|
|
57
|
+
Used by language `math` (a.k.a. `pymdownx.arithmatex`) and pseudo-language `csf` (Confluence Storage Format pass-through).
|
|
55
58
|
"""
|
|
56
59
|
|
|
57
60
|
if classes is None:
|
|
@@ -73,9 +76,11 @@ _CONVERTER = markdown.Markdown(
|
|
|
73
76
|
"markdown.extensions.tables",
|
|
74
77
|
"md_in_html",
|
|
75
78
|
"pymdownx.arithmatex",
|
|
79
|
+
"pymdownx.caret",
|
|
76
80
|
"pymdownx.emoji",
|
|
77
81
|
"pymdownx.highlight", # required by `pymdownx.superfences`
|
|
78
82
|
"pymdownx.magiclink",
|
|
83
|
+
"pymdownx.mark",
|
|
79
84
|
"pymdownx.superfences",
|
|
80
85
|
"pymdownx.tilde",
|
|
81
86
|
"sane_lists",
|
|
@@ -83,13 +88,16 @@ _CONVERTER = markdown.Markdown(
|
|
|
83
88
|
extension_configs={
|
|
84
89
|
"footnotes": {"BACKLINK_TITLE": ""},
|
|
85
90
|
"pymdownx.arithmatex": {"generic": True, "preview": False, "tex_inline_wrap": ["", ""], "tex_block_wrap": ["", ""]},
|
|
86
|
-
"pymdownx.emoji": {
|
|
87
|
-
"emoji_generator": _emoji_generator,
|
|
88
|
-
},
|
|
91
|
+
"pymdownx.emoji": {"emoji_generator": _emoji_generator},
|
|
89
92
|
"pymdownx.highlight": {
|
|
90
93
|
"use_pygments": False,
|
|
91
94
|
},
|
|
92
|
-
"pymdownx.superfences": {
|
|
95
|
+
"pymdownx.superfences": {
|
|
96
|
+
"custom_fences": [
|
|
97
|
+
{"name": "math", "class": "arithmatex", "format": _verbatim_formatter},
|
|
98
|
+
{"name": "csf", "class": "csf", "format": _verbatim_formatter},
|
|
99
|
+
]
|
|
100
|
+
},
|
|
93
101
|
},
|
|
94
102
|
)
|
|
95
103
|
|
md2conf/mermaid.py
CHANGED
|
@@ -47,14 +47,12 @@ def has_mmdc() -> bool:
|
|
|
47
47
|
def render_diagram(source: str, output_format: Literal["png", "svg"] = "png") -> bytes:
|
|
48
48
|
"Generates a PNG or SVG image from a Mermaid diagram source."
|
|
49
49
|
|
|
50
|
-
filename = f"tmp_mermaid.{output_format}"
|
|
51
|
-
|
|
52
50
|
cmd = [
|
|
53
51
|
get_mmdc(),
|
|
54
52
|
"--input",
|
|
55
53
|
"-",
|
|
56
54
|
"--output",
|
|
57
|
-
|
|
55
|
+
"-",
|
|
58
56
|
"--outputFormat",
|
|
59
57
|
output_format,
|
|
60
58
|
"--backgroundColor",
|
|
@@ -66,27 +64,23 @@ def render_diagram(source: str, output_format: Literal["png", "svg"] = "png") ->
|
|
|
66
64
|
if is_docker():
|
|
67
65
|
cmd.extend(["-p", os.path.join(root, "puppeteer-config.json")])
|
|
68
66
|
LOGGER.debug("Executing: %s", " ".join(cmd))
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
finally:
|
|
91
|
-
if os.path.exists(filename):
|
|
92
|
-
os.remove(filename)
|
|
67
|
+
|
|
68
|
+
proc = subprocess.Popen(
|
|
69
|
+
cmd,
|
|
70
|
+
stdout=subprocess.PIPE,
|
|
71
|
+
stdin=subprocess.PIPE,
|
|
72
|
+
stderr=subprocess.PIPE,
|
|
73
|
+
text=False,
|
|
74
|
+
)
|
|
75
|
+
stdout, stderr = proc.communicate(input=source.encode("utf-8"))
|
|
76
|
+
if proc.returncode:
|
|
77
|
+
messages = [f"failed to convert Mermaid diagram; exit code: {proc.returncode}"]
|
|
78
|
+
console_output = stdout.decode("utf-8")
|
|
79
|
+
if console_output:
|
|
80
|
+
messages.append(f"output:\n{console_output}")
|
|
81
|
+
console_error = stderr.decode("utf-8")
|
|
82
|
+
if console_error:
|
|
83
|
+
messages.append(f"error:\n{console_error}")
|
|
84
|
+
raise RuntimeError("\n".join(messages))
|
|
85
|
+
|
|
86
|
+
return stdout
|