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

182 lines
4.1 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
from .. import InlineTag as _InlineTag
from html import escape as _htmlescape
2018-01-02 18:57:04 +01:00
2018-07-30 14:03:40 +02:00
__all__ = ["LinkTag", "ProfileTag", "TopicTag", "TutorialTag", "ProgramTag"]
2018-01-02 18:57:04 +01:00
2018-08-25 17:38:24 +02:00
class LinkTag(_InlineTag):
2018-01-02 18:57:04 +01:00
""" The main link tag.
Example uses:
2018-08-25 17:38:24 +02:00
2018-01-02 18:57:04 +01:00
[url=https://example.org/hi]Go to example.org[/url]!
[url=/Fr/index.php][/url]
2018-07-30 14:03:40 +02:00
[url]https://random.org/randomize.php[/url] """
2018-01-02 18:57:04 +01:00
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-07-27 00:53:59 +02:00
2018-08-25 17:38:24 +02:00
if value is None:
self.preprocess = self._preprocess_if_no_value
return
# Otherwise, get the URL and validate.
2018-07-27 00:53:59 +02:00
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):
2019-07-30 13:35:49 +02:00
target = self.tweak("link_target", "").casefold()
tattrs = ''
if target == 'blank':
tattrs = ' target="_blank" rel="noopener"'
return '<a href="{}"{}>'.format(_htmlescape(self._url), tattrs)
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-08-25 17:38:24 +02:00
class ProfileTag(LinkTag):
2018-01-02 18:57:04 +01:00
""" A special link tag for Planète Casio's profiles.
Adds the prefix to the content, and sets the value.
Example uses:
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
[profil]Cakeisalie5[/] """
2018-01-02 18:57:04 +01:00
aliases = ('[profil]', '[profile]')
2018-01-16 13:34:11 +01:00
def prepare(self, name, value):
2018-07-30 14:03:40 +02:00
# Override the LinkTag's prepare method.
2018-07-27 00:53:59 +02:00
2018-01-02 18:57:04 +01:00
pass
def preprocess(self, content):
2018-07-30 14:03:40 +02:00
# Check the username's content (see `check(…, "pseudo")` in PCv42).
2018-07-27 00:53:59 +02:00
username = content
2018-07-30 14:03:40 +02:00
allowed = "abcdefghijklmnopqrstuvwxyz0123456789_ -."
2018-08-25 17:38:24 +02:00
if any(car not in allowed for car in allowed):
2018-07-30 14:03:40 +02:00
raise ValueError("invalid username!")
# Prepare the tag.
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
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
class TopicTag(LinkTag):
""" A special link tag for Planète Casio's topics.
Adds the prefix to the content, and sets the value.
Example uses:
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
[topic]234[/] """
aliases = ('[topic]',)
def prepare(self, name, value):
# Override the LinkTag's prepare method.
pass
def preprocess(self, content):
# Check the topic number.
topic = int(content)
# Prepare the tag.
self._url = 'https://www.planet-casio.com/Fr/forums/' \
f'lecture_sujet.php?id={topic}'
self._validate()
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
class TutorialTag(LinkTag):
""" A special link tag for Planète Casio's tutorial.
Adds the prefix to the content, and sets the value.
Example uses:
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
[tutorial]71[/tutorial]
[tuto]71[/tuto] """
aliases = ('[tutorial]', '[tuto]')
def prepare(self, name, value):
# Override the LinkTag's prepare method.
pass
def preprocess(self, content):
# Check the topic number.
topic = int(content)
# Prepare the tag.
self._url = 'https://www.planet-casio.com/Fr/programmation/' \
f'tutoriels.php?id={topic}'
self._validate()
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
class ProgramTag(LinkTag):
""" A special link tag for a Planète Casio's program.
Adds the prefix to the content, and sets the value.
Example uses:
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
[program]3598[/program]
[prog]3598[/prog] """
aliases = ('[program]', '[prog]')
def prepare(self, name, value):
# Override the LinkTag's prepare method.
pass
def preprocess(self, content):
# Check the program number.
program = int(content)
# Prepare the tag.
self._url = 'https://www.planet-casio.com/Fr/programmes/' \
f'voir_un_programme_casio.php?showid={program}'
self._validate()
2018-01-02 18:57:04 +01:00
# End of file.