aboutsummaryrefslogtreecommitdiff
path: root/textoutpc/builtin/_Link.py
blob: efe4a5afc18b4a3348b8e6779ed5ef01a18d47ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/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.
#******************************************************************************

from .. import InlineTag as _InlineTag
from html import escape as _htmlescape

__all__ = ["LinkTag", "ProfileTag", "TopicTag", "TutorialTag", "ProgramTag"]


class LinkTag(_InlineTag):
	""" 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

	def _validate(self):
		for prefix in ('http://', 'https://', 'ftp://', '/', '#'):
			if self._url.startswith(prefix):
				break
		else:
			raise Exception("No allowed prefix!")

	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.

		if value is None:
			self.preprocess = self._preprocess_if_no_value
			return

		# Otherwise, get the URL and validate.

		self._url = value
		self._validate()
		self.default = self._default_if_value

	def _default_if_value(self):
		return self._url

	def _preprocess_if_no_value(self, content):
		self._url = content
		self._validate()

	def begin_html(self):
		return '<a href="{}">'.format(_htmlescape(self._url))

	def end_html(self):
		return '</a>'

	def begin_lightscript(self):
		return '['

	def end_lightscript(self):
		url = self._url.replace('(', '%28').replace(')', '%29')
		return ']({})'.format(url)


class ProfileTag(LinkTag):
	""" A special link tag for Planète Casio's profiles.
		Adds the prefix to the content, and sets the value.
		Example uses:

		[profil]Cakeisalie5[/] """

	aliases = ('[profil]', '[profile]')

	def prepare(self, name, value):
		# Override the LinkTag's prepare method.

		pass

	def preprocess(self, content):
		# Check the username's content (see `check(…, "pseudo")` in PCv42).

		username = content
		allowed = "abcdefghijklmnopqrstuvwxyz0123456789_ -."
		if any(car not in allowed for car in allowed):
			raise ValueError("invalid username!")

		# Prepare the tag.

		self._url = 'https://www.planet-casio.com/Fr/compte/voir_profil.php' \
			'?membre={}'.format(username)
		self._validate()


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:

		[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()


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:

		[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()


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:

		[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()

# End of file.