wpsync 1.17.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.
- wpsync/__init__.py +3 -0
- wpsync/__main__.py +3 -0
- wpsync/cli.py +1479 -0
- wpsync/config.py +178 -0
- wpsync/documents.py +294 -0
- wpsync/editors.py +68 -0
- wpsync/markup/__init__.py +0 -0
- wpsync/markup/dom.py +371 -0
- wpsync/markup/markdown.py +526 -0
- wpsync/media.py +161 -0
- wpsync/posts.py +78 -0
- wpsync/pull.py +400 -0
- wpsync/push.py +288 -0
- wpsync/state.py +219 -0
- wpsync/storage.py +28 -0
- wpsync/sync.py +501 -0
- wpsync/trash.py +132 -0
- wpsync/wordpress/__init__.py +0 -0
- wpsync/wordpress/autop.py +114 -0
- wpsync/wordpress/client.py +233 -0
- wpsync/wordpress/content.py +602 -0
- wpsync/wordpress/remote.py +264 -0
- wpsync-1.17.2.dist-info/METADATA +148 -0
- wpsync-1.17.2.dist-info/RECORD +27 -0
- wpsync-1.17.2.dist-info/WHEEL +4 -0
- wpsync-1.17.2.dist-info/entry_points.txt +2 -0
- wpsync-1.17.2.dist-info/licenses/LICENSE +61 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
"""Post content: WordPress HTML to the post Markdown dialect and back.
|
|
2
|
+
|
|
3
|
+
Conversion is checked against a model of what WordPress displays. Content
|
|
4
|
+
with block markup is displayed block by block; content without it goes
|
|
5
|
+
through WordPress's `wpautop` filter. The stored post and the post a push
|
|
6
|
+
would send are both put through that model, and any part of the post whose
|
|
7
|
+
Markdown would not display the same is kept as its exact original source.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import re
|
|
14
|
+
from collections.abc import Iterator
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
|
|
17
|
+
from markdown_it.token import Token
|
|
18
|
+
|
|
19
|
+
from wpsync.markup.dom import (
|
|
20
|
+
HTML_SPACE,
|
|
21
|
+
Comment,
|
|
22
|
+
Element,
|
|
23
|
+
Node,
|
|
24
|
+
canonical,
|
|
25
|
+
parse_html,
|
|
26
|
+
to_html,
|
|
27
|
+
trim_block_whitespace,
|
|
28
|
+
)
|
|
29
|
+
from wpsync.markup.markdown import (
|
|
30
|
+
Converter,
|
|
31
|
+
Unconvertible,
|
|
32
|
+
join_blocks,
|
|
33
|
+
kept_markdown,
|
|
34
|
+
pieces,
|
|
35
|
+
render_inline,
|
|
36
|
+
render_tokens,
|
|
37
|
+
)
|
|
38
|
+
from wpsync.media import Media
|
|
39
|
+
from wpsync.posts import Conversion, UnrepresentableContent
|
|
40
|
+
from wpsync.wordpress.autop import BLOCK_NAMES, autop
|
|
41
|
+
|
|
42
|
+
EDITOR_CLASSES = frozenset(
|
|
43
|
+
{
|
|
44
|
+
"wp-block-heading",
|
|
45
|
+
"wp-block-list",
|
|
46
|
+
"wp-block-quote",
|
|
47
|
+
"wp-block-code",
|
|
48
|
+
"wp-block-separator",
|
|
49
|
+
"has-alpha-channel-opacity",
|
|
50
|
+
"wp-block-paragraph",
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
"""Classes the block editor adds to unstyled elements."""
|
|
54
|
+
|
|
55
|
+
EDITOR_LINK_ATTRS = frozenset({"data-type", "data-id"})
|
|
56
|
+
"""Attributes the editor adds to links for its own use; browsers ignore them."""
|
|
57
|
+
|
|
58
|
+
UPLOADS_PATH = "/wp-content/uploads/"
|
|
59
|
+
|
|
60
|
+
STATIC_BLOCKS = frozenset(
|
|
61
|
+
"core/" + name
|
|
62
|
+
for name in """paragraph heading list list-item quote code preformatted separator
|
|
63
|
+
image gallery table html columns column group spacer pullquote verse buttons
|
|
64
|
+
button details freeform more nextpage media-text cover audio video file""".split()
|
|
65
|
+
)
|
|
66
|
+
"""Blocks whose stored HTML is what WordPress displays."""
|
|
67
|
+
|
|
68
|
+
DELIMITER = re.compile(
|
|
69
|
+
r"<!--\s+(?P<closer>/)?wp:(?P<namespace>[a-z][a-z0-9_-]*/)?"
|
|
70
|
+
r"(?P<name>[a-z][a-z0-9_-]*)\s+(?P<attrs>\{.*?\}\s+)?(?P<void>/)?-->",
|
|
71
|
+
re.DOTALL,
|
|
72
|
+
)
|
|
73
|
+
WP_IMAGE_CLASS = re.compile(r"wp-image-(\d+)")
|
|
74
|
+
SIZE_CLASS = re.compile(r"size-([\w-]+)")
|
|
75
|
+
PLACEMENT_CLASS = re.compile(r"align(?:left|right|center|wide|full|none)")
|
|
76
|
+
CAPTION_CLASS = "wp-element-caption"
|
|
77
|
+
RESIZED_CLASS = "is-resized"
|
|
78
|
+
"""The class WordPress puts on the figure of an image given its own width."""
|
|
79
|
+
EMBED_ATTRS = frozenset({"url", "type", "providerNameSlug", "responsive", "className"})
|
|
80
|
+
EMBED_CLASS = re.compile(
|
|
81
|
+
r"wp-block-embed(?:-[\w-]+)?|is-type-[\w-]+|is-provider-[\w-]+"
|
|
82
|
+
r"|wp-embed-aspect-[\w-]+|wp-has-aspect-ratio"
|
|
83
|
+
)
|
|
84
|
+
EMBED_URL = re.compile(r"https?://[^\s<>\"]+")
|
|
85
|
+
CLASSIC_MARKUP = re.compile(
|
|
86
|
+
r"<!--.*?-->|<(script|style|pre|textarea)\b.*?</\1\s*>"
|
|
87
|
+
r"|<(/?)([a-zA-Z][\w:-]*)\b[^>]*?(/?)>|(\n[ \t]*\n)",
|
|
88
|
+
re.DOTALL | re.IGNORECASE,
|
|
89
|
+
)
|
|
90
|
+
VOID_NAMES = frozenset(
|
|
91
|
+
"area base br col embed hr img input link meta param source track wbr".split()
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def is_block_content(raw: str) -> bool:
|
|
96
|
+
return "<!-- wp:" in raw
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# Push ----------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def render_html(markdown: str, media: Media) -> str:
|
|
103
|
+
"""The HTML a push sends; raw HTML from the Markdown is sent exactly."""
|
|
104
|
+
html = []
|
|
105
|
+
for piece in pieces(markdown):
|
|
106
|
+
if piece.raw is not None:
|
|
107
|
+
html.append(piece.raw)
|
|
108
|
+
else:
|
|
109
|
+
html.append(to_html(render_tokens(_present(piece.tokens, media))))
|
|
110
|
+
return "\n\n".join(html)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _present(tokens: list[Token], media: Media) -> list[Token]:
|
|
114
|
+
"""Use WordPress's presentation for Markdown images and tables."""
|
|
115
|
+
presented: list[Token] = []
|
|
116
|
+
index = 0
|
|
117
|
+
while index < len(tokens):
|
|
118
|
+
token = tokens[index]
|
|
119
|
+
window = tokens[index : index + 3]
|
|
120
|
+
if [t.type for t in window] == ["paragraph_open", "inline", "paragraph_close"]:
|
|
121
|
+
children = [
|
|
122
|
+
c
|
|
123
|
+
for c in window[1].children or []
|
|
124
|
+
if not (c.type == "text" and not c.content.strip(HTML_SPACE))
|
|
125
|
+
]
|
|
126
|
+
if len(children) == 1 and children[0].type == "image":
|
|
127
|
+
image = render_inline(children[0])[0]
|
|
128
|
+
assert isinstance(image, Element)
|
|
129
|
+
presented.append(_html_token(to_html([_image_figure(image, media)])))
|
|
130
|
+
index += 3
|
|
131
|
+
continue
|
|
132
|
+
if token.type == "table_open":
|
|
133
|
+
presented.append(_html_token('<figure class="wp-block-table">'))
|
|
134
|
+
presented.append(token)
|
|
135
|
+
if token.type == "table_close":
|
|
136
|
+
presented.append(_html_token("</figure>"))
|
|
137
|
+
index += 1
|
|
138
|
+
return presented
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _html_token(html: str) -> Token:
|
|
142
|
+
token = Token("html_block", "", 0)
|
|
143
|
+
token.content = html + "\n"
|
|
144
|
+
return token
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _image_figure(image: Element, media: Media) -> Element:
|
|
148
|
+
"""WordPress's image figure: placement on the figure, the rest on the image."""
|
|
149
|
+
attrs = dict(image.attrs)
|
|
150
|
+
title = attrs.pop("title", None)
|
|
151
|
+
classes = attrs.pop("class", "").split()
|
|
152
|
+
figure_classes = ["wp-block-image"]
|
|
153
|
+
image_classes = [c for c in classes if not PLACEMENT_CLASS.fullmatch(c)]
|
|
154
|
+
library = media.lookup(attrs["src"])
|
|
155
|
+
if library is not None:
|
|
156
|
+
attrs["src"] = library.url
|
|
157
|
+
figure_classes.append(f"size-{library.size}")
|
|
158
|
+
image_classes.append(f"wp-image-{library.id}")
|
|
159
|
+
figure_classes.extend(c for c in classes if PLACEMENT_CLASS.fullmatch(c))
|
|
160
|
+
if "width" in attrs or "width" in attrs.get("style", ""):
|
|
161
|
+
figure_classes.append(RESIZED_CLASS)
|
|
162
|
+
if image_classes:
|
|
163
|
+
attrs["class"] = " ".join(image_classes)
|
|
164
|
+
children: list[Node] = [Element("img", attrs)]
|
|
165
|
+
if title:
|
|
166
|
+
children.append(Element("figcaption", {"class": CAPTION_CLASS}, [title]))
|
|
167
|
+
return Element("figure", {"class": " ".join(figure_classes)}, children)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
# What WordPress displays ---------------------------------------------------
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@dataclass(frozen=True)
|
|
174
|
+
class _Unit:
|
|
175
|
+
"""A part of the stored content that converts, or is kept, as a whole."""
|
|
176
|
+
|
|
177
|
+
source: str
|
|
178
|
+
"""The exact original text."""
|
|
179
|
+
nodes: list[Node]
|
|
180
|
+
"""What WordPress displays for it."""
|
|
181
|
+
label: str
|
|
182
|
+
convertible: bool
|
|
183
|
+
separator: str = ""
|
|
184
|
+
"""The whitespace before it in the original."""
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def display(content: str) -> list[Node]:
|
|
188
|
+
"""What WordPress displays for stored or pushed post content."""
|
|
189
|
+
if not is_block_content(content):
|
|
190
|
+
return parse_html(autop(content))
|
|
191
|
+
nodes: list[Node] = []
|
|
192
|
+
for unit in _block_units(content):
|
|
193
|
+
if unit.separator and nodes:
|
|
194
|
+
nodes.append(" ")
|
|
195
|
+
nodes.extend(unit.nodes)
|
|
196
|
+
return nodes
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def same_display(stored: str, pushed: str) -> bool:
|
|
200
|
+
"""Whether WordPress displays two post contents the same."""
|
|
201
|
+
return _comparable(display(stored)) == _comparable(display(pushed))
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _comparable(nodes: list[Node]) -> tuple:
|
|
205
|
+
return canonical(_canonical_media(nodes), EDITOR_CLASSES)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _displays_same(first: list[Node], second: list[Node]) -> bool:
|
|
209
|
+
return _comparable(first) == _comparable(second)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _blank(node: Node) -> bool:
|
|
213
|
+
return isinstance(node, str) and not node.strip(HTML_SPACE)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _block_units(raw: str) -> list[_Unit]:
|
|
217
|
+
try:
|
|
218
|
+
return list(_scan_blocks(raw))
|
|
219
|
+
except ValueError:
|
|
220
|
+
return [_Unit(raw, parse_html(raw), "malformed block markup", False)]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _scan_blocks(raw: str) -> Iterator[_Unit]:
|
|
224
|
+
depth = 0
|
|
225
|
+
start = cursor = 0
|
|
226
|
+
name = ""
|
|
227
|
+
attrs: dict = {}
|
|
228
|
+
names: set[str] = set()
|
|
229
|
+
inner_start = 0
|
|
230
|
+
for match in DELIMITER.finditer(raw):
|
|
231
|
+
block = (match["namespace"] or "core/") + match["name"]
|
|
232
|
+
if match["closer"]:
|
|
233
|
+
depth -= 1
|
|
234
|
+
if depth < 0:
|
|
235
|
+
raise ValueError(f"unexpected closing delimiter for {block}")
|
|
236
|
+
if depth == 0:
|
|
237
|
+
if block != name:
|
|
238
|
+
raise ValueError(f"{block} closes {name}")
|
|
239
|
+
yield from _freeform(raw[cursor:start])
|
|
240
|
+
yield _block_unit(
|
|
241
|
+
raw[start : match.end()],
|
|
242
|
+
name,
|
|
243
|
+
attrs,
|
|
244
|
+
names,
|
|
245
|
+
raw[inner_start : match.start()],
|
|
246
|
+
raw[cursor:start],
|
|
247
|
+
)
|
|
248
|
+
cursor = match.end()
|
|
249
|
+
continue
|
|
250
|
+
if depth == 0:
|
|
251
|
+
name, start, inner_start, names = block, match.start(), match.end(), set()
|
|
252
|
+
try:
|
|
253
|
+
attrs = json.loads(match["attrs"]) if match["attrs"] else {}
|
|
254
|
+
except json.JSONDecodeError as error:
|
|
255
|
+
raise ValueError("invalid block attributes") from error
|
|
256
|
+
names.add(block)
|
|
257
|
+
if match["void"]:
|
|
258
|
+
if depth == 0:
|
|
259
|
+
yield from _freeform(raw[cursor:start])
|
|
260
|
+
yield _Unit(match.group(0), parse_html(match.group(0)), block, False)
|
|
261
|
+
cursor = match.end()
|
|
262
|
+
continue
|
|
263
|
+
depth += 1
|
|
264
|
+
if depth:
|
|
265
|
+
raise ValueError(f"unclosed block {name}")
|
|
266
|
+
yield from _freeform(raw[cursor:])
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _freeform(text: str) -> Iterator[_Unit]:
|
|
270
|
+
source = text.strip(HTML_SPACE)
|
|
271
|
+
if source:
|
|
272
|
+
separator = text[: len(text) - len(text.lstrip(HTML_SPACE))]
|
|
273
|
+
yield _Unit(source, parse_html(source), "classic content", True, separator)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _block_unit(
|
|
277
|
+
markup: str, name: str, attrs: dict, names: set[str], inner: str, before: str
|
|
278
|
+
) -> _Unit:
|
|
279
|
+
separator = before[len(before.rstrip(HTML_SPACE)) :]
|
|
280
|
+
if name == "core/embed" or name.startswith("core-embed/"):
|
|
281
|
+
url = _plain_embed_url(attrs, inner)
|
|
282
|
+
if url is not None:
|
|
283
|
+
embed = Element("p", {"data-wpsync-embed": url})
|
|
284
|
+
return _Unit(markup, [embed], name, True, separator)
|
|
285
|
+
if name == "core/shortcode":
|
|
286
|
+
return _Unit(markup, parse_html(autop(inner)), name, True, separator)
|
|
287
|
+
if names <= STATIC_BLOCKS:
|
|
288
|
+
nodes = parse_html(DELIMITER.sub("", inner))
|
|
289
|
+
return _Unit(markup, nodes, name, True, separator)
|
|
290
|
+
return _Unit(markup, parse_html(markup), name, False, separator)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _plain_embed_url(attrs: dict, inner: str) -> str | None:
|
|
294
|
+
"""The URL of an embed WordPress displays from its address alone."""
|
|
295
|
+
url = attrs.get("url")
|
|
296
|
+
classes = attrs.get("className", "").split()
|
|
297
|
+
if (
|
|
298
|
+
not isinstance(url, str)
|
|
299
|
+
or set(attrs) - EMBED_ATTRS
|
|
300
|
+
or not all(EMBED_CLASS.fullmatch(c) for c in classes)
|
|
301
|
+
):
|
|
302
|
+
return None
|
|
303
|
+
figures = [n for n in parse_html(inner) if not _blank(n)]
|
|
304
|
+
if len(figures) != 1 or not isinstance(figures[0], Element):
|
|
305
|
+
return None
|
|
306
|
+
figure = figures[0]
|
|
307
|
+
if figure.tag != "figure" or set(figure.attrs) != {"class"}:
|
|
308
|
+
return None
|
|
309
|
+
if not all(EMBED_CLASS.fullmatch(c) for c in figure.attrs["class"].split()):
|
|
310
|
+
return None
|
|
311
|
+
wrappers = [n for n in figure.children if not _blank(n)]
|
|
312
|
+
if len(wrappers) != 1 or not isinstance(wrappers[0], Element):
|
|
313
|
+
return None
|
|
314
|
+
text = wrappers[0].children
|
|
315
|
+
if len(text) != 1 or not isinstance(text[0], str) or text[0].strip() != url:
|
|
316
|
+
return None
|
|
317
|
+
return url
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _classic_units(raw: str) -> list[_Unit]:
|
|
321
|
+
"""Split classic content where `wpautop` starts a new paragraph or block.
|
|
322
|
+
|
|
323
|
+
Boundaries are blank lines and the edges of block elements, counted only
|
|
324
|
+
outside every other element.
|
|
325
|
+
"""
|
|
326
|
+
cuts = [0]
|
|
327
|
+
depth = 0
|
|
328
|
+
for match in CLASSIC_MARKUP.finditer(raw):
|
|
329
|
+
if match[5] is not None:
|
|
330
|
+
if depth == 0:
|
|
331
|
+
cuts.append(match.start())
|
|
332
|
+
continue
|
|
333
|
+
name = (match[1] or match[3] or "").lower()
|
|
334
|
+
block = name in BLOCK_NAMES
|
|
335
|
+
if match[1] or (match[3] and not match[2] and not match[4]):
|
|
336
|
+
if depth == 0 and block:
|
|
337
|
+
cuts.append(match.start())
|
|
338
|
+
if match[3] and name not in VOID_NAMES:
|
|
339
|
+
depth += 1
|
|
340
|
+
if match[1] or (match[3] and match[2]):
|
|
341
|
+
if match[3]:
|
|
342
|
+
depth = max(0, depth - 1)
|
|
343
|
+
if depth == 0 and block:
|
|
344
|
+
cuts.append(match.end())
|
|
345
|
+
cuts.append(len(raw))
|
|
346
|
+
chunks = (raw[start:end] for start, end in zip(cuts, cuts[1:]))
|
|
347
|
+
units = []
|
|
348
|
+
for chunk in chunks:
|
|
349
|
+
source = chunk.strip(HTML_SPACE)
|
|
350
|
+
if source:
|
|
351
|
+
nodes = parse_html(autop(source))
|
|
352
|
+
units.append(_Unit(source, nodes, _classic_label(nodes), True))
|
|
353
|
+
return units
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _classic_label(nodes: list[Node]) -> str:
|
|
357
|
+
elements = [n for n in nodes if isinstance(n, Element)]
|
|
358
|
+
if len(elements) == 1 and elements[0].tag == "p":
|
|
359
|
+
elements = [
|
|
360
|
+
c for c in elements[0].children if isinstance(c, Element)
|
|
361
|
+
] or elements
|
|
362
|
+
return f"<{elements[0].tag}>" if elements else "classic text"
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
# Comparable forms of WordPress presentation --------------------------------
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
@dataclass(frozen=True)
|
|
369
|
+
class _Image:
|
|
370
|
+
"""An image as either editor presents it."""
|
|
371
|
+
|
|
372
|
+
src: str
|
|
373
|
+
alt: str | None
|
|
374
|
+
caption: str | None
|
|
375
|
+
media_id: str | None
|
|
376
|
+
size: str | None
|
|
377
|
+
placement: tuple[str, ...]
|
|
378
|
+
"""Alignment classes, which may sit on the image or its figure."""
|
|
379
|
+
figure_classes: tuple[str, ...]
|
|
380
|
+
image_classes: tuple[str, ...]
|
|
381
|
+
attrs: tuple[tuple[str, str], ...]
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _parse_image(node: Node) -> _Image | None:
|
|
385
|
+
"""Read a block-editor image figure or a classic paragraph holding one image."""
|
|
386
|
+
if not isinstance(node, Element):
|
|
387
|
+
return None
|
|
388
|
+
children = [c for c in node.children if not _blank(c)]
|
|
389
|
+
caption = None
|
|
390
|
+
figure_classes: list[str] = []
|
|
391
|
+
if node.tag == "figure" and "wp-block-image" in node.attrs.get("class", "").split():
|
|
392
|
+
if set(node.attrs) != {"class"} or not children:
|
|
393
|
+
return None
|
|
394
|
+
if len(children) == 2:
|
|
395
|
+
caption = _caption_text(children[1])
|
|
396
|
+
if caption is None:
|
|
397
|
+
return None
|
|
398
|
+
elif len(children) != 1:
|
|
399
|
+
return None
|
|
400
|
+
figure_classes = [
|
|
401
|
+
c for c in node.attrs["class"].split() if c != "wp-block-image"
|
|
402
|
+
]
|
|
403
|
+
elif not (node.tag == "p" and not node.attrs and len(children) == 1):
|
|
404
|
+
return None
|
|
405
|
+
image = children[0]
|
|
406
|
+
if not isinstance(image, Element) or image.tag != "img" or "src" not in image.attrs:
|
|
407
|
+
return None
|
|
408
|
+
attrs = dict(image.attrs)
|
|
409
|
+
src, alt = attrs.pop("src"), attrs.pop("alt", None)
|
|
410
|
+
image_classes = attrs.pop("class", "").split()
|
|
411
|
+
pooled = figure_classes + image_classes
|
|
412
|
+
ids = [m[1] for c in image_classes if (m := WP_IMAGE_CLASS.fullmatch(c))]
|
|
413
|
+
sizes = [m[1] for c in pooled if (m := SIZE_CLASS.fullmatch(c))]
|
|
414
|
+
if len(ids) > 1 or len(sizes) > 1 or len(ids) != len(sizes):
|
|
415
|
+
return None
|
|
416
|
+
|
|
417
|
+
def own(classes: list[str]) -> tuple[str, ...]:
|
|
418
|
+
return tuple(
|
|
419
|
+
c
|
|
420
|
+
for c in classes
|
|
421
|
+
if not (
|
|
422
|
+
WP_IMAGE_CLASS.fullmatch(c)
|
|
423
|
+
or SIZE_CLASS.fullmatch(c)
|
|
424
|
+
or PLACEMENT_CLASS.fullmatch(c)
|
|
425
|
+
or c == RESIZED_CLASS
|
|
426
|
+
)
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
return _Image(
|
|
430
|
+
src,
|
|
431
|
+
alt,
|
|
432
|
+
caption,
|
|
433
|
+
ids[0] if ids else None,
|
|
434
|
+
sizes[0] if sizes else None,
|
|
435
|
+
tuple(sorted({c for c in pooled if PLACEMENT_CLASS.fullmatch(c)})),
|
|
436
|
+
own(figure_classes),
|
|
437
|
+
own(image_classes),
|
|
438
|
+
tuple(attrs.items()),
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _caption_text(node: Node) -> str | None:
|
|
443
|
+
"""A plain figure caption's text; styled or attributed captions return None."""
|
|
444
|
+
if not isinstance(node, Element) or node.tag != "figcaption":
|
|
445
|
+
return None
|
|
446
|
+
if node.attrs and node.attrs != {"class": CAPTION_CLASS}:
|
|
447
|
+
return None
|
|
448
|
+
if not all(isinstance(child, str) for child in node.children):
|
|
449
|
+
return None
|
|
450
|
+
return "".join(node.children).strip(HTML_SPACE) # type: ignore[arg-type]
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
def _canonical_media(nodes: list[Node]) -> list[Node]:
|
|
454
|
+
"""Give each displayed image, embed, and comment paragraph one comparable form.
|
|
455
|
+
|
|
456
|
+
The forms are block elements, like the figures and paragraphs they stand for.
|
|
457
|
+
"""
|
|
458
|
+
result: list[Node] = []
|
|
459
|
+
for node in nodes:
|
|
460
|
+
image = _parse_image(node)
|
|
461
|
+
if image is not None:
|
|
462
|
+
node = Element(
|
|
463
|
+
"figure",
|
|
464
|
+
{
|
|
465
|
+
"data-wpsync-image": "",
|
|
466
|
+
"src": image.src,
|
|
467
|
+
"alt": "" if image.alt is None else image.alt,
|
|
468
|
+
"has-alt": str(image.alt is not None),
|
|
469
|
+
"caption": image.caption or "",
|
|
470
|
+
"media": f"{image.media_id}:{image.size}",
|
|
471
|
+
"placement": " ".join(image.placement),
|
|
472
|
+
"figure-class": " ".join(sorted(image.figure_classes)),
|
|
473
|
+
"image-class": " ".join(sorted(image.image_classes)),
|
|
474
|
+
**dict(image.attrs),
|
|
475
|
+
},
|
|
476
|
+
)
|
|
477
|
+
elif isinstance(node, Element) and node.tag == "p" and not node.attrs:
|
|
478
|
+
content = [c for c in node.children if not _blank(c)]
|
|
479
|
+
if len(content) == 1 and isinstance(content[0], str):
|
|
480
|
+
url = content[0].strip(HTML_SPACE)
|
|
481
|
+
if EMBED_URL.fullmatch(url):
|
|
482
|
+
node = Element("p", {"data-wpsync-embed": url})
|
|
483
|
+
elif content and all(isinstance(c, Comment) for c in content):
|
|
484
|
+
# WordPress wraps a lone <!--more--> in an empty paragraph.
|
|
485
|
+
result.extend(content)
|
|
486
|
+
continue
|
|
487
|
+
if isinstance(node, Element) and not _is_media_form(node):
|
|
488
|
+
node = Element(
|
|
489
|
+
node.tag, _displayed_attrs(node), _canonical_media(node.children)
|
|
490
|
+
)
|
|
491
|
+
result.append(node)
|
|
492
|
+
return result
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def _displayed_attrs(node: Element) -> dict[str, str]:
|
|
496
|
+
if node.tag != "a":
|
|
497
|
+
return node.attrs
|
|
498
|
+
return {k: v for k, v in node.attrs.items() if k not in EDITOR_LINK_ATTRS}
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def _is_media_form(node: Element) -> bool:
|
|
502
|
+
return "data-wpsync-image" in node.attrs or "data-wpsync-embed" in node.attrs
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
# Pull ----------------------------------------------------------------------
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def to_markdown(raw: str, media: Media) -> Conversion:
|
|
509
|
+
"""Convert stored content, keeping as exact source what would not display alike."""
|
|
510
|
+
units = _block_units(raw) if is_block_content(raw) else _classic_units(raw)
|
|
511
|
+
converter = Converter(EDITOR_CLASSES)
|
|
512
|
+
fragments = [_convert(unit, converter, media) for unit in units]
|
|
513
|
+
converted = [
|
|
514
|
+
index for index, fragment in enumerate(fragments) if fragment is not None
|
|
515
|
+
]
|
|
516
|
+
expected = _comparable(display(raw))
|
|
517
|
+
|
|
518
|
+
def document(chosen: list[int]) -> str:
|
|
519
|
+
parts = [
|
|
520
|
+
fragments[index] if index in chosen else kept_markdown(unit.source)
|
|
521
|
+
for index, unit in enumerate(units)
|
|
522
|
+
]
|
|
523
|
+
return join_blocks([part for part in parts if part]) # type: ignore[misc]
|
|
524
|
+
|
|
525
|
+
def displays_alike(chosen: list[int]) -> bool:
|
|
526
|
+
pushed = render_html(document(chosen), media)
|
|
527
|
+
return _comparable(display(pushed)) == expected
|
|
528
|
+
|
|
529
|
+
if not displays_alike(converted):
|
|
530
|
+
if not displays_alike([]):
|
|
531
|
+
raise UnrepresentableContent(
|
|
532
|
+
"the post displays differently even when kept entirely as HTML"
|
|
533
|
+
)
|
|
534
|
+
while not displays_alike(converted):
|
|
535
|
+
# Find the first conversion that breaks the document and keep it.
|
|
536
|
+
good, bad = 0, len(converted)
|
|
537
|
+
while bad - good > 1:
|
|
538
|
+
middle = (good + bad) // 2
|
|
539
|
+
if displays_alike(converted[:middle]):
|
|
540
|
+
good = middle
|
|
541
|
+
else:
|
|
542
|
+
bad = middle
|
|
543
|
+
converted.pop(bad - 1)
|
|
544
|
+
kept = tuple(
|
|
545
|
+
unit.label for index, unit in enumerate(units) if index not in converted
|
|
546
|
+
)
|
|
547
|
+
pages = tuple(dict.fromkeys(_uploaded_pages(display(raw))))
|
|
548
|
+
return Conversion(document(converted), kept, pages)
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def _convert(unit: _Unit, converter: Converter, media: Media) -> str | None:
|
|
552
|
+
if not unit.convertible:
|
|
553
|
+
return None
|
|
554
|
+
try:
|
|
555
|
+
nodes = trim_block_whitespace(unit.nodes)
|
|
556
|
+
parts = [converter.block(_for_conversion(node, media)) for node in nodes]
|
|
557
|
+
except Unconvertible:
|
|
558
|
+
return None
|
|
559
|
+
fragment = join_blocks(parts)
|
|
560
|
+
if not _displays_same(display(render_html(fragment, media)), unit.nodes):
|
|
561
|
+
return None
|
|
562
|
+
return fragment
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
def _for_conversion(node: Node, media: Media) -> Node:
|
|
566
|
+
"""Rewrite WordPress presentation markup into the form the converter reads."""
|
|
567
|
+
if not isinstance(node, Element):
|
|
568
|
+
return node
|
|
569
|
+
if "data-wpsync-embed" in node.attrs:
|
|
570
|
+
return Element("p", {}, [node.attrs["data-wpsync-embed"]])
|
|
571
|
+
classes = node.attrs.get("class", "").split()
|
|
572
|
+
if node.tag == "figure" and "wp-block-table" in classes:
|
|
573
|
+
tables = [c for c in node.children if isinstance(c, Element)]
|
|
574
|
+
if len(classes) == 1 and len(tables) == 1 and tables[0].tag == "table":
|
|
575
|
+
return tables[0]
|
|
576
|
+
image = _parse_image(node)
|
|
577
|
+
if image is not None and not image.figure_classes:
|
|
578
|
+
attrs = {"src": image.src, **dict(image.attrs)}
|
|
579
|
+
if image.alt is not None:
|
|
580
|
+
attrs["alt"] = image.alt
|
|
581
|
+
if image.media_id is not None:
|
|
582
|
+
attrs["src"] = media.remember(image.src, image.media_id, image.size) # type: ignore[arg-type]
|
|
583
|
+
if image.caption:
|
|
584
|
+
attrs["title"] = image.caption
|
|
585
|
+
classes = list(image.placement) + list(image.image_classes)
|
|
586
|
+
if classes:
|
|
587
|
+
attrs["class"] = " ".join(classes)
|
|
588
|
+
return Element("p", {}, [Element("img", attrs)])
|
|
589
|
+
return Element(
|
|
590
|
+
node.tag,
|
|
591
|
+
_displayed_attrs(node),
|
|
592
|
+
[_for_conversion(c, media) for c in node.children],
|
|
593
|
+
)
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
def _uploaded_pages(nodes: list[Node]) -> Iterator[str]:
|
|
597
|
+
"""The sources of iframes showing files uploaded to a WordPress site."""
|
|
598
|
+
for node in nodes:
|
|
599
|
+
if isinstance(node, Element):
|
|
600
|
+
if node.tag == "iframe" and UPLOADS_PATH in node.attrs.get("src", ""):
|
|
601
|
+
yield node.attrs["src"]
|
|
602
|
+
yield from _uploaded_pages(node.children)
|