markdown-to-confluence 0.4.4__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.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/METADATA +51 -23
- markdown_to_confluence-0.4.5.dist-info/RECORD +33 -0
- {markdown_to_confluence-0.4.4.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 +6 -0
- md2conf/application.py +14 -8
- md2conf/converter.py +522 -272
- md2conf/csf.py +66 -0
- md2conf/domain.py +2 -0
- md2conf/drawio.py +18 -14
- md2conf/latex.py +245 -0
- md2conf/local.py +2 -2
- md2conf/markdown.py +3 -1
- md2conf/mermaid.py +21 -27
- md2conf/text.py +54 -0
- md2conf/xml.py +37 -0
- markdown_to_confluence-0.4.4.dist-info/RECORD +0 -31
- {markdown_to_confluence-0.4.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/WHEEL +0 -0
- {markdown_to_confluence-0.4.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/entry_points.txt +0 -0
- {markdown_to_confluence-0.4.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/top_level.txt +0 -0
- {markdown_to_confluence-0.4.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/zip-safe +0 -0
md2conf/csf.py
CHANGED
|
@@ -149,3 +149,69 @@ def elements_to_string(root: ET._Element) -> str:
|
|
|
149
149
|
return m.group(1)
|
|
150
150
|
else:
|
|
151
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
|
@@ -76,9 +76,11 @@ _CONVERTER = markdown.Markdown(
|
|
|
76
76
|
"markdown.extensions.tables",
|
|
77
77
|
"md_in_html",
|
|
78
78
|
"pymdownx.arithmatex",
|
|
79
|
+
"pymdownx.caret",
|
|
79
80
|
"pymdownx.emoji",
|
|
80
81
|
"pymdownx.highlight", # required by `pymdownx.superfences`
|
|
81
82
|
"pymdownx.magiclink",
|
|
83
|
+
"pymdownx.mark",
|
|
82
84
|
"pymdownx.superfences",
|
|
83
85
|
"pymdownx.tilde",
|
|
84
86
|
"sane_lists",
|
|
@@ -86,7 +88,7 @@ _CONVERTER = markdown.Markdown(
|
|
|
86
88
|
extension_configs={
|
|
87
89
|
"footnotes": {"BACKLINK_TITLE": ""},
|
|
88
90
|
"pymdownx.arithmatex": {"generic": True, "preview": False, "tex_inline_wrap": ["", ""], "tex_block_wrap": ["", ""]},
|
|
89
|
-
"pymdownx.emoji": {"emoji_generator": _emoji_generator
|
|
91
|
+
"pymdownx.emoji": {"emoji_generator": _emoji_generator},
|
|
90
92
|
"pymdownx.highlight": {
|
|
91
93
|
"use_pygments": False,
|
|
92
94
|
},
|
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
|
md2conf/text.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
|
|
10
|
+
def wrap_text(text: str, line_length: int = 160) -> str:
|
|
11
|
+
"""
|
|
12
|
+
Wraps text by replacing individual whitespace characters with a linefeed such that lines in the output honor the specified line length.
|
|
13
|
+
|
|
14
|
+
:param text: Input text, optionally with existing UNIX line endings.
|
|
15
|
+
:param line_length: Desired line length.
|
|
16
|
+
:returns: Wrapped output text. Long words that exceed the specified line length are not broken.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
if line_length < 1:
|
|
20
|
+
raise ValueError("expected: line_length > 0")
|
|
21
|
+
|
|
22
|
+
input = text.encode("utf-8")
|
|
23
|
+
output = bytearray(len(input))
|
|
24
|
+
pos = 0
|
|
25
|
+
length = len(input)
|
|
26
|
+
|
|
27
|
+
while pos < length:
|
|
28
|
+
end = min(pos + line_length, length)
|
|
29
|
+
|
|
30
|
+
# find any linefeed already in input
|
|
31
|
+
left = pos
|
|
32
|
+
while left < end and input[left] != 0x0A:
|
|
33
|
+
left += 1
|
|
34
|
+
if left != end:
|
|
35
|
+
output[pos : left + 1] = input[pos : left + 1]
|
|
36
|
+
pos = left + 1 # include linefeed
|
|
37
|
+
continue
|
|
38
|
+
|
|
39
|
+
# find the nearest whitespace before end of line
|
|
40
|
+
right = end
|
|
41
|
+
while right > pos and input[right - 1] not in b"\t\v\f\r ":
|
|
42
|
+
right -= 1
|
|
43
|
+
|
|
44
|
+
if right == pos or end == length:
|
|
45
|
+
# no whitespace found or at end of input; copy the rest
|
|
46
|
+
output[pos:end] = input[pos:end]
|
|
47
|
+
pos = end
|
|
48
|
+
else:
|
|
49
|
+
# replace the whitespace with a newline
|
|
50
|
+
output[pos : right - 1] = input[pos : right - 1]
|
|
51
|
+
output[right - 1] = 0x0A # linefeed '\n'
|
|
52
|
+
pos = right # skip the whitespace (already replaced)
|
|
53
|
+
|
|
54
|
+
return output.decode("utf-8")
|
md2conf/xml.py
CHANGED
|
@@ -87,6 +87,8 @@ def is_xml_equal(
|
|
|
87
87
|
"""
|
|
88
88
|
Compare two XML documents for equivalence, ignoring leading/trailing whitespace differences and attribute definition order.
|
|
89
89
|
|
|
90
|
+
Elements may be excluded, in which case they compare equal to any element of the same type that has the same tail text.
|
|
91
|
+
|
|
90
92
|
:param tree1: XML document as an element tree.
|
|
91
93
|
:param tree2: XML document as an element tree.
|
|
92
94
|
:param skip_attributes: Attributes to exclude, in `{namespace}name` notation.
|
|
@@ -101,3 +103,38 @@ def element_to_text(node: ET._Element) -> str:
|
|
|
101
103
|
"Returns all text contained in an element as a concatenated string."
|
|
102
104
|
|
|
103
105
|
return "".join(node.itertext()).strip()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def unwrap_substitute(name: str, root: ET._Element) -> None:
|
|
109
|
+
"""
|
|
110
|
+
Substitutes all occurrences of an element with its contents.
|
|
111
|
+
|
|
112
|
+
:param name: Element tag name to find and replace.
|
|
113
|
+
:param root: Top-most element at which to start.
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
for node in root.iterdescendants(name):
|
|
117
|
+
if node.text:
|
|
118
|
+
# append first piece of text in this element at the end of previous sibling, or text contained by parent
|
|
119
|
+
if (prev_node := node.getprevious()) is not None:
|
|
120
|
+
prev_node.tail = (prev_node.tail or "") + node.text
|
|
121
|
+
elif (parent_node := node.getparent()) is not None: # always true except for root
|
|
122
|
+
parent_node.text = (parent_node.text or "") + node.text
|
|
123
|
+
else:
|
|
124
|
+
raise NotImplementedError("must always have a previous sibling or a parent")
|
|
125
|
+
if node.tail:
|
|
126
|
+
if len(node) > 0:
|
|
127
|
+
# append text immediately following the closing tag of this element to the last child element of this element
|
|
128
|
+
last_node = node[-1]
|
|
129
|
+
last_node.tail = (last_node.tail or "") + node.tail
|
|
130
|
+
else: # node has no child elements, only text
|
|
131
|
+
if (prev_node := node.getprevious()) is not None:
|
|
132
|
+
prev_node.tail = (prev_node.tail or "") + node.tail
|
|
133
|
+
elif (parent_node := node.getparent()) is not None: # always true except for root
|
|
134
|
+
parent_node.text = (parent_node.text or "") + node.tail
|
|
135
|
+
else:
|
|
136
|
+
raise NotImplementedError("must always have a previous sibling or a parent")
|
|
137
|
+
for child in node.iterchildren(reversed=True):
|
|
138
|
+
node.addnext(child)
|
|
139
|
+
if (parent_node := node.getparent()) is not None: # always true except for root
|
|
140
|
+
parent_node.remove(node)
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
markdown_to_confluence-0.4.4.dist-info/licenses/LICENSE,sha256=Pv43so2bPfmKhmsrmXFyAvS7M30-1i1tzjz6-dfhyOo,1077
|
|
2
|
-
md2conf/__init__.py,sha256=nfPrvkkECt_Szpol9kTkli5cL-tFqFzn8JefKlCyb3s,402
|
|
3
|
-
md2conf/__main__.py,sha256=RImfFrO2m9C5iebmBrHKlLjosy_A8AY4O7PK9CmiWSw,11120
|
|
4
|
-
md2conf/api.py,sha256=-UrtFi3pFOVMsYj7VTgCqHIBinHsSLUucmGSeureEaE,37253
|
|
5
|
-
md2conf/application.py,sha256=MBV4Lg6s40Q0Z4SK7W9cSo-k2A-ckmu8wosuwo_2XWM,8301
|
|
6
|
-
md2conf/collection.py,sha256=EobgMRJgkYloWlY03NZJ52MRC_SGLpTVCHkltDbQyt0,837
|
|
7
|
-
md2conf/converter.py,sha256=WNfxGEacibINQYCZXszaMoUvJIP4Rq8IDMjz5RSH0IY,51162
|
|
8
|
-
md2conf/csf.py,sha256=tEbl3e5UQmbRSQK3lQqmpADoy638tlJihutqJoWhumA,4374
|
|
9
|
-
md2conf/domain.py,sha256=tA9V0vb5Vo9Nt0eQvwAFARaM9TX88LBVQ73nVvdcaqA,1851
|
|
10
|
-
md2conf/drawio.py,sha256=P_t7Wp7Tg9XkZM2ZchWCWWEdBaU1KgZ_YX9ZlkZo4Dk,8293
|
|
11
|
-
md2conf/entities.dtd,sha256=M6NzqL5N7dPs_eUA_6sDsiSLzDaAacrx9LdttiufvYU,30215
|
|
12
|
-
md2conf/extra.py,sha256=VuMxuOnnC2Qwy6y52ukIxsaYhrZArRqMmRHRE4QZl8g,687
|
|
13
|
-
md2conf/local.py,sha256=mUe2QBHhdZesygmYq5kXyGyl_ZmfUECgxDxylQwTa7g,3732
|
|
14
|
-
md2conf/markdown.py,sha256=z9sVTA_Hcn551DZgdO7Nbod-IZ-43B4eJLklrYQ2KYI,3141
|
|
15
|
-
md2conf/matcher.py,sha256=m5rZjYZSjhKfdeKS8JdPq7cG861Mc6rVZBkrIOZTHGE,6916
|
|
16
|
-
md2conf/mermaid.py,sha256=f0x7ISj-41ZMh4zTAFPhIWwr94SDcsVZUc1NWqmH_G4,2508
|
|
17
|
-
md2conf/metadata.py,sha256=LzZM-oPNnzCULmLhF516tPlV5zZBknccwMHt8Nan-xg,1007
|
|
18
|
-
md2conf/processor.py,sha256=z2d2KMPEYWaxflOtH2UTwrjzpPU8TtLSEUvor85ez1Q,9732
|
|
19
|
-
md2conf/properties.py,sha256=RC1jY_TKVbOv2bJxXn27Fj4fNWzyoNUQt6ltgUyVQAQ,3987
|
|
20
|
-
md2conf/puppeteer-config.json,sha256=-dMTAN_7kNTGbDlfXzApl0KJpAWna9YKZdwMKbpOb60,159
|
|
21
|
-
md2conf/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
22
|
-
md2conf/scanner.py,sha256=Cyvjab8tBvKgubttQvNagS8nailuTvFBqUGoiX5MNp8,5351
|
|
23
|
-
md2conf/toc.py,sha256=hpqqDbFgNJg5-ul8qWjOglI3Am0sbwR-TLwGN5G9Qo0,2447
|
|
24
|
-
md2conf/uri.py,sha256=KbLBdRFtZTQTZd8b4j0LtE8Pb68Ly0WkemF4iW-EAB4,1158
|
|
25
|
-
md2conf/xml.py,sha256=ERkGLuJgVFrOkzsMLd1RHU5SGFfFSxxNFZAeKrfdtRs,3454
|
|
26
|
-
markdown_to_confluence-0.4.4.dist-info/METADATA,sha256=Ql8vwKX6wtZNtUVoT0z35HgiFQcW-fFWsWFUMLkNErw,32306
|
|
27
|
-
markdown_to_confluence-0.4.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
28
|
-
markdown_to_confluence-0.4.4.dist-info/entry_points.txt,sha256=F1zxa1wtEObtbHS-qp46330WVFLHdMnV2wQ-ZorRmX0,50
|
|
29
|
-
markdown_to_confluence-0.4.4.dist-info/top_level.txt,sha256=_FJfl_kHrHNidyjUOuS01ngu_jDsfc-ZjSocNRJnTzU,8
|
|
30
|
-
markdown_to_confluence-0.4.4.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
31
|
-
markdown_to_confluence-0.4.4.dist-info/RECORD,,
|
|
File without changes
|
{markdown_to_confluence-0.4.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{markdown_to_confluence-0.4.4.dist-info → markdown_to_confluence-0.4.5.dist-info}/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|