2018-01-02 18:57:04 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
from .__base__ import *
|
|
|
|
from html import escape as _htmlescape
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
__all__ = ["TextoutLinkTag", "TextoutProfileTag"]
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
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]
|
|
|
|
"""
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
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):
|
2018-01-19 21:23:35 +01:00
|
|
|
self.url = None
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
# 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:
|
2018-01-05 22:13:30 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
# Otherwise, get the URL and validate.
|
2018-01-16 13:34:11 +01:00
|
|
|
self.url = value
|
2018-01-05 22:13:30 +01:00
|
|
|
self._validate()
|
|
|
|
|
2018-01-19 21:23:35 +01:00
|
|
|
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):
|
2018-01-05 22:13:30 +01:00
|
|
|
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):
|
2018-01-05 22:13:30 +01:00
|
|
|
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]
|
|
|
|
"""
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
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):
|
2018-01-05 22:13:30 +01:00
|
|
|
# FIXME: check the username content!
|
2018-01-19 11:23:19 +01:00
|
|
|
username = cin.read()
|
2018-01-05 22:13:30 +01:00
|
|
|
self.url = 'https://www.planet-casio.com/Fr/compte/voir_profil.php' \
|
2018-01-16 13:34:11 +01:00
|
|
|
'?membre={}'.format(content)
|
2018-01-05 22:13:30 +01:00
|
|
|
self._validate()
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
# End of file.
|