2
0
Fork 0
textout/textoutpc/tags/builtin/Link.py

87 lines
2.2 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-02-19 20:13:10 +01:00
from ..base import TextoutInlineTag as _TextoutInlineTag
from html import escape as _htmlescape
2018-01-02 18:57:04 +01:00
__all__ = ["TextoutLinkTag", "TextoutProfileTag"]
2018-02-19 20:13:10 +01:00
class TextoutLinkTag(_TextoutInlineTag):
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]',)
raw = True
2018-01-02 18:57:04 +01:00
def _validate(self):
for prefix in ('http://', 'https://', 'ftp://', '/', '#'):
if self._url.startswith(prefix):
2018-01-02 18:57:04 +01:00
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:
self.preprocess = self._preprocess_if_no_value
return
# Otherwise, get the URL and validate.
self._url = value
self._validate()
2018-01-22 20:33:25 +01:00
self.default = self._default_if_value
2018-01-22 20:33:25 +01:00
def _default_if_value(self):
return self._url
def _preprocess_if_no_value(self, content):
self._url = content
self._validate()
2018-01-02 18:57:04 +01:00
def begin_html(self):
2018-02-11 21:31:39 +01:00
return '<a href="{}">'.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
def begin_lightscript(self):
return '['
def end_lightscript(self):
url = self._url.replace('(', '%28').replace(')', '%29')
return ']({})'.format(url)
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):
# Override the TextoutLinkTag's prepare method.
2018-01-02 18:57:04 +01:00
pass
def preprocess(self, content):
# FIXME: check the username content!
username = content
self._url = 'https://www.planet-casio.com/Fr/compte/voir_profil.php' \
'?membre={}'.format(username)
self._validate()
2018-01-02 18:57:04 +01:00
# End of file.