2
0
Fork 0
textout/textoutpc/Tags/Text.py

109 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import *
from .__color__ import *
__all__ = ["TextoutTextTag"]
# ---
# Data.
# ---
_fonts = {
"arial": "Arial",
"comic": "Comic MS",
"tahoma": "Tahoma",
"courier": "Courier",
"haettenschweiler": "Haettenschweiler",
"mono": "monospace",
"monospace": "monospace"
}
# ---
# Tag definition.
# ---
class TextoutTextTag(TextoutInlineTag):
""" 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]
[color=rgb(255, 255, 255, 0.4)]BLACKNESS[/color]
[color=hsl(0, 100%, 0.5)]This will be red.[/color]
"""
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-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
else:
2018-01-22 20:49:30 +01:00
self._color = get_color(name)
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 ''
return '' \
2018-01-22 20:49:30 +01:00
+ ('', '<b>')[self._bold] \
+ ('', '<i>')[self._italic] \
+ ('', '<u>')[self._underline] \
+ ('', '<strike>')[self._strike] \
+ props
def end_html(self):
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]
# End of file.