2018-01-02 21:21:27 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
from .__base__ import *
|
|
|
|
from .__color__ import *
|
2018-01-02 21:21:27 +01:00
|
|
|
|
|
|
|
__all__ = ["TextoutTextTag"]
|
|
|
|
|
|
|
|
# ---
|
|
|
|
# Data.
|
|
|
|
# ---
|
|
|
|
|
|
|
|
_fonts = {
|
|
|
|
"arial": "Arial",
|
|
|
|
"comic": "Comic MS",
|
|
|
|
"tahoma": "Tahoma",
|
|
|
|
"courier": "Courier",
|
|
|
|
"haettenschweiler": "Haettenschweiler",
|
|
|
|
"mono": "monospace",
|
|
|
|
"monospace": "monospace"
|
|
|
|
}
|
|
|
|
|
|
|
|
# ---
|
|
|
|
# Tag definition.
|
|
|
|
# ---
|
|
|
|
|
2018-01-05 03:31:33 +01:00
|
|
|
class TextoutTextTag(TextoutInlineTag):
|
2018-01-02 21:21:27 +01:00
|
|
|
""" Main tag for setting text formatting.
|
|
|
|
Example uses:
|
|
|
|
|
|
|
|
[b]Bold text.[/b]
|
|
|
|
[i]Italic text.[/i]
|
|
|
|
[u]Underlined text.[/u]
|
|
|
|
[strike]Striked text.[/strike]
|
|
|
|
[font=arial]Arial text.[/font]
|
|
|
|
[arial]Arial text again.[/arial]
|
|
|
|
[blue]This will be in blue[/blue]
|
|
|
|
[color=blue]This as well[/color]
|
2018-01-05 03:31:33 +01:00
|
|
|
[color=rgb(255, 255, 255, 0.4)]BLACKNESS[/color]
|
2018-01-02 21:21:27 +01:00
|
|
|
[color=hsl(0, 100%, 0.5)]This will be red.[/color]
|
|
|
|
"""
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
aliases = ('[b]', '[i]', '[u]', '[s]', '[strike]',
|
|
|
|
'[monospace]', '[mono]', '[font]', '[color]',
|
|
|
|
'[arial]', '[comic]', '[tahoma]', '[courier]',
|
|
|
|
'[haettenschweiler]', '[red]', '[green]', '[blue]',
|
|
|
|
'[yellow]', '[maroon]', '[purple]', '[gray]',
|
|
|
|
'[grey]', '[brown]')
|
|
|
|
|
2018-01-16 13:34:11 +01:00
|
|
|
def prepare(self, name, value):
|
2018-01-22 20:49:30 +01:00
|
|
|
self._bold = False
|
|
|
|
self._italic = False
|
|
|
|
self._underline = False
|
|
|
|
self._strike = False
|
|
|
|
self._font = None
|
|
|
|
self._color = None
|
2018-01-02 21:21:27 +01:00
|
|
|
|
2018-01-16 13:34:11 +01:00
|
|
|
name = name[1:-1]
|
|
|
|
if name == "b":
|
2018-01-22 20:49:30 +01:00
|
|
|
self._bold = True
|
2018-01-16 13:34:11 +01:00
|
|
|
elif name == "i":
|
2018-01-22 20:49:30 +01:00
|
|
|
self._italic = True
|
2018-01-16 13:34:11 +01:00
|
|
|
elif name == "u":
|
2018-01-22 20:49:30 +01:00
|
|
|
self._underline = True
|
2018-01-16 13:34:11 +01:00
|
|
|
elif name in ("s", "strike"):
|
2018-01-22 20:49:30 +01:00
|
|
|
self._strike = True
|
2018-01-16 13:34:11 +01:00
|
|
|
elif name == "color":
|
2018-01-22 20:49:30 +01:00
|
|
|
self._color = get_color(value)
|
2018-01-16 13:34:11 +01:00
|
|
|
elif name == "font":
|
|
|
|
assert value in _fonts
|
2018-01-22 20:49:30 +01:00
|
|
|
self._font = value
|
2018-01-16 13:34:11 +01:00
|
|
|
elif name in _fonts:
|
2018-01-22 20:49:30 +01:00
|
|
|
self._font = value
|
2018-01-02 21:21:27 +01:00
|
|
|
else:
|
2018-01-22 20:49:30 +01:00
|
|
|
self._color = get_color(name)
|
2018-01-05 22:13:30 +01:00
|
|
|
|
2018-01-02 21:21:27 +01:00
|
|
|
def begin_html(self):
|
|
|
|
props = []
|
2018-01-22 20:49:30 +01:00
|
|
|
if self._font:
|
|
|
|
props.append('font-family: ' + self._font)
|
|
|
|
if self._color:
|
|
|
|
# always append the #rgb color: it will be read by older
|
|
|
|
# browsers if the `rgba()` function isn't supported.
|
|
|
|
|
|
|
|
props.append('color: #%02X%02X%02X' % self._color[0:3])
|
|
|
|
if self._color[3] < 1.0:
|
|
|
|
props.append('color: rgba({}, {}, {}, {})' \
|
|
|
|
.format(*self._color))
|
|
|
|
|
|
|
|
self._has_props = bool(props)
|
2018-01-19 11:23:19 +01:00
|
|
|
props = '<span style="{}">'.format('; '.join(props)) if props else ''
|
2018-01-02 21:21:27 +01:00
|
|
|
|
|
|
|
return '' \
|
2018-01-22 20:49:30 +01:00
|
|
|
+ ('', '<b>')[self._bold] \
|
|
|
|
+ ('', '<i>')[self._italic] \
|
|
|
|
+ ('', '<u>')[self._underline] \
|
|
|
|
+ ('', '<strike>')[self._strike] \
|
2018-01-02 21:21:27 +01:00
|
|
|
+ props
|
|
|
|
|
|
|
|
def end_html(self):
|
2018-01-05 22:13:30 +01:00
|
|
|
return '' \
|
2018-01-22 20:49:30 +01:00
|
|
|
+ ('', '</span>')[self._has_props] \
|
|
|
|
+ ('', '</strike>')[self._strike] \
|
|
|
|
+ ('', '</u>')[self._underline] \
|
|
|
|
+ ('', '</i>')[self._italic] \
|
|
|
|
+ ('', '</b>')[self._bold]
|
2018-01-02 21:21:27 +01:00
|
|
|
|
|
|
|
# End of file.
|