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

85 lines
2.2 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 *
2018-01-02 18:57:04 +01:00
__all__ = ["TextoutImageTag", "TextoutAdminImageTag"]
class TextoutImageTag(TextoutRawBlockTag):
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]',)
2018-01-02 18:57:04 +01:00
2018-01-16 13:34:11 +01:00
def prepare(self, name, value):
2018-01-02 18:57:04 +01:00
self.width = None
self.height = None
self.align = None
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':
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
2018-01-16 13:34:11 +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
2018-01-16 13:34:11 +01:00
def process_text(self, content):
return '![ {} ]!'.format(content)
2018-01-02 18:57:04 +01:00
2018-01-16 13:34:11 +01:00
def process_html(self, content):
2018-01-02 18:57:04 +01:00
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')
2018-01-16 13:34:11 +01:00
return '<img src="{}"{}{}>'.format(self._geturl(_htmlescape(content)),
2018-01-02 18:57:04 +01:00
' 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[/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]',)
2018-01-16 13:34:11 +01:00
def preprocess(self, content):
# FIXME: check image URL!
return 'https://www.planet-casio.com/images/ad/' + content
2018-01-02 18:57:04 +01:00
# End of file.