-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_caption.py
More file actions
109 lines (91 loc) · 3.33 KB
/
image_caption.py
File metadata and controls
109 lines (91 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
caption - Manage markdown captions
Applying style and auto-numbering to Python-Markdown content.
https://github.com/flywire/caption
Copyright (c) 2020-2023 flywire
Copyright (c) 2023 sanzoghenzo
forked from yafg - https://git.sr.ht/~ferruck/yafg
Copyright (c) 2019-2020 Philipp Trommler
SPDX-License-Identifier: GPL-3.0-or-later
"""
from markdown import Extension
from .caption import CaptionTreeprocessor
class ImageCaptionTreeProcessor(CaptionTreeprocessor):
name = "figure"
content_tag = "figure"
def __init__(
self,
md=None,
caption_prefix="",
numbering=True,
caption_prefix_class=None,
caption_class=None,
content_class=None,
strip_title=True,
caption_top=False,
caption_id=True,
):
super(ImageCaptionTreeProcessor, self).__init__(
md=md,
caption_prefix=caption_prefix,
numbering=numbering,
caption_prefix_class=caption_prefix_class,
caption_class=caption_class,
content_class=content_class,
caption_top=caption_top,
caption_id=caption_id,
)
self.strip_title = strip_title
def matches(self, par):
self._a = None
self._img = par.find("./img")
if self._img is None:
self._a = par.find("./a")
if self._a is None:
return False
self._img = self._a.find("./img")
return self._img is not None
def get_title(self, par):
return self._img.get("title")
def build_content_element(self, par, caption, replace=True):
super(ImageCaptionTreeProcessor, self).build_content_element(par, caption, replace=replace)
if self._a is not None:
self._a.tail = "\n"
par.append(self._a)
else:
self._img.tail = self._img.tail or "" + "\n"
par.append(self._img)
def build_caption_element(self, title):
caption = super(ImageCaptionTreeProcessor, self).build_caption_element(title)
if self.strip_title and title:
del self._img.attrib["title"]
return caption
class ImageCaptionExtension(Extension):
# caption Extension
def __init__(self, **kwargs):
# Setup configs
self.config = {
"caption_prefix": [
"Figure",
"The text to show in front of the image caption.",
],
"numbering": [True, "Add the caption number to the prefix."],
"caption_prefix_class": [
"",
"CSS class to add to the caption prefix <span /> element.",
],
"caption_class": ["", "CSS class to add to the caption element."],
"content_class": ["", "CSS class to add to the content element."],
"strip_title": [True, "Remove the title from the img tag."],
"caption_top": [False, "Put the caption at the top of the image."],
"caption_id": [True, "Add an id to the element."],
}
super(ImageCaptionExtension, self).__init__(**kwargs)
def extendMarkdown(self, md):
md.treeprocessors.register(
ImageCaptionTreeProcessor(md, **self.getConfigs()),
"figurecaptiontreeprocessor",
8,
)
def makeExtension(**kwargs):
return ImageCaptionExtension(**kwargs)