aboutsummaryrefslogtreecommitdiff
path: root/textoutpc/builtin/_Label.py
blob: 37159173bb7d8445cd082899a8643ae946f8b4b8 (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
#!/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
import re as _re

__all__ = ["LabelTag", "TargetTag"]

_labelexpr = _re.compile('^[a-z0-9-]{1,16}$', _re.I)


class LabelTag(_InlineTag):
	""" The label tag, defines an anchor at a point of the post.
		Example uses:

		[label=installation]Installation de tel logiciel... (no ending req.)
		[label=compilation][/label] Compilation de tel logiciel...
	"""

	aliases = ('[label]',)

	def prepare(self, name, value):
		if not _labelexpr.match(value):
			raise Exception
		self._label = value

	def begin_html(self):
		#name = 'label-{}'.format(self._label)
		#if _v42compat:
		#	name += ' ' + self._label
		name = self.tweak("label_prefix", "") + self._label
		return '<a name="{}"></a>'.format(name)


class TargetTag(_InlineTag):
	""" The goto tag, links to an anchor defined in the post.
		Example uses:

		[target=installation]Check out the installation manual[/target]!
	"""

	aliases = ('[target]',)

	def prepare(self, name, value):
		if not _labelexpr.match(value):
			raise Exception
		self._label = value

	def begin_html(self):
		#name = 'label-' + self._label
		name = self.tweak("label_prefix", "") + self._label
		return '<a href="#{}">'.format(name)

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

# End of file.