aboutsummaryrefslogtreecommitdiff
path: root/textoutpc/builtin/_List.py
blob: af926d814f9d54827840d6c866bc13a62f21e5b4 (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
#!/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 BlockTag as _BlockTag

__all__ = ["ListTag", "ListElementTag"]


# Bullet style names.

_ol_list_style_types = {
	'disc':   'disc',
	'circle': 'circle',
	'square': 'square',
}
_ul_list_style_types = {
	'1':      'decimal',
	'a':      'lower-alpha',
	'A':      'upper-alpha',
	'i':      'lower-roman',
	'I':      'upper-roman',
}

_list_style_types = _ol_list_style_types.copy()
_list_style_types.update(_ul_list_style_types)

_ul_lst_names = set(_ul_list_style_types.keys())
_ol_lst_names = set(_ol_list_style_types.keys())

# Tag definitions.

class _ListTagBase(_BlockTag):
	pass

class ListElementTag(_BlockTag):
	""" List element for basic lists (see `ListTag`). """

	aliases = ('[*]', '[li]')
	only_in = (_ListTagBase,)
	notempty = True
	superblock = True
	not_within_itself = True

	def begin_html(self):
		return '<li>'

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

class ListTag(_ListTagBase):
	""" Main tag for making basic lists.
		Example use:

		[ul]
		[*] Item number one.
		[*] Item number [b]two[/b].
		[/ul]
	"""

	aliases = ('[list]', '[ul]', '[ol]')
	notempty = True
	superblock = True
	allowed_tags = (ListElementTag,)
	no_text = True

	def prepare(self, name, value):
		us = _ul_lst_names
		os = _ol_lst_names

		if   name == '[list]' and value == None:
			self._tag = 'ul'
			self._style = None
		elif name == '[list]' and value in us:
			self._tag = 'ul'
			self._style = value
		elif name == '[list]' and value in os:
			self._tag = 'ol'
			self._style = value
		elif name == '[ul]'   and value == None:
			self._tag = 'ul'
			self._style = None
		elif name == '[ul]'   and value in us:
			self._tag = 'ul'
			self._style = value
		elif name == '[ol]'   and value == None:
			self._tag = 'ol'
			self._style = None
		elif name == '[ol]'   and value in os:
			self._tag = 'ol'
			self._style = value
		else:
			raise ValueError("invalid bullet style")

		# Find out the HTML style name.

		if self._style != None:
			self._style = _list_style_types[self._style]

	def begin_html(self):
		tag = f'<{self._tag}'
		if self._style != None:
			tag += f' style="list-style-type: {self._style}"'
		tag += '>'

		return tag

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

# End of file.