Convert Wordpress caption to figure

In Wordpress, inserting image with a caption can look like:

[caption id="attachment_42" caption="Image Description"]<a ...><img ... /></a>[/caption]
[caption id="attachment_42"]<a ...><img ... /></a> Image Description[/caption]
[caption id="attachment_42"]<img ... > Image Description[/caption]

Replace by an HTML figure tag
This commit is contained in:
Martin (mart-e) 2023-06-04 12:34:53 +02:00 committed by Martin Trigaux
commit 48166bd687
3 changed files with 79 additions and 1 deletions

View file

@ -107,6 +107,13 @@ def decode_wp_content(content, br=True):
return re.sub(pattern, lambda m: dic[m.group()], string)
content = _multi_replace(pre_tags, content)
# convert [caption] tags into <figure>
content = re.sub(
r'\[caption(?:.*?)(?:caption=\"(.*?)\")?\]'
r'((?:\<a(?:.*?)\>)?(?:\<img.*?\>)(?:\<\/a\>)?)\s?(.*?)\[\/caption\]',
r'<figure>\n\2\n<figcaption>\1\3</figcaption>\n</figure>',
content)
return content