2
0
Fork 0

Improved the organization with color decoding.

This commit is contained in:
Thomas Touhey 2018-01-02 21:21:27 +01:00
parent bcad5e9f32
commit c30d2a2b9c
No known key found for this signature in database
GPG key ID: 2ECEB0517AD947FB
7 changed files with 314 additions and 342 deletions

28
textoutpc/Tags/Align.py Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import TextoutTag
__all__ = ["TextoutAlignTag"]
class TextoutAlignTag(TextoutTag):
""" Main tag for aligning text.
Example uses:
[align=center]This text is centered horizontally.[/align]
[justify]This text is justified.[/justify]
"""
def _prepare(self):
if self.name != 'align':
self.value = self.name
if not self.value in ('center', 'justify'):
raise Exception
def begin_html(self):
return '<div style="text-align: {};">'.format(self.value)
def end_html(self):
return self.content + '</div>'
# End of file.

View file

@ -1,42 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import TextoutTag
__all__ = ["TextoutFontTag"]
_fonts = {
"arial": "Arial",
"comic": "Comic MS",
"tahoma": "Tahoma",
"courier": "Courier",
"haettenschweiler": "Haettenschweiler"
}
class TextoutFontTag(TextoutTag):
""" The main tag for setting the text font of the content.
Example uses:
[font=arial]This will be in arial[/font]
[arial]This as well[/arial]
"""
def _prepare(self):
if self.name == "font":
name = self.value
else:
if self.value != None:
raise Exception
name = self.name
self.font = _fonts[name.lower()]
def begin_html(self):
if not self.font:
return ""
return '<span style="font-family: {};">'.format(font)
def end_html(self):
return self.content + '</span>'
# End of file.

View file

@ -1,115 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import TextoutTag
__all__ = ["TextoutBoldTag", "TextoutItalicTag", "TextoutUnderlineTag",
"TextoutStrikeTag", "TextoutMonoTag", "TextoutAlignTag", "TextoutSizeTag"]
class TextoutBoldTag(TextoutTag):
""" Main tag for setting the weight to bold.
Example uses:
[b]Bold text.[/b]
"""
def begin_html(self):
return "<b>"
def end_html(self):
return self.content + "</b>"
class TextoutItalicTag(TextoutTag):
""" Main tag for displaying text in italic.
Example uses:
[i]Italic text.[/i]
"""
def begin_html(self):
return "<i>"
def end_html(self):
return self.content "</i>"
class TextoutUnderlineTag(TextoutTag):
""" Main tag for underlining text.
Example uses:
[u]Underlined text.[/u]
"""
def begin_html(self):
return "<u>"
def html(self):
return self.content + "</u>"
class TextoutStrikeTag(TextoutTag):
""" Main tag for striking text.
Example uses:
[strike]Cakeisalie5 is ugly.[/strike]
"""
def begin_html(self):
return "<strike>"
def end_html(self):
return self.content + "</strike>"
class TextoutMonoTag(TextoutTag):
""" Main tag for setting a monospace to text.
Example uses:
[mono]Monospace text![/mono]
"""
def begin_html(self):
return '<span style="font-family: monospace;">"
def end_html(self):
return self.content + '</span>'
class TextoutAlignTag(TextoutTag):
""" Main tag for aligning text.
Example uses:
[align=center]This text is centered horizontally.[/align]
[justify]This text is justified.[/justify]
"""
def _prepare(self):
if self.name != 'align':
self.value = self.name
if not self.value in ('center', 'justify'):
raise Exception
def begin_html(self):
return '<div style="text-align: {};">'.format(self.value)
def end_html(self):
return self.content + '</div>'
class TextoutSizeTag(TextoutTag):
""" Main tag for setting the font size.
Example uses:
[big]WOW, THIS IS BIG.[/big]
[size=small]and this is tiny.[/size]
"""
def _prepare(self):
if self.name != 'size':
self.value = self.name
if not self.value in ('small', 'big'):
raise Exception
def begin_html(self):
return '<span style="font-size: {};">' \
.format({'big': '15px', 'small': '9px'}[self.value])
def end_html(self):
return self.content + '</span>'
# End of file.

