markdown-to-confluence 0.4.0__py3-none-any.whl → 0.4.2__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.0.dist-info → markdown_to_confluence-0.4.2.dist-info}/METADATA +133 -43
- markdown_to_confluence-0.4.2.dist-info/RECORD +27 -0
- md2conf/__init__.py +1 -1
- md2conf/__main__.py +57 -18
- md2conf/api.py +242 -125
- md2conf/application.py +40 -48
- md2conf/collection.py +17 -11
- md2conf/converter.py +540 -107
- md2conf/drawio.py +222 -0
- md2conf/extra.py +13 -0
- md2conf/local.py +5 -12
- md2conf/matcher.py +64 -7
- md2conf/mermaid.py +2 -7
- md2conf/metadata.py +2 -0
- md2conf/processor.py +48 -57
- md2conf/properties.py +45 -12
- md2conf/scanner.py +17 -9
- md2conf/xml.py +70 -0
- markdown_to_confluence-0.4.0.dist-info/RECORD +0 -25
- {markdown_to_confluence-0.4.0.dist-info → markdown_to_confluence-0.4.2.dist-info}/WHEEL +0 -0
- {markdown_to_confluence-0.4.0.dist-info → markdown_to_confluence-0.4.2.dist-info}/entry_points.txt +0 -0
- {markdown_to_confluence-0.4.0.dist-info → markdown_to_confluence-0.4.2.dist-info}/licenses/LICENSE +0 -0
- {markdown_to_confluence-0.4.0.dist-info → markdown_to_confluence-0.4.2.dist-info}/top_level.txt +0 -0
- {markdown_to_confluence-0.4.0.dist-info → markdown_to_confluence-0.4.2.dist-info}/zip-safe +0 -0
md2conf/drawio.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
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 base64
|
|
10
|
+
import typing
|
|
11
|
+
import zlib
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from struct import unpack
|
|
14
|
+
from urllib.parse import unquote_to_bytes
|
|
15
|
+
|
|
16
|
+
import lxml.etree as ET
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class DrawioError(ValueError):
|
|
20
|
+
"""
|
|
21
|
+
Raised when the input does not adhere to the draw.io document format, or processing the input into a draw.io diagram fails.
|
|
22
|
+
|
|
23
|
+
Examples include:
|
|
24
|
+
|
|
25
|
+
* invalid or corrupt PNG file
|
|
26
|
+
* PNG chunk with embedded diagram data not found
|
|
27
|
+
* the structure of the outer XML does not match the expected format
|
|
28
|
+
* URL decoding error
|
|
29
|
+
* decompression error during INFLATE
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def inflate(data: bytes) -> bytes:
|
|
34
|
+
"""
|
|
35
|
+
Decompresses (inflates) data compressed using the raw DEFLATE algorithm.
|
|
36
|
+
|
|
37
|
+
:param data: Compressed data using raw DEFLATE format.
|
|
38
|
+
:returns: Uncompressed data.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
# -zlib.MAX_WBITS indicates raw DEFLATE stream (no zlib/gzip headers)
|
|
42
|
+
return zlib.decompress(data, -zlib.MAX_WBITS)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def decompress_diagram(xml_data: typing.Union[bytes, str]) -> ET._Element:
|
|
46
|
+
"""
|
|
47
|
+
Decompresses the text content of the `<diagram>` element in a draw.io XML document.
|
|
48
|
+
|
|
49
|
+
If the data is not compressed, the de-serialized XML element tree is returned.
|
|
50
|
+
|
|
51
|
+
Expected input (as `bytes` or `str`):
|
|
52
|
+
```
|
|
53
|
+
<mxfile>
|
|
54
|
+
<diagram>... ENCODED_COMPRESSED_DATA ...</diagram>
|
|
55
|
+
</mxfile>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Output (as XML element tree):
|
|
59
|
+
```
|
|
60
|
+
<mxfile>
|
|
61
|
+
<diagram>
|
|
62
|
+
<mxGraphModel>
|
|
63
|
+
<root>
|
|
64
|
+
...
|
|
65
|
+
</root>
|
|
66
|
+
</mxGraphModel>
|
|
67
|
+
</diagram>
|
|
68
|
+
</mxfile>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
:param xml_data: The serialized XML document.
|
|
72
|
+
:returns: XML element tree with the text contained within the `<diagram>` element expanded into a sub-tree.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
try:
|
|
76
|
+
root = ET.fromstring(xml_data)
|
|
77
|
+
except ET.ParseError as e:
|
|
78
|
+
raise DrawioError("invalid outer XML") from e
|
|
79
|
+
|
|
80
|
+
if root.tag != "mxfile":
|
|
81
|
+
raise DrawioError("root element is not `<mxfile>`")
|
|
82
|
+
|
|
83
|
+
diagram_elem = root.find("diagram")
|
|
84
|
+
if diagram_elem is None:
|
|
85
|
+
raise DrawioError("`<diagram>` element not found")
|
|
86
|
+
|
|
87
|
+
if len(diagram_elem) > 0:
|
|
88
|
+
# already decompressed
|
|
89
|
+
return root
|
|
90
|
+
|
|
91
|
+
if diagram_elem.text is None:
|
|
92
|
+
raise DrawioError("`<diagram>` element has no data")
|
|
93
|
+
|
|
94
|
+
# reverse base64-encoding of inner data
|
|
95
|
+
try:
|
|
96
|
+
base64_decoded = base64.b64decode(diagram_elem.text, validate=True)
|
|
97
|
+
except ValueError as e:
|
|
98
|
+
raise DrawioError("raw text data in `<diagram>` element is not properly Base64-encoded") from e
|
|
99
|
+
|
|
100
|
+
# decompress inner data
|
|
101
|
+
try:
|
|
102
|
+
embedded_data = inflate(base64_decoded)
|
|
103
|
+
except zlib.error as e:
|
|
104
|
+
raise DrawioError("`<diagram>` element text data cannot be decompressed using INFLATE") from e
|
|
105
|
+
|
|
106
|
+
# reverse URL-encoding of inner data
|
|
107
|
+
try:
|
|
108
|
+
url_decoded = unquote_to_bytes(embedded_data)
|
|
109
|
+
except ValueError as e:
|
|
110
|
+
raise DrawioError("decompressed data in `<diagram>` element is not properly URL-encoded") from e
|
|
111
|
+
|
|
112
|
+
# create sub-tree from decompressed data
|
|
113
|
+
try:
|
|
114
|
+
tree = ET.fromstring(url_decoded)
|
|
115
|
+
except ET.ParseError as e:
|
|
116
|
+
raise DrawioError("invalid inner XML extracted from `<diagram>` element") from e
|
|
117
|
+
|
|
118
|
+
# update document
|
|
119
|
+
diagram_elem.text = None
|
|
120
|
+
diagram_elem.append(tree)
|
|
121
|
+
|
|
122
|
+
return root
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def extract_xml_from_png(png_data: bytes) -> ET._Element:
|
|
126
|
+
"""
|
|
127
|
+
Extracts an editable draw.io diagram from a PNG file.
|
|
128
|
+
|
|
129
|
+
:param png_data: PNG binary data, with an embedded draw.io diagram.
|
|
130
|
+
:returns: XML element tree of a draw.io diagram.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
# PNG signature is always the first 8 bytes
|
|
134
|
+
png_signature = b"\x89PNG\r\n\x1a\n"
|
|
135
|
+
if not png_data.startswith(png_signature):
|
|
136
|
+
raise DrawioError("not a valid PNG file")
|
|
137
|
+
|
|
138
|
+
offset = len(png_signature)
|
|
139
|
+
while offset < len(png_data):
|
|
140
|
+
if offset + 8 > len(png_data):
|
|
141
|
+
raise DrawioError("corrupted PNG: incomplete chunk header")
|
|
142
|
+
|
|
143
|
+
# read chunk length (4 bytes) and type (4 bytes)
|
|
144
|
+
(length,) = unpack(">I", png_data[offset : offset + 4])
|
|
145
|
+
chunk_type = png_data[offset + 4 : offset + 8]
|
|
146
|
+
offset += 8
|
|
147
|
+
|
|
148
|
+
if offset + length + 4 > len(png_data):
|
|
149
|
+
raise DrawioError(f"corrupted PNG: incomplete data for chunk {chunk_type.decode('ascii')}")
|
|
150
|
+
|
|
151
|
+
# read chunk data
|
|
152
|
+
chunk_data = png_data[offset : offset + length]
|
|
153
|
+
offset += length
|
|
154
|
+
|
|
155
|
+
# skip CRC (4 bytes)
|
|
156
|
+
offset += 4
|
|
157
|
+
|
|
158
|
+
# extracts draw.io diagram data from a `tEXt` chunk with the keyword `mxfile` embedded in a PNG
|
|
159
|
+
if chunk_type != b"tEXt":
|
|
160
|
+
continue
|
|
161
|
+
|
|
162
|
+
# format: keyword\0text
|
|
163
|
+
null_pos = chunk_data.find(b"\x00")
|
|
164
|
+
if null_pos < 0:
|
|
165
|
+
raise DrawioError("corrupted PNG: tEXt chunk missing keyword")
|
|
166
|
+
|
|
167
|
+
keyword = chunk_data[:null_pos].decode("latin1")
|
|
168
|
+
if keyword != "mxfile":
|
|
169
|
+
continue
|
|
170
|
+
|
|
171
|
+
textual_data = chunk_data[null_pos + 1 :]
|
|
172
|
+
|
|
173
|
+
try:
|
|
174
|
+
url_decoded = unquote_to_bytes(textual_data)
|
|
175
|
+
except ValueError as e:
|
|
176
|
+
raise DrawioError("data in `tEXt` chunk is not properly URL-encoded") from e
|
|
177
|
+
|
|
178
|
+
# decompress data embedded in the outer XML wrapper
|
|
179
|
+
return decompress_diagram(url_decoded)
|
|
180
|
+
|
|
181
|
+
# matching `tEXt` chunk not found
|
|
182
|
+
raise DrawioError("not a PNG file made with draw.io")
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def extract_xml_from_svg(svg_data: bytes) -> ET._Element:
|
|
186
|
+
"""
|
|
187
|
+
Extracts an editable draw.io diagram from an SVG file.
|
|
188
|
+
|
|
189
|
+
:param svg_data: SVG XML data, with an embedded draw.io diagram.
|
|
190
|
+
:returns: XML element tree of a draw.io diagram.
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
try:
|
|
194
|
+
root = ET.fromstring(svg_data)
|
|
195
|
+
except ET.ParseError as e:
|
|
196
|
+
raise DrawioError("invalid SVG XML") from e
|
|
197
|
+
|
|
198
|
+
content = root.attrib.get("content")
|
|
199
|
+
if content is None:
|
|
200
|
+
raise DrawioError("SVG root element has no attribute `content`")
|
|
201
|
+
|
|
202
|
+
return decompress_diagram(content)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def extract_diagram(path: Path) -> bytes:
|
|
206
|
+
"""
|
|
207
|
+
Extracts an editable draw.io diagram from a PNG file.
|
|
208
|
+
|
|
209
|
+
:param path: Path to a PNG or SVG file with an embedded draw.io diagram.
|
|
210
|
+
:returns: XML data of a draw.io diagram as bytes.
|
|
211
|
+
"""
|
|
212
|
+
|
|
213
|
+
if path.name.endswith(".drawio.png"):
|
|
214
|
+
with open(path, "rb") as png_file:
|
|
215
|
+
root = extract_xml_from_png(png_file.read())
|
|
216
|
+
elif path.name.endswith(".drawio.svg"):
|
|
217
|
+
with open(path, "rb") as svg_file:
|
|
218
|
+
root = extract_xml_from_svg(svg_file.read())
|
|
219
|
+
else:
|
|
220
|
+
raise DrawioError(f"unrecognized file type for {path.name}")
|
|
221
|
+
|
|
222
|
+
return ET.tostring(root, encoding="utf8", method="xml")
|
md2conf/extra.py
CHANGED
|
@@ -12,3 +12,16 @@ if sys.version_info >= (3, 12):
|
|
|
12
12
|
from typing import override as override # noqa: F401
|
|
13
13
|
else:
|
|
14
14
|
from typing_extensions import override as override # noqa: F401
|
|
15
|
+
|
|
16
|
+
if sys.version_info >= (3, 12):
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
def path_relative_to(destination: Path, origin: Path) -> Path:
|
|
20
|
+
return destination.relative_to(origin, walk_up=True)
|
|
21
|
+
|
|
22
|
+
else:
|
|
23
|
+
import os.path
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
def path_relative_to(destination: Path, origin: Path) -> Path:
|
|
27
|
+
return Path(os.path.relpath(destination, start=origin))
|
md2conf/local.py
CHANGED
|
@@ -45,9 +45,7 @@ class LocalProcessor(Processor):
|
|
|
45
45
|
self.out_dir = out_dir or root_dir
|
|
46
46
|
|
|
47
47
|
@override
|
|
48
|
-
def _synchronize_tree(
|
|
49
|
-
self, root: DocumentNode, root_id: Optional[ConfluencePageID]
|
|
50
|
-
) -> None:
|
|
48
|
+
def _synchronize_tree(self, root: DocumentNode, root_id: Optional[ConfluencePageID]) -> None:
|
|
51
49
|
"""
|
|
52
50
|
Creates the cross-reference index.
|
|
53
51
|
|
|
@@ -59,9 +57,7 @@ class LocalProcessor(Processor):
|
|
|
59
57
|
page_id = node.page_id
|
|
60
58
|
else:
|
|
61
59
|
digest = self._generate_hash(node.absolute_path)
|
|
62
|
-
LOGGER.info(
|
|
63
|
-
"Identifier %s assigned to page: %s", digest, node.absolute_path
|
|
64
|
-
)
|
|
60
|
+
LOGGER.info("Identifier %s assigned to page: %s", digest, node.absolute_path)
|
|
65
61
|
page_id = digest
|
|
66
62
|
|
|
67
63
|
self.page_metadata.add(
|
|
@@ -70,13 +66,12 @@ class LocalProcessor(Processor):
|
|
|
70
66
|
page_id=page_id,
|
|
71
67
|
space_key=node.space_key or self.site.space_key or "HOME",
|
|
72
68
|
title=node.title or "",
|
|
69
|
+
synchronized=node.synchronized,
|
|
73
70
|
),
|
|
74
71
|
)
|
|
75
72
|
|
|
76
73
|
@override
|
|
77
|
-
def _update_page(
|
|
78
|
-
self, page_id: ConfluencePageID, document: ConfluenceDocument, path: Path
|
|
79
|
-
) -> None:
|
|
74
|
+
def _update_page(self, page_id: ConfluencePageID, document: ConfluenceDocument, path: Path) -> None:
|
|
80
75
|
"""
|
|
81
76
|
Saves the document as Confluence Storage Format XHTML to the local disk.
|
|
82
77
|
"""
|
|
@@ -101,9 +96,7 @@ class LocalProcessorFactory(ProcessorFactory):
|
|
|
101
96
|
self.out_dir = out_dir
|
|
102
97
|
|
|
103
98
|
def create(self, root_dir: Path) -> Processor:
|
|
104
|
-
return LocalProcessor(
|
|
105
|
-
self.options, self.site, out_dir=self.out_dir, root_dir=root_dir
|
|
106
|
-
)
|
|
99
|
+
return LocalProcessor(self.options, self.site, out_dir=self.out_dir, root_dir=root_dir)
|
|
107
100
|
|
|
108
101
|
|
|
109
102
|
class LocalConverter(Converter):
|
md2conf/matcher.py
CHANGED
|
@@ -10,14 +10,57 @@ import os.path
|
|
|
10
10
|
from dataclasses import dataclass
|
|
11
11
|
from fnmatch import fnmatch
|
|
12
12
|
from pathlib import Path
|
|
13
|
-
from typing import Iterable, Optional, Union, overload
|
|
13
|
+
from typing import Iterable, Optional, Union, final, overload
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
@dataclass(frozen=True)
|
|
16
|
+
@dataclass(frozen=True, eq=True)
|
|
17
|
+
class _BaseEntry:
|
|
18
|
+
"""
|
|
19
|
+
Represents a file or directory entry.
|
|
20
|
+
|
|
21
|
+
Entries are primarily sorted alphabetically case-insensitive.
|
|
22
|
+
When two items are equal case-insensitive, conflicting items are put in case-sensitive order.
|
|
23
|
+
|
|
24
|
+
:param name: Name of the file-system entry.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
name: str
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def lower_name(self) -> str:
|
|
31
|
+
return self.name.lower()
|
|
32
|
+
|
|
33
|
+
def __lt__(self, other: "_BaseEntry") -> bool:
|
|
34
|
+
return (self.lower_name, self.name) < (other.lower_name, other.name)
|
|
35
|
+
|
|
36
|
+
def __le__(self, other: "_BaseEntry") -> bool:
|
|
37
|
+
return (self.lower_name, self.name) <= (other.lower_name, other.name)
|
|
38
|
+
|
|
39
|
+
def __ge__(self, other: "_BaseEntry") -> bool:
|
|
40
|
+
return (self.lower_name, self.name) >= (other.lower_name, other.name)
|
|
41
|
+
|
|
42
|
+
def __gt__(self, other: "_BaseEntry") -> bool:
|
|
43
|
+
return (self.lower_name, self.name) > (other.lower_name, other.name)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@final
|
|
47
|
+
class FileEntry(_BaseEntry):
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@final
|
|
52
|
+
class DirectoryEntry(_BaseEntry):
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, eq=True)
|
|
17
57
|
class Entry:
|
|
18
58
|
"""
|
|
19
59
|
Represents a file or directory entry.
|
|
20
60
|
|
|
61
|
+
When sorted, directories come before files and items are primarily arranged in alphabetical order case-insensitive.
|
|
62
|
+
When two items are equal case-insensitive, conflicting items are put in case-sensitive order.
|
|
63
|
+
|
|
21
64
|
:param name: Name of the file-system entry to match against the rule-set.
|
|
22
65
|
:param is_dir: True if the entry is a directory.
|
|
23
66
|
"""
|
|
@@ -25,6 +68,22 @@ class Entry:
|
|
|
25
68
|
name: str
|
|
26
69
|
is_dir: bool
|
|
27
70
|
|
|
71
|
+
@property
|
|
72
|
+
def lower_name(self) -> str:
|
|
73
|
+
return self.name.lower()
|
|
74
|
+
|
|
75
|
+
def __lt__(self, other: "Entry") -> bool:
|
|
76
|
+
return (not self.is_dir, self.lower_name, self.name) < (not other.is_dir, other.lower_name, other.name)
|
|
77
|
+
|
|
78
|
+
def __le__(self, other: "Entry") -> bool:
|
|
79
|
+
return (not self.is_dir, self.lower_name, self.name) <= (not other.is_dir, other.lower_name, other.name)
|
|
80
|
+
|
|
81
|
+
def __ge__(self, other: "Entry") -> bool:
|
|
82
|
+
return (not self.is_dir, self.lower_name, self.name) >= (not other.is_dir, other.lower_name, other.name)
|
|
83
|
+
|
|
84
|
+
def __gt__(self, other: "Entry") -> bool:
|
|
85
|
+
return (not self.is_dir, self.lower_name, self.name) > (not other.is_dir, other.lower_name, other.name)
|
|
86
|
+
|
|
28
87
|
|
|
29
88
|
@dataclass
|
|
30
89
|
class MatcherOptions:
|
|
@@ -146,9 +205,9 @@ class Matcher:
|
|
|
146
205
|
:returns: A filtered list of names that didn't match any of the exclusion rules.
|
|
147
206
|
"""
|
|
148
207
|
|
|
149
|
-
return
|
|
208
|
+
return sorted(entry for entry in entries if self.is_included(entry))
|
|
150
209
|
|
|
151
|
-
def
|
|
210
|
+
def listing(self, path: Path) -> list[Entry]:
|
|
152
211
|
"""
|
|
153
212
|
Returns only those entries in a directory whose name doesn't match any of the exclusion rules.
|
|
154
213
|
|
|
@@ -156,6 +215,4 @@ class Matcher:
|
|
|
156
215
|
:returns: A filtered list of entries whose name didn't match any of the exclusion rules.
|
|
157
216
|
"""
|
|
158
217
|
|
|
159
|
-
return self.filter(
|
|
160
|
-
Entry(entry.name, entry.is_dir()) for entry in os.scandir(path)
|
|
161
|
-
)
|
|
218
|
+
return self.filter(Entry(entry.name, entry.is_dir()) for entry in os.scandir(path))
|
md2conf/mermaid.py
CHANGED
|
@@ -19,10 +19,7 @@ LOGGER = logging.getLogger(__name__)
|
|
|
19
19
|
def is_docker() -> bool:
|
|
20
20
|
"True if the application is running in a Docker container."
|
|
21
21
|
|
|
22
|
-
return (
|
|
23
|
-
os.environ.get("CHROME_BIN") == "/usr/bin/chromium-browser"
|
|
24
|
-
and os.environ.get("PUPPETEER_SKIP_DOWNLOAD") == "true"
|
|
25
|
-
)
|
|
22
|
+
return os.environ.get("CHROME_BIN") == "/usr/bin/chromium-browser" and os.environ.get("PUPPETEER_SKIP_DOWNLOAD") == "true"
|
|
26
23
|
|
|
27
24
|
|
|
28
25
|
def get_mmdc() -> str:
|
|
@@ -79,9 +76,7 @@ def render_diagram(source: str, output_format: Literal["png", "svg"] = "png") ->
|
|
|
79
76
|
)
|
|
80
77
|
stdout, stderr = proc.communicate(input=source.encode("utf-8"))
|
|
81
78
|
if proc.returncode:
|
|
82
|
-
messages = [
|
|
83
|
-
f"failed to convert Mermaid diagram; exit code: {proc.returncode}"
|
|
84
|
-
]
|
|
79
|
+
messages = [f"failed to convert Mermaid diagram; exit code: {proc.returncode}"]
|
|
85
80
|
console_output = stdout.decode("utf-8")
|
|
86
81
|
if console_output:
|
|
87
82
|
messages.append(f"output:\n{console_output}")
|
md2conf/metadata.py
CHANGED
|
@@ -33,8 +33,10 @@ class ConfluencePageMetadata:
|
|
|
33
33
|
:param page_id: Confluence page ID.
|
|
34
34
|
:param space_key: Confluence space key.
|
|
35
35
|
:param title: Document title.
|
|
36
|
+
:param synchronized: True if the document content is parsed and synchronized with Confluence.
|
|
36
37
|
"""
|
|
37
38
|
|
|
38
39
|
page_id: str
|
|
39
40
|
space_key: str
|
|
40
41
|
title: str
|
|
42
|
+
synchronized: bool
|
md2conf/processor.py
CHANGED
|
@@ -15,7 +15,7 @@ from typing import Iterable, Optional
|
|
|
15
15
|
|
|
16
16
|
from .collection import ConfluencePageCollection
|
|
17
17
|
from .converter import ConfluenceDocument, ConfluenceDocumentOptions, ConfluencePageID
|
|
18
|
-
from .matcher import Matcher, MatcherOptions
|
|
18
|
+
from .matcher import DirectoryEntry, FileEntry, Matcher, MatcherOptions
|
|
19
19
|
from .metadata import ConfluenceSiteMetadata
|
|
20
20
|
from .properties import ArgumentError
|
|
21
21
|
from .scanner import Scanner
|
|
@@ -28,6 +28,7 @@ class DocumentNode:
|
|
|
28
28
|
page_id: Optional[str]
|
|
29
29
|
space_key: Optional[str]
|
|
30
30
|
title: Optional[str]
|
|
31
|
+
synchronized: bool
|
|
31
32
|
|
|
32
33
|
_children: list["DocumentNode"]
|
|
33
34
|
|
|
@@ -35,13 +36,15 @@ class DocumentNode:
|
|
|
35
36
|
self,
|
|
36
37
|
absolute_path: Path,
|
|
37
38
|
page_id: Optional[str],
|
|
38
|
-
space_key: Optional[str]
|
|
39
|
-
title: Optional[str]
|
|
39
|
+
space_key: Optional[str],
|
|
40
|
+
title: Optional[str],
|
|
41
|
+
synchronized: bool,
|
|
40
42
|
):
|
|
41
43
|
self.absolute_path = absolute_path
|
|
42
44
|
self.page_id = page_id
|
|
43
45
|
self.space_key = space_key
|
|
44
46
|
self.title = title
|
|
47
|
+
self.synchronized = synchronized
|
|
45
48
|
self._children = []
|
|
46
49
|
|
|
47
50
|
def count(self) -> int:
|
|
@@ -98,16 +101,11 @@ class Processor:
|
|
|
98
101
|
local_dir = local_dir.resolve(True)
|
|
99
102
|
LOGGER.info("Processing directory: %s", local_dir)
|
|
100
103
|
|
|
101
|
-
#
|
|
104
|
+
# build index of all Markdown files in directory hierarchy
|
|
102
105
|
root = self._index_directory(local_dir, None)
|
|
103
106
|
LOGGER.info("Indexed %d document(s)", root.count())
|
|
104
107
|
|
|
105
|
-
|
|
106
|
-
self._synchronize_tree(root, self.options.root_page_id)
|
|
107
|
-
|
|
108
|
-
# Step 3: synchronize files in directory hierarchy with pages in space
|
|
109
|
-
for path, metadata in self.page_metadata.items():
|
|
110
|
-
self._synchronize_page(path, ConfluencePageID(metadata.page_id))
|
|
108
|
+
self._process_items(root)
|
|
111
109
|
|
|
112
110
|
def process_page(self, path: Path) -> None:
|
|
113
111
|
"""
|
|
@@ -115,31 +113,33 @@ class Processor:
|
|
|
115
113
|
"""
|
|
116
114
|
|
|
117
115
|
LOGGER.info("Processing page: %s", path)
|
|
118
|
-
|
|
119
|
-
# Step 1: parse Markdown file
|
|
120
116
|
root = self._index_file(path)
|
|
121
117
|
|
|
122
|
-
|
|
118
|
+
self._process_items(root)
|
|
119
|
+
|
|
120
|
+
def _process_items(self, root: DocumentNode) -> None:
|
|
121
|
+
"""
|
|
122
|
+
Processes a sub-tree rooted at an ancestor node.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
# synchronize directory tree structure with page hierarchy in space (find matching pages in Confluence)
|
|
123
126
|
self._synchronize_tree(root, self.options.root_page_id)
|
|
124
127
|
|
|
125
|
-
#
|
|
128
|
+
# synchronize files in directory hierarchy with pages in space
|
|
126
129
|
for path, metadata in self.page_metadata.items():
|
|
127
|
-
|
|
130
|
+
if metadata.synchronized:
|
|
131
|
+
self._synchronize_page(path, ConfluencePageID(metadata.page_id))
|
|
128
132
|
|
|
129
133
|
def _synchronize_page(self, path: Path, page_id: ConfluencePageID) -> None:
|
|
130
134
|
"""
|
|
131
135
|
Synchronizes a single Markdown document with its corresponding Confluence page.
|
|
132
136
|
"""
|
|
133
137
|
|
|
134
|
-
page_id, document = ConfluenceDocument.create(
|
|
135
|
-
path, self.options, self.root_dir, self.site, self.page_metadata
|
|
136
|
-
)
|
|
138
|
+
page_id, document = ConfluenceDocument.create(path, self.options, self.root_dir, self.site, self.page_metadata)
|
|
137
139
|
self._update_page(page_id, document, path)
|
|
138
140
|
|
|
139
141
|
@abstractmethod
|
|
140
|
-
def _synchronize_tree(
|
|
141
|
-
self, node: DocumentNode, page_id: Optional[ConfluencePageID]
|
|
142
|
-
) -> None:
|
|
142
|
+
def _synchronize_tree(self, node: DocumentNode, page_id: Optional[ConfluencePageID]) -> None:
|
|
143
143
|
"""
|
|
144
144
|
Creates the cross-reference index and synchronizes the directory tree structure with the Confluence page hierarchy.
|
|
145
145
|
|
|
@@ -150,17 +150,13 @@ class Processor:
|
|
|
150
150
|
...
|
|
151
151
|
|
|
152
152
|
@abstractmethod
|
|
153
|
-
def _update_page(
|
|
154
|
-
self, page_id: ConfluencePageID, document: ConfluenceDocument, path: Path
|
|
155
|
-
) -> None:
|
|
153
|
+
def _update_page(self, page_id: ConfluencePageID, document: ConfluenceDocument, path: Path) -> None:
|
|
156
154
|
"""
|
|
157
155
|
Saves the document as Confluence Storage Format XHTML.
|
|
158
156
|
"""
|
|
159
157
|
...
|
|
160
158
|
|
|
161
|
-
def _index_directory(
|
|
162
|
-
self, local_dir: Path, parent: Optional[DocumentNode]
|
|
163
|
-
) -> DocumentNode:
|
|
159
|
+
def _index_directory(self, local_dir: Path, parent: Optional[DocumentNode]) -> DocumentNode:
|
|
164
160
|
"""
|
|
165
161
|
Indexes Markdown files in a directory hierarchy recursively.
|
|
166
162
|
"""
|
|
@@ -169,36 +165,40 @@ class Processor:
|
|
|
169
165
|
|
|
170
166
|
matcher = Matcher(MatcherOptions(source=".mdignore", extension="md"), local_dir)
|
|
171
167
|
|
|
172
|
-
files: list[
|
|
173
|
-
directories: list[
|
|
168
|
+
files: list[FileEntry] = []
|
|
169
|
+
directories: list[DirectoryEntry] = []
|
|
174
170
|
for entry in os.scandir(local_dir):
|
|
175
171
|
if matcher.is_excluded(entry):
|
|
176
172
|
continue
|
|
177
173
|
|
|
178
174
|
if entry.is_file():
|
|
179
|
-
files.append(
|
|
175
|
+
files.append(FileEntry(entry.name))
|
|
180
176
|
elif entry.is_dir():
|
|
181
|
-
directories.append(
|
|
177
|
+
directories.append(DirectoryEntry(entry.name))
|
|
178
|
+
|
|
179
|
+
files.sort()
|
|
180
|
+
directories.sort()
|
|
182
181
|
|
|
183
182
|
# make page act as parent node
|
|
184
183
|
parent_doc: Optional[Path] = None
|
|
185
|
-
if (
|
|
186
|
-
parent_doc =
|
|
187
|
-
elif (
|
|
188
|
-
parent_doc =
|
|
189
|
-
elif (
|
|
190
|
-
parent_doc =
|
|
184
|
+
if FileEntry("index.md") in files:
|
|
185
|
+
parent_doc = local_dir / "index.md"
|
|
186
|
+
elif FileEntry("README.md") in files:
|
|
187
|
+
parent_doc = local_dir / "README.md"
|
|
188
|
+
elif FileEntry(f"{local_dir.name}.md") in files:
|
|
189
|
+
parent_doc = local_dir / f"{local_dir.name}.md"
|
|
191
190
|
|
|
192
191
|
if parent_doc is None and self.options.keep_hierarchy:
|
|
193
|
-
parent_doc =
|
|
192
|
+
parent_doc = local_dir / "index.md"
|
|
194
193
|
|
|
195
194
|
# create a blank page for directory entry
|
|
196
|
-
with open(parent_doc, "w"):
|
|
197
|
-
|
|
195
|
+
with open(parent_doc, "w") as f:
|
|
196
|
+
print("[[_LISTING_]]", file=f)
|
|
198
197
|
|
|
199
198
|
if parent_doc is not None:
|
|
200
|
-
|
|
201
|
-
|
|
199
|
+
parent_entry = FileEntry(parent_doc.name)
|
|
200
|
+
if parent_entry in files:
|
|
201
|
+
files.remove(parent_entry)
|
|
202
202
|
|
|
203
203
|
# promote Markdown document in directory as parent page in Confluence
|
|
204
204
|
node = self._index_file(parent_doc)
|
|
@@ -206,20 +206,14 @@ class Processor:
|
|
|
206
206
|
parent.add_child(node)
|
|
207
207
|
parent = node
|
|
208
208
|
elif parent is None:
|
|
209
|
-
|
|
210
|
-
if self.options.root_page_id is not None:
|
|
211
|
-
page_id = self.options.root_page_id.page_id
|
|
212
|
-
parent = DocumentNode(local_dir, page_id=page_id)
|
|
213
|
-
else:
|
|
214
|
-
# local use only, raises error with remote synchronization
|
|
215
|
-
parent = DocumentNode(local_dir, page_id=None)
|
|
209
|
+
raise ArgumentError(f"root page requires corresponding top-level Markdown document in {local_dir}")
|
|
216
210
|
|
|
217
211
|
for file in files:
|
|
218
|
-
node = self._index_file(file)
|
|
212
|
+
node = self._index_file(local_dir / Path(file.name))
|
|
219
213
|
parent.add_child(node)
|
|
220
214
|
|
|
221
215
|
for directory in directories:
|
|
222
|
-
self._index_directory(directory, parent)
|
|
216
|
+
self._index_directory(local_dir / Path(directory.name), parent)
|
|
223
217
|
|
|
224
218
|
return parent
|
|
225
219
|
|
|
@@ -238,6 +232,7 @@ class Processor:
|
|
|
238
232
|
page_id=document.page_id,
|
|
239
233
|
space_key=document.space_key,
|
|
240
234
|
title=document.title,
|
|
235
|
+
synchronized=document.synchronized if document.synchronized is not None else True,
|
|
241
236
|
)
|
|
242
237
|
|
|
243
238
|
def _generate_hash(self, absolute_path: Path) -> str:
|
|
@@ -254,9 +249,7 @@ class ProcessorFactory:
|
|
|
254
249
|
options: ConfluenceDocumentOptions
|
|
255
250
|
site: ConfluenceSiteMetadata
|
|
256
251
|
|
|
257
|
-
def __init__(
|
|
258
|
-
self, options: ConfluenceDocumentOptions, site: ConfluenceSiteMetadata
|
|
259
|
-
) -> None:
|
|
252
|
+
def __init__(self, options: ConfluenceDocumentOptions, site: ConfluenceSiteMetadata) -> None:
|
|
260
253
|
self.options = options
|
|
261
254
|
self.site = site
|
|
262
255
|
|
|
@@ -283,9 +276,7 @@ class Converter:
|
|
|
283
276
|
else:
|
|
284
277
|
raise ArgumentError(f"expected: valid file or directory path; got: {path}")
|
|
285
278
|
|
|
286
|
-
def process_directory(
|
|
287
|
-
self, local_dir: Path, root_dir: Optional[Path] = None
|
|
288
|
-
) -> None:
|
|
279
|
+
def process_directory(self, local_dir: Path, root_dir: Optional[Path] = None) -> None:
|
|
289
280
|
"""
|
|
290
281
|
Recursively scans a directory hierarchy for Markdown files, and processes each, resolving cross-references.
|
|
291
282
|
"""
|