diff options
author | Botond Hende <nettingman@gmail.com> | 2024-08-08 21:39:17 +0200 |
---|---|---|
committer | Botond Hende <nettingman@gmail.com> | 2024-08-08 21:39:17 +0200 |
commit | ed0aa1323a7acae363c670cc1dd6611c09e0c3fb (patch) | |
tree | 6ba7dc3c303e791ad52c02986ac27a5a2cdde798 | |
parent | c307de76f9b54d5c65196341ee12ea38ac3d6e6b (diff) |
extended image handles foreign images
-rw-r--r-- | extendedimage.py | 34 | ||||
-rw-r--r-- | newtablink.py | 5 | ||||
-rw-r--r-- | utils.py | 3 |
3 files changed, 27 insertions, 15 deletions
diff --git a/extendedimage.py b/extendedimage.py index 4babc03..cec690f 100644 --- a/extendedimage.py +++ b/extendedimage.py @@ -11,6 +11,7 @@ Parameters can be encapsulated in double-quotes. Use "" for empty parameter. """ from marko import inline from marko.helpers import MarkoExtension +from . import utils class ExtendedImageElement(inline.InlineElement): @@ -32,20 +33,29 @@ class ExtendedImageElement(inline.InlineElement): return text -class ExtendedImageRenderer: - def render_extended_image_element(self, element): - img = f'<img src="{element.src}" title="{element.title}" alt="{element.alt}" />' +def __create_image_renderer(filters: list[str]): + class ExtendedImageRenderer: + FILTERS = filters - if element.href != "": - img = f'<a href="{element.href}">{img}</a>' + def render_extended_image_element(self, element): + img = f'<img src="{element.src}" title="{element.title}" alt="{element.alt}" />' - if element.figcaption != "": - img = f"<figure>{img}<figcaption>{element.figcaption}</figcaption></figure>" + if element.href != "": + img = f'<a href="{element.href}" {"target=\"_blank\"" if utils.is_foreign_url(element.href, ExtendedImageRenderer.FILTERS) else ""}>{img}</a>' - return img + if element.figcaption != "": + img = f"<figure>{img}<figcaption>{element.figcaption}</figcaption></figure>" + return img -ExtendedImage = MarkoExtension( - elements=[ExtendedImageElement], - renderer_mixins=[ExtendedImageRenderer] -) + return ExtendedImageRenderer + + +def get_image_renderer(filters: list[str] = None): + if filters is None: + filters = [] + + return MarkoExtension( + elements=[ExtendedImageElement], + renderer_mixins=[__create_image_renderer(filters)] + ) diff --git a/newtablink.py b/newtablink.py index df42833..621424d 100644 --- a/newtablink.py +++ b/newtablink.py @@ -7,6 +7,7 @@ MyExtension = get_new_tab_links(["example.org"]) """ import marko.inline from marko.helpers import MarkoExtension +from . import utils def __create_new_tab_links(filters: list[str]): @@ -14,9 +15,7 @@ def __create_new_tab_links(filters: list[str]): FILTERS = filters def render_link(self, element: marko.inline.Link) -> str: - dest_lower = element.dest.lower() - - if (dest_lower.startswith("http://") or dest_lower.startswith("https://")) and not any((elem in dest_lower for elem in NewTabLinks.FILTERS)): + if utils.is_foreign_url(element.dest, NewTabLinks.FILTERS): return f"<a href=\"{element.dest}\" target=\"_blank\">{self.render_children(element)}</a>" return super().render_link(element) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..ea5e0c1 --- /dev/null +++ b/utils.py @@ -0,0 +1,3 @@ +def is_foreign_url(link: str, filters: list[str]): + dest_lower = link.lower() + return (dest_lower.startswith("http://") or dest_lower.startswith("https://")) and not any((elem in dest_lower for elem in filters))
\ No newline at end of file |