29
textoutpc/Tags/Size.py Executable file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import TextoutTag
__all__ = ["TextoutSizeTag"]
class TextoutSizeTag(TextoutTag):
""" Main tag for setting the font size.
Example uses:
[big]WOW, THIS IS BIG.[/big]
[size=small]and this is tiny.[/size]
"""
def _prepare(self):
if self.name != 'size':
self.value = self.name
if not self.value in ('small', 'big'):
raise Exception
def begin_html(self):
return '<span style="font-size: {};">' \
.format({'big': '15px', 'small': '9px'}[self.value])
def end_html(self):
return self.content + '</span>'
# End of file.

100
textoutpc/Tags/Text.py Executable file
View file

@ -0,0 +1,100 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .__base__ import TextoutTag
from .__color__ import get_color
__all__ = ["TextoutTextTag"]
# ---
# Data.
# ---
_fonts = {
"arial": "Arial",
"comic": "Comic MS",
"tahoma": "Tahoma",
"courier": "Courier",
"haettenschweiler": "Haettenschweiler",
"mono": "monospace",
"monospace": "monospace"
}
# ---
# Tag definition.
# ---
class TextoutTextTag(TextoutTag):
""" 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)]BLACKNESS[/color]
[color=hsl(0, 100%, 0.5)]This will be red.[/color]
"""
def prepare(self):
self.bold = False
self.italic = False
self.underline = False
self.strike = False
self.font = None
self.color = None
if self.name == "b":
self.bold = True
elif self.name == "i":
self.italic = True
elif self.name == "u":
self.underline = True
elif self.name == "strike":
self.strike = True
elif self.name == "color":
self.color = get_color(self.value)
elif self.name == "font":
if not self.value in _fonts:
raise Exception
else:
self.font = self.value
elif self.name in _fonts:
self.font = self.value
else:
self.color = get_color(self.name)
def begin_html(self):
props = []
if self.font:
props.append('font-family: ' + self.font)
if self.color:
if self.color[3] < 255:
col = 'rgba({}, {}, {}, {})'.format(*self.color)
else:
col = '#%02X%02X%02X' \
% (self.color[0], self.color[1], self.color[2])
props.append('color: ' + col)
if props:
props = '<span style="{}">'.format('; '.join(props))
return '' \
+ ('', '<b>')[self.bold] \
+ ('', '<i>')[self.italic] \
+ ('', '<u>')[self.underline] \
+ ('', '<strike>')[self.strike] \
+ props
def end_html(self):
return self.content \
+ ('', '</u>')[self.underline] \
+ ('', '</i>')[self.italic] \
+ ('', '</b>')[self.bold] \
+ ('', '</strike>')[self.strike] \
+ ('', '</span>')[self.font != None or self.color != None]
# End of file.

View file

@ -1,16 +1,17 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re as _re, colorsys as _color, math as _math
from .__base__ import TextoutTag
import re as _re
import colorsys as _color
import math as _math
__all__ = ['TextoutColorTag']
__all__ = ["get_color"]
# ---
# Colors per color name.
# Color names and correspondances.
# ---
_cols = {
_colors = {
# Standard CSS color names.
'aliceblue': '#F0F8FF', 'antiquewhite': '#FAEBD7', 'aqua': '#00FFFF',
'aquamarine': '#7FFFD4', 'azure': '#F0FFFF', 'beige': '#F5F5DC',
@ -70,10 +71,6 @@ _cols = {
'yellow': '#FFFF00', 'yellowgreen': '#9ACD32'
}
# ---
# Main CSS color expression.
# ---
_cr = _re.compile('^\s*('
'#'
'(?P<thr_r>[0-9a-f])'
@ -83,29 +80,31 @@ _cr = _re.compile('^\s*('
'(?P<six_r>[0-9a-f]{2})'
'(?P<six_g>[0-9a-f]{2})'
'(?P<six_b>[0-9a-f]{2})'
'|rgb\s*\('
'|rgba?\s*\('
'\s*' '(?P<rgb_r>[0-9]{1,3})' '\s*,'
'\s*' '(?P<rgb_g>[0-9]{1,3})' '\s*,'
'\s*' '(?P<rgb_b>[0-9]{1,3})' '\s*'
'\s*' '(?P<rgb_b>[0-9]{1,3})' '\s*(,'
'\s*' '((?P<rgb_a_per>0*[0-9]{0,3}(\.[0-9]*)?)%'
'|(?P<rgb_a_flt>0*(\.[0-9]*)?))' '\s*)?'
'\)'
'|hsla?\s*\('
'\s*' '(?P<hsl_hue>-?0*[0-9]{1,3}(\.[0-9]*)?)'
'(?P<hsl_agl>deg|grad|rad|turn|)' '\s*[,\\s]'
'\s*' '((?P<hsl_sat_per>0*[0-9]{1,3})%'
'\s*' '((?P<hsl_sat_per>0*[0-9]{1,3}(\.[0-9]*)?)%'
'|(?P<hsl_sat_flt>0*(\.[0-9]*)?))' '\s*[,\\s]'
'\s*' '((?P<hsl_lgt_per>0*[0-9]{1,3})%'
'\s*' '((?P<hsl_lgt_per>0*[0-9]{1,3}(\.[0-9]*)?)%'
'|(?P<hsl_lgt_flt>0*(\.[0-9]*)?))' '\s*([,\\s]'
'\s*' '((?P<hsl_aph_per>0*[0-9]{0,3})%?'
'\s*' '((?P<hsl_aph_per>0*[0-9]{0,3}(\.[0-9]*)?)%'
'|(?P<hsl_aph_flt>0*(\.[0-9]*)?))' '\s*)?'
'\)'
'|hlsa?\s*\('
'\s*' '(?P<hls_hue>-?0*[0-9]{1,3}(\.[0-9]*)?)'
'(?P<hls_agl>deg|grad|rad|turn|)' '\s*[,\\s]'
'\s*' '((?P<hls_lgt_per>0*[0-9]{1,3})%'
'\s*' '((?P<hls_lgt_per>0*[0-9]{1,3}(\.[0-9]*)?)%'
'|(?P<hls_lgt_flt>0*(\.[0-9]*)?))' '\s*[,\\s]'
'\s*' '((?P<hls_sat_per>0*[0-9]{1,3})%'
'\s*' '((?P<hls_sat_per>0*[0-9]{1,3}(\.[0-9]*)?)%'
'|(?P<hls_sat_flt>0*(\.[0-9]*)?))' '\s*([,\\s]'
'\s*' '((?P<hls_aph_per>0*[0-9]{0,3})%?'
'\s*' '((?P<hls_aph_per>0*[0-9]{0,3}(\.[0-9]*)?)%'
'|(?P<hls_aph_flt>0*(\.[0-9]*)?))' '\s*)?'
'\)'
# '|hwb\s*\('
@ -119,169 +118,156 @@ _cr = _re.compile('^\s*('
')\s*$', _re.I)
# ---
# HWB to RGB.
# Algorithm taken from here: https://drafts.csswg.org/css-color/#hwb-to-rgb
# Utilitaires.
# ---
def _hwb_to_rgb(h, w, b):
""" Convert HWB to RGB color. """
""" Convert HWB to RGB color.
https://drafts.csswg.org/css-color/#hwb-to-rgb """
r, g, b = _color.hls_to_rgb(h, 0.5, 1.0)
f = lambda x: x * (1 - w - b) + w
return f(r), f(g), f(b)
# ---
# Main tag definition.
# Main function.
# ---
class TextoutColorTag(TextoutTag):
""" The main tag for setting the text color.
Example uses:
[blue]This will be in blue[/blue]
[color=blue]This as well[/color]
[color=rgb(255, 255, 255)]BLACKNESS[/color]
[color=hsl(0, 100%, 0.5)]This will be red.[/color]
"""
def get_color(value):
""" Get a color from a string.
Returns an (r, g, b, a) color.
Raises an exception if there's a problem. """
def _prepare(self):
if self.name != 'color':
self.value = self.name
# Check if is a color name.
try: value = _colors[value]
except: pass
# Check if is a color name.
value = self.value
try: value = _cols[self.value]
except: pass
# Initialize the alpha.
alpha = 1.0
# Initialize the alpha.
self.a = None
# Get the match.
match = _cr.match(value).groupdict()
if match['thr_r']:
r = int(match['thr_r'] * 2, 16)
g = int(match['thr_g'] * 2, 16)
b = int(match['thr_b'] * 2, 16)
elif match['six_r']:
r = int(match['six_r'], 16)
g = int(match['six_g'], 16)
b = int(match['six_b'], 16)
elif match['rgb_r']:
r = int(match['rgb_r'])
g = int(match['rgb_g'])
b = int(match['rgb_b'])
# Get the match.
g = _cr.match(value).groupdict()
if g['thr_r']:
self.r = int(g['thr_r'] * 2, 16)
self.g = int(g['thr_g'] * 2, 16)
self.b = int(g['thr_b'] * 2, 16)
elif g['six_r']:
self.r = int(g['six_r'], 16)
self.g = int(g['six_g'], 16)
self.b = int(g['six_b'], 16)
elif g['rgb_r']:
self.r = int(g['rgb_r'])
self.g = int(g['rgb_g'])
self.b = int(g['rgb_b'])
if self.r > 255 or self.g > 255 or self.b > 255:
raise Exception
elif g['hsl_hue'] or g['hls_hue']:
if g['hsl_hue']:
hue = float(g['hsl_hue'])
agl = g['hsl_agl']
# Saturation.
if g['hsl_sat_per']:
sat = float(g['hsl_sat_per']) / 100.0
else:
sat = float(g['hsl_sat_flt'])
if sat > 1.0:
sat /= 100.0
# Light.
if g['hsl_lgt_per']:
lgt = float(g['hsl_lgt_per']) / 100.0
else:
lgt = float(g['hsl_lgt_flt'])
if lgt > 1.0:
lgt /= 100.0
# Alpha value.
if g['hsl_aph_per']:
alpha = float(g['hsl_aph_per']) / 100.0
elif g['hsl_aph_flt']:
alpha = float(g['hsl_aph_flt'])
else:
alpha = None
else:
hue = float(g['hls_hue'])
agl = g['hls_agl']
# Saturation.
if g['hls_sat_per']:
sat = float(g['hls_sat_per']) / 100.0
else:
sat = float(g['hls_sat_flt'])
# Light.
if g['hls_lgt_per']:
lgt = float(g['hls_lgt_per']) / 100.0
else:
lgt = float(g['hls_lgt_flt'])
# Alpha value.
if g['hls_aph_per']:
alpha = float(g['hls_aph_per']) / 100.0
elif g['hls_aph_flt']:
alpha = float(g['hls_aph_flt'])
else:
alpha = None
# Prepare the angle.
if agl == 'grad':
hue = hue * 400.0
elif agl == 'rad':
hue = hue / (2 * math.pi)
elif not agl or agl == 'deg':
hue = hue / 360.0
hue = hue % 1.0
if sat > 1 or lgt > 1:
raise Exception
r, g, b = _color.hls_to_rgb(hue, lgt, sat)
self.r, self.g, self.b = map(lambda x:int(round(x * 255)),
(r, g, b))
self.a = alpha
elif g['hwb_hue']:
hue = float(g['hwb_hue'])
agl = g['hwb_agl']
# Alpha value.
if match['rgb_a_per']:
alpha = float(match['rgb_a_per']) / 100.0
elif match['rgb_a_flt']:
alpha = float(match['rgb_a_flt'])
else:
alpha = 1.0
elif match['hsl_hue'] or match['hls_hue']:
if match['hsl_hue']:
hue = float(match['hsl_hue'])
agl = match['hsl_agl']
# Saturation.
if g['hwb_wht_per']:
wht = float(g['hwb_wht_per']) / 100.0
if match['hsl_sat_per']:
sat = float(match['hsl_sat_per']) / 100.0
else:
wht = float(g['hwb_wht_flt'])
sat = float(match['hsl_sat_flt'])
if sat > 1.0:
sat /= 100.0
# Light.
if g['hwb_blk_per']:
blk = float(g['hwb_blk_per']) / 100.0
if match['hsl_lgt_per']:
lgt = float(match['hsl_lgt_per']) / 100.0
else:
blk = float(g['hwb_blk_flt'])
lgt = float(match['hsl_lgt_flt'])
if lgt > 1.0:
lgt /= 100.0
# Prepare the angle.
if agl == 'grad':
hue = hue * 400.0
elif agl == 'rad':
hue = hue / (2 * math.pi)
elif not agl or agl == 'deg':
hue = hue / 360.0
hue = hue % 1.0
if wht > 1 or blk > 1:
raise Exception
r, g, b = _hwb_to_rgb(hue, wht, blk)
self.r, self.g, self.b = map(lambda x:int(round(x * 255)),
(r, g, b))
def begin_html(self):
if self.a != None:
color = "rgba({}, {}, {}, {})".format(self.r, self.g, self.b,
self.a)
# Alpha value.
if match['hsl_aph_per']:
alpha = float(match['hsl_aph_per']) / 100.0
elif match['hsl_aph_flt']:
alpha = float(match['hsl_aph_flt'])
else:
alpha = 1.0
else:
color = "#%02X%02X%02X" % (self.r, self.g, self.b)
hue = float(match['hls_hue'])
agl = match['hls_agl']
return '<span style="color: {}">'.format(color)
# Saturation.
if match['hls_sat_per']:
sat = float(match['hls_sat_per']) / 100.0
else:
sat = float(match['hls_sat_flt'])
def end_html(self):
return self.content + '</span>'
# Light.
if match['hls_lgt_per']:
lgt = float(match['hls_lgt_per']) / 100.0
else:
lgt = float(match['hls_lgt_flt'])
# Alpha value.
if match['hls_aph_per']:
alpha = float(match['hls_aph_per']) / 100.0
elif match['hls_aph_flt']:
alpha = float(match['hls_aph_flt'])
else:
alpha = 1.0
# Prepare the angle.
if agl == 'grad':
hue = hue * 400.0
elif agl == 'rad':
hue = hue / (2 * math.pi)
elif not agl or agl == 'deg':
hue = hue / 360.0
hue = hue % 1.0
if sat > 1 or lgt > 1:
raise Exception
r, g, b = _color.hls_to_rgb(hue, lgt, sat)
r, g, b = map(lambda x:int(round(x * 255)), (r, g, b))
elif match['hwb_hue']:
hue = float(match['hwb_hue'])
agl = match['hwb_agl']
# Saturation.
if match['hwb_wht_per']:
wht = float(match['hwb_wht_per']) / 100.0
else:
wht = float(match['hwb_wht_flt'])
# Light.
if match['hwb_blk_per']:
blk = float(match['hwb_blk_per']) / 100.0
else:
blk = float(match['hwb_blk_flt'])
# Prepare the angle.
if agl == 'grad':
hue = hue * 400.0
elif agl == 'rad':
hue = hue / (2 * math.pi)
elif not agl or agl == 'deg':
hue = hue / 360.0
hue = hue % 1.0
if wht > 1 or blk > 1:
raise Exception
r, g, b = _hwb_to_rgb(hue, wht, blk)
r, g, b = map(lambda x:int(round(x * 255)), (r, g, b))
if r < 0 or r > 255 or g < 0 or g > 255 or b < 0 or b > 255:
raise Exception
if alpha < 0.0 or alpha > 1.0:
raise Exception
return (r, g, b, alpha)
# End of file.

View file

@ -1,49 +1,35 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .Simple import *
from .Color import *
from .Font import *
from .Align import *
from .Code import *
from .Image import *
from .Label import *
from .Link import *
from .Image import *
from .Code import *
from .Progress import *
from .Quote import *
from .Size import *
from .Spoiler import *
from .Text import *
from .Video import *
__all__ = ["tags"]
tags = {
'b': TextoutBoldTag,
'i': TextoutItalicTag,
'u': TextoutUnderlineTag,
'strike': TextoutStrikeTag,
'monospace': TextoutMonoTag,
'mono': TextoutMonoTag,
'b': TextoutTextTag, 'i': TextoutTextTag, 'u': TextoutTextTag,
'strike': TextoutTextTag,
'monospace': TextoutTextTag, 'mono': TextoutTextTag,
'arial': TextoutTextTag, 'comic': TextoutTextTag, 'tahoma': TextoutTextTag,
'courier': TextoutTextTag, 'haettenschweiler': TextoutTextTag,
'color': TextoutTextTag, 'red': TextoutTextTag, 'blue': TextoutTextTag,
'green': TextoutTextTag, 'yellow': TextoutTextTag,
'maroon': TextoutTextTag, 'purple': TextoutTextTag, 'gray': TextoutTextTag,
'grey': TextoutTextTag, 'brown': TextoutTextTag,
'`': TextoutInlineCodeTag,
'inlinecode': TextoutInlineCodeTag,
# 'noeval': TextoutRepeatTag,
'arial': TextoutFontTag,
'comic': TextoutFontTag,
'tahoma': TextoutFontTag,
'courier': TextoutFontTag,
'haettenschweiler': TextoutFontTag,
'color': TextoutColorTag,
'red': TextoutColorTag,
'blue': TextoutColorTag,
'green': TextoutColorTag,
'yellow': TextoutColorTag,
'maroon': TextoutColorTag,
'purple': TextoutColorTag,
'gray': TextoutColorTag,
'grey': TextoutColorTag,
'brown': TextoutColorTag,
'url': TextoutLinkTag,
'profil': TextoutProfileTag,
'profile': TextoutProfileTag,