2
0
Fork 0
textout/textoutpc/Tags/Image.py

84 lines
2.0 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from urllib.parse import urlparse
from html import escape as _htmlescape
from .__base__ import TextoutTag
__all__ = ["TextoutImageTag", "TextoutAdminImageTag"]
class TextoutImageTag(TextoutBlockTag):
2018-01-02 18:57:04 +01:00
""" The main tag for displaying an image.
Example uses:
[img]picture_url[/img]
[img=center]picture_url[/img]
[img=12x24]picture_url[/img]
[img=center|12x24]picture_url[/img]
[img=x24|right]picture_url[/img]
"""
def _prepare(self):
self.eval = False
self.width = None
self.height = None
self.align = None
val = self.value if self.value else ""
for arg in val.split('|'):
if not arg:
pass
elif arg[0] in '0123456789x':
dim = arg.split('x')
try: self.width = int(dim[0])
except: pass
try: self.height = int(dim[1])
except: pass
elif arg in ('center', 'right', 'float-left', 'float-right'):
self.align = arg
def end_text(self):
return '![ {} ]!'.format(self.content)
def end_html(self):
url = self.content
if not url:
return ""
u = urlparse(self.content)
if u.scheme and not u.scheme in ('http', 'https', 'ftp'):
return ""
url = _htmlescape(url)
style = []
if self.width:
style.append('width: {}px'.format(self.width))
elif self.height:
style.append('width: auto')
if self.height:
style.append('height: {}px'.format(self.height))
elif self.width:
style.append('height: auto')
return '<img src="{}"{}{}>'.format(self.content,
' class="{}"'.format(self.align) if self.align else '',
' style="{}"'.format('; '.join(style)) if style else '')
class TextoutAdminImageTag(TextoutImageTag):
""" This tag is special for Planète Casio, as it takes images from
the `ad`ministration's image folder.
It just adds this folder's prefix.
Example uses:
[adimg]some_picture.png[/adimg]
[adimg=center|x24]some_picture_with_attributes.png[/adimg]
"""
def _set(self):
self.content = 'https://www.planet-casio.com/images/ad/' \
+ self.content
super(TextoutAdminImageTag, self)._set()
# End of file.