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

107 lines
2.9 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
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]
"""
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):
self._width = None
self._height = None
self._align = None
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':
self._width = None
self._height = None
2018-01-02 18:57:04 +01:00
dim = arg.split('x')
try: self._width = int(dim[0])
2018-01-02 18:57:04 +01:00
except: pass
try: self._height = int(dim[1])
2018-01-02 18:57:04 +01:00
except: pass
elif arg in ('center', 'left', 'right'):
self._align = arg
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
def preprocess(self, content):
for prefix in ('http://', 'https://', 'ftp://', '/'):
2018-01-16 13:34:11 +01:00
if content.startswith(prefix):
break
else:
raise Exception("No allowed prefix!")
2018-01-02 18:57:04 +01:00
self._url = content
2018-01-19 11:23:19 +01:00
def content_html(self):
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:
cls.append('img-float')
if self._align:
cls.append('img-{}'.format(self._align))
2018-01-02 18:57:04 +01:00
return '<img src="{}"{}{} />'.format(\
_htmlescape(self._url),
' 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._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:
[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):
path = content
2018-01-16 13:34:11 +01:00
# FIXME: check image URL!
self._url = 'https://www.planet-casio.com/images/ad/' + path
2018-01-02 18:57:04 +01:00
# End of file.