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

81 lines
1.7 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import *
from html import escape as _htmlescape
2018-01-02 18:57:04 +01:00
__all__ = ["TextoutLinkTag", "TextoutProfileTag"]
class TextoutLinkTag(TextoutRawInlineTag):
2018-01-02 18:57:04 +01:00
""" The main link tag.
Example uses:
[url=https://example.org/hi]Go to example.org[/url]!
[url=/Fr/index.php][/url]
[url]https://random.org/randomize.php[/url]
"""
aliases = ('[url]',)
2018-01-02 18:57:04 +01:00
def _validate(self):
for prefix in ('http://', 'https://', 'ftp://', '/', '#'):
if self.url.startswith(prefix):
break
else:
raise Exception("No allowed prefix!")
2018-01-16 13:34:11 +01:00
def prepare(self, name, value):
self.url = None
# If there is no value, wait until we have a content to
# decide if we are valid or not.
2018-01-16 13:34:11 +01:00
if value == None:
return
# Otherwise, get the URL and validate.
2018-01-16 13:34:11 +01:00
self.url = value
self._validate()
def preprocess(self, cin):
content = cin.read()
if not self.url:
self.url = content
self._validate()
if not content:
content = self.url
return content
2018-01-02 18:57:04 +01:00
def end_text(self):
return ' (voir "{}")'.format(self.value)
2018-01-02 18:57:04 +01:00
def begin_html(self):
return '<a href="{}" target="_blank" rel="noopener">' \
2018-01-19 22:44:43 +01:00
.format(_htmlescape(self.url))
2018-01-02 18:57:04 +01:00
def end_html(self):
return '</a>'
2018-01-02 18:57:04 +01:00
class TextoutProfileTag(TextoutLinkTag):
""" A special link tag for Planète Casio's profiles.
Adds the prefix to the content, and sets the value.
Example uses:
[profil]Cakeisalie5[/profil]
"""
aliases = ('[profil]', '[profile]')
2018-01-16 13:34:11 +01:00
def prepare(self, name, value):
2018-01-02 18:57:04 +01:00
pass
2018-01-19 11:23:19 +01:00
def preprocess(self, cin):
# FIXME: check the username content!
2018-01-19 11:23:19 +01:00
username = cin.read()
self.url = 'https://www.planet-casio.com/Fr/compte/voir_profil.php' \
2018-01-16 13:34:11 +01:00
'?membre={}'.format(content)
self._validate()
2018-01-02 18:57:04 +01:00
# End of file.