2
0
Fork 0
textout/textoutpc/builtin/_Image.py

130 lines
3.3 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
2018-01-02 18:57:04 +01:00
import urllib.parse as _urlparse
from .. import BlockTag as _BlockTag
2018-01-02 18:57:04 +01:00
from html import escape as _htmlescape
__all__ = ["ImageTag", "AdminImageTag"]
2018-01-02 18:57:04 +01:00
2018-08-25 17:38:24 +02:00
class ImageTag(_BlockTag):
2018-01-02 18:57:04 +01:00
""" The main tag for displaying an image.
Example uses:
2018-08-25 17:38:24 +02:00
2018-01-02 18:57:04 +01:00
[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]
"""
aliases = ('[img]',)
raw = True
2018-01-02 18:57:04 +01:00
2018-01-16 13:34:11 +01:00
def prepare(self, name, value):
2018-07-29 19:51:42 +02:00
_align = {
'center': ('center', False),
'centre': ('center', False),
'left': ('left', False),
'right': ('right', False),
'float': (None, True),
'floating': (None, True),
'float-left': ('left', True),
'float-center': ('center', True),
'float-centre': ('center', True),
'float-right': ('right', True),
}
self._width = None
self._height = None
self._align = None
self._float = False
2018-01-02 18:57:04 +01:00
2018-08-25 17:38:24 +02:00
for arg in ("", value)[value is not None].split('|'):
2018-01-02 18:57:04 +01:00
if not arg:
pass
elif arg[0] in '0123456789x':
self._width = None
self._height = None
2018-01-02 18:57:04 +01:00
dim = arg.split('x')
2018-08-25 17:38:24 +02:00
try:
self._width = int(dim[0])
except ValueError:
pass
try:
self._height = int(dim[1])
except ValueError:
pass
2018-07-29 19:51:42 +02:00
elif arg in _align:
al, fl = _align[arg]
2018-08-25 17:38:24 +02:00
if al is not None:
2018-07-29 19:51:42 +02:00
self._align = al
if fl:
self._float = True
2018-01-02 18:57:04 +01:00
2018-07-30 14:03:40 +02:00
def preprocess(self, content):
try:
self._image = self.image(content)
except:
url = _urlparse.urlparse(content)
if url.scheme not in ('http', 'https'):
raise Exception("No allowed prefix!")
self._image = content
2018-01-19 11:23:19 +01:00
def content_html(self):
if isinstance(self._image, str):
url = _htmlescape(self._image)
return '<p><a href="{}">{}</a></p>'.format(url, url)
2018-01-02 18:57:04 +01:00
style = []
cls = []
if self._width:
style.append('width: {}px'.format(self._width))
elif self._height:
2018-01-02 18:57:04 +01:00
style.append('width: auto')
if self._height:
style.append('height: {}px'.format(self._height))
elif self._width:
2018-01-02 18:57:04 +01:00
style.append('height: auto')
if self._float:
2018-07-29 19:51:42 +02:00
cls.append('img-float-{}'.format(self._align or 'right'))
elif self._align:
cls.append('img-{}'.format(self._align))
2018-01-02 18:57:04 +01:00
return '<img src="{}"{}{} />'.format(_htmlescape(self._image.embed),
' class="{}"'.format(' '.join(cls)) if cls else '',
2018-01-02 18:57:04 +01:00
' style="{}"'.format('; '.join(style)) if style else '')
def content_lightscript(self):
url = self._image.embed.replace('[', '%5B').replace(']', '%5D')
return '[[image:{}]]'.format(url)
2018-08-25 17:38:24 +02:00
class AdminImageTag(ImageTag):
2018-01-02 18:57:04 +01:00
""" 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:
2018-08-25 17:38:24 +02:00
[adimg]some_picture.png[/img]
[adimg=center]some_picture.png[/img]
[adimg=12x24]some_picture.png[/img]
[adimg=center|12x24]some_picture.png[/img]
[adimg=x24|right]some_picture.png[/img]
2018-01-02 18:57:04 +01:00
"""
aliases = ('[adimg]',)
def preprocess(self, content):
2018-07-30 14:03:40 +02:00
self._url = 'https://www.planet-casio.com/images/ad/' + content
self._checkurl()
2018-01-02 18:57:04 +01:00
# End of file.