summaryrefslogtreecommitdiff
path: root/extendedimage.py
diff options
context:
space:
mode:
Diffstat (limited to 'extendedimage.py')
-rw-r--r--extendedimage.py34
1 files changed, 22 insertions, 12 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)]
+ )