55 lines
2.6 KiB
Python
55 lines
2.6 KiB
Python
def add_lightboxes(html: str,
|
|
lightbox_class: str = "lightbox",
|
|
wrapper_class: str = "lightbox-wrapper") -> str:
|
|
counter: int = 0
|
|
img_start: int = html.find("<img")
|
|
while img_start > -1:
|
|
counter += 1
|
|
img_end: int = html.find(">", img_start + 4)
|
|
lightbox: str = f"<span id=\"fig{counter}\"></span>\n" +\
|
|
f"<figure id=\"figure{counter}\">\n" +\
|
|
f" <a href=\"#figure{counter}\">\n" +\
|
|
f" {html[img_start:img_end + 1]}\n" +\
|
|
f" </a>\n"
|
|
alt_start: int = html.find("alt=\"", img_start + 4)
|
|
if alt_start > -1:
|
|
alt_end: int = html.find("\"", alt_start + 5)
|
|
lightbox = lightbox + f" <figcaption>\n" +\
|
|
f" {html[alt_start + 5:alt_end]}\n" +\
|
|
f" </figcaption>\n"
|
|
lightbox = lightbox + f" <div class=\"{lightbox_class}\">\n" +\
|
|
f" <div class=\"{wrapper_class}\">\n" +\
|
|
f" <a href=\"#fig{counter}\">\n" +\
|
|
f" [close]\n" +\
|
|
f" </a>\n" +\
|
|
f" {html[img_start:img_end + 1]}\n" +\
|
|
f" </div>\n" +\
|
|
f" </div>\n" +\
|
|
f"</figure>\n"
|
|
if html[img_start - 3:img_start] == "<p>" and html[img_end + 1:img_end + 5] == "</p>":
|
|
img_start -= 3
|
|
img_end += 4
|
|
html = html[:img_start] + lightbox + html[img_end + 1:]
|
|
img_start = html.find("<img", img_start + len(lightbox))
|
|
return html
|
|
|
|
|
|
def add_sidenotes(html: str,
|
|
toggle_class: str = "sidenote-toggle",
|
|
label_class: str = "sidenote-number",
|
|
sidenote_class: str = "sidenote") -> str:
|
|
counter: int = 0
|
|
start: int = html.find("[°")
|
|
while start > -1:
|
|
counter += 1
|
|
end: int = html.find("]", start)
|
|
text: str = html[start + 2:end]
|
|
sidenote: str = f"<label for=\"sn-{counter}\" class=\"{toggle_class} {label_class}\"></label>\n" +\
|
|
f"<input type=\"checkbox\" id=\"sn-{counter}\" class=\"{toggle_class}\" />\n" +\
|
|
f"<span class=\"{sidenote_class}\">\n" +\
|
|
f" {text}\n" +\
|
|
f"</span>\n"
|
|
html = html[:start] + sidenote + html[end + 1:]
|
|
start = html.find("[°", start + len(sidenote))
|
|
return html
|