2018-01-02 18:57:04 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-02-19 19:50:26 +01:00
|
|
|
#******************************************************************************
|
|
|
|
# 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
|
|
|
|
2018-07-28 19:36:43 +02:00
|
|
|
from .. import BlockTag as _TextoutBlockTag
|
2018-01-02 18:57:04 +01:00
|
|
|
from html import escape as _htmlescape
|
|
|
|
|
|
|
|
__all__ = ["TextoutImageTag", "TextoutAdminImageTag"]
|
|
|
|
|
2018-02-19 20:13:10 +01:00
|
|
|
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]
|
|
|
|
"""
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
aliases = ('[img]',)
|
2018-01-24 12:33:33 +01:00
|
|
|
raw = True
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-16 13:34:11 +01:00
|
|
|
def prepare(self, name, value):
|
2018-01-21 22:08:23 +01:00
|
|
|
self._width = None
|
|
|
|
self._height = None
|
|
|
|
self._align = None
|
2018-01-23 21:24:16 +01:00
|
|
|
self._float = False
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-16 13:34:11 +01:00
|
|
|
for arg in ("", value)[value != None].split('|'):
|
2018-01-02 18:57:04 +01:00
|
|
|
if not arg:
|
|
|
|
pass
|
|
|
|
elif arg[0] in '0123456789x':
|
2018-01-23 21:24:16 +01:00
|
|
|
self._width = None
|
|
|
|
self._height = None
|
|
|
|
|
2018-01-02 18:57:04 +01:00
|
|
|
dim = arg.split('x')
|
2018-01-21 22:08:23 +01:00
|
|
|
try: self._width = int(dim[0])
|
2018-01-02 18:57:04 +01:00
|
|
|
except: pass
|
2018-01-21 22:08:23 +01:00
|
|
|
try: self._height = int(dim[1])
|
2018-01-02 18:57:04 +01:00
|
|
|
except: pass
|
2018-01-23 21:24:16 +01:00
|
|
|
elif arg in ('center', 'left', 'right'):
|
2018-01-21 22:08:23 +01:00
|
|
|
self._align = arg
|
2018-01-23 21:24:16 +01:00
|
|
|
elif arg in ('float-left', 'float-right'):
|
|
|
|
self._align = arg[6:]
|
|
|
|
self._float = True
|
|
|
|
elif arg in ('float', 'floating'):
|
|
|
|
self._float = True
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-21 22:08:23 +01:00
|
|
|
def preprocess(self, content):
|
2018-01-05 22:13:30 +01:00
|
|
|
for prefix in ('http://', 'https://', 'ftp://', '/'):
|
2018-01-16 13:34:11 +01:00
|
|
|
if content.startswith(prefix):
|
2018-01-05 22:13:30 +01:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise Exception("No allowed prefix!")
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-21 22:08:23 +01:00
|
|
|
self._url = content
|
2018-01-19 11:23:19 +01:00
|
|
|
|
2018-01-21 22:08:23 +01:00
|
|
|
def content_html(self):
|
2018-01-02 18:57:04 +01:00
|
|
|
style = []
|
2018-01-23 21:24:16 +01:00
|
|
|
cls = []
|
|
|
|
if self._width:
|
2018-01-21 22:08:23 +01:00
|
|
|
style.append('width: {}px'.format(self._width))
|
2018-01-23 21:24:16 +01:00
|
|
|
elif self._height:
|
2018-01-02 18:57:04 +01:00
|
|
|
style.append('width: auto')
|
2018-01-23 21:24:16 +01:00
|
|
|
if self._height:
|
2018-01-21 22:08:23 +01:00
|
|
|
style.append('height: {}px'.format(self._height))
|
2018-01-23 21:24:16 +01:00
|
|
|
elif self._width:
|
2018-01-02 18:57:04 +01:00
|
|
|
style.append('height: auto')
|
2018-01-23 21:24:16 +01:00
|
|
|
if self._float:
|
|
|
|
cls.append('img-float')
|
|
|
|
if self._align:
|
|
|
|
cls.append('img-{}'.format(self._align))
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-24 13:34:13 +01:00
|
|
|
return '<img src="{}"{}{} />'.format(\
|
|
|
|
_htmlescape(self._url),
|
2018-01-23 21:24:16 +01:00
|
|
|
' class="{}"'.format(' '.join(cls)) if cls else '',
|
2018-01-02 18:57:04 +01:00
|
|
|
' style="{}"'.format('; '.join(style)) if style else '')
|
|
|
|
|
2018-02-11 12:01:32 +01:00
|
|
|
def content_lightscript(self):
|
|
|
|
url = self._url.replace('[', '%5B').replace(']', '%5D')
|
|
|
|
return '[[image:{}]]'.format(url)
|
|
|
|
|
2018-01-02 18:57:04 +01:00
|
|
|
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:
|
|
|
|
|
2018-01-05 22:13:30 +01: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
|
|
|
"""
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
aliases = ('[adimg]',)
|
|
|
|
|
2018-01-21 22:08:23 +01:00
|
|
|
def preprocess(self, content):
|
|
|
|
path = content
|
2018-01-16 13:34:11 +01:00
|
|
|
# FIXME: check image URL!
|
2018-01-21 22:08:23 +01:00
|
|
|
self._url = 'https://www.planet-casio.com/images/ad/' + path
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
# End of file.
|