2
0
Fork 0

Better smiley implementation.

This commit is contained in:
Thomas Touhey 2018-07-28 19:36:43 +02:00
parent a2fdc5d5a0
commit 8d9d4360ed
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
28 changed files with 404 additions and 248 deletions

View File

@ -28,6 +28,7 @@ There are a few public members you can define as a tag:
level, and adds a paragraph tag implicitely.
- ``inlined``: if is a block, transforms automatically the surrounding block
into a superblock while it's there.
- ``procvalue``: process the value as normal text before passing it.
So for example, if I want to make the inline tag ``[hello]`` as an example,
with the alternate name ``[hai]``, I'd start off by writing:

View File

@ -7,27 +7,38 @@
Really simplifies the thing.
"""
import io as _io
from .translate import Translator as _Translator
from io import StringIO as _StringIO
__all__ = ["version", "tohtml", "tolightscript"]
from ._options import TextoutOptions as Options, \
TextoutBlockTag as BlockTag, TextoutInlineTag as InlineTag, \
TextoutParagraphTag as ParagraphTag, TextoutSmiley as Smiley
from ._translate import Translator as _Translator
__all__ = ["version", "tohtml", "tolightscript",
"Options", "BlockTag", "ParagraphTag", "InlineTag", "Smiley"]
version = "0.1.1"
def tohtml(message, **tweaks):
# ---
# Public functions.
# ---
_default_options = Options()
def tohtml(message, options = _default_options, **tweaks):
""" Converts textout BBcode to HTML.
Receives a string, returns a string. """
return _Translator(_io.StringIO(message), _io.StringIO(), 'html', \
tweaks).process().getvalue()
return _Translator(_StringIO(message), _StringIO(), 'html', \
tweaks, options).process().getvalue()
def tolightscript(message, **tweaks):
def tolightscript(message, options = _default_options, **tweaks):
""" Converts textout BBcode to Lightscript.
Receives a string, returns a string. """
return "" # TODO: real thing one day
return _Translator(_io.StringIO(message), _io.StringIO(), 'lightscript', \
tweaks).process().getvalue()
return _Translator(_StringIO(message), _StringIO(), 'lightscript', \
tweaks, options).process().getvalue()
# End of file.

67
textoutpc/_html.py Normal file
View File

@ -0,0 +1,67 @@
#!/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.
#******************************************************************************
""" Utilities for HTML conversions. """
import regex as _re
from html import escape
__all__ = ["escape", "urls", "SmileyConvertor"]
# ---
# Smileys.
# ---
class SmileyConvertor:
""" Smileys convertor. """
def __init__(self, smileys = {}):
self._html = {escape(a): b.url \
for a, b in smileys.items() if b.url != None}
self._re = _re.compile('(^|\\s)(' + '|'.join(map(_re.escape,
self._html.keys())) + ')(\\s|$)')
def convert(self, text):
def sub_html(m):
return m.group(1) + '<img src="' + self._html[m.group(2)] \
+ '">' + m.group(3)
# FIXME: this is to avoid the ":):)" case, but there probably
# is a cleaner way to fix this…
text = self._re.sub(sub_html, text)
text = self._re.sub(sub_html, text)
return text
# ---
# URLs.
# ---
_urlreg = _re.compile("""\
(?P<sp>^|\s|[[:punct:]])
(?P<url>(https?|ftp):
(?P<ucore>[^\[\]\(\)\s]* (\[(?&ucore)\]?)* (\((?&ucore)\)?)*)*
)
""", _re.VERBOSE | _re.M)
def urls(text):
""" Convert URLs. """
def _sub_html(m):
sp = m.group('sp')
url = m.group('url')
aft = ''
# Hack for the last comma.
if url[-1] == ',':
url, aft = url[:-1], ','
text = '{}<a href="{}">{}</a>{}' \
.format(sp, url, url, aft)
return text
return _urlreg.sub(_sub_html, text)
# End of file.

View File

@ -3,7 +3,8 @@
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" Base classes to use with tags in textoutpc, with a manager class.
""" Base classes to use with options (tags, smileys) in textoutpc, with a
manager class.
For your tag to be used as a textoutpc tag, you have to make it
inherit one of the `TextoutBlockTag` or `TextoutInlineTag` classes.
@ -17,8 +18,11 @@ from inspect import ismodule as _ismod, isclass as _isclass, \
currentframe as _currentframe, getouterframes as _getouterframes
from importlib import import_module as _importmod
__all__ = ["TextoutTags", "TextoutTag", "TextoutBlockTag", "TextoutInlineTag",
"TextoutParagraphTag"]
from ._html import SmileyConvertor as _htmlsm
__all__ = ["TextoutOptions",
"TextoutTag", "TextoutBlockTag", "TextoutInlineTag", "TextoutParagraphTag",
"TextoutSmiley"]
def _getargscount(func):
try:
@ -27,10 +31,12 @@ def _getargscount(func):
return len(_getargspec(func).args)
# ---
# Main base tag class.
# For more about defining a tag, see `/TAGS.md`.
# Tags.
# ---
# Main base tag class.
# For more about defining a tag, see `doc/tags.md`.
class TextoutTag:
""" The textout tag base class.
Is initialized with these values:
@ -82,6 +88,7 @@ class TextoutTag:
prep(*args)
# Prepare the preprocessing elements.
if hasattr(self, 'preprocess'):
if hasattr(self, 'preprocess_' + ot):
self.__preprocess0 = self.preprocess
@ -98,6 +105,11 @@ class TextoutTag:
if hasattr(self, 'default_' + ot):
self.default = getattr(self, 'default_' + ot)
def __repr__(self):
return f"{self.__class__.__name__}(name = {repr(self.__name)}, " \
f"value = {repr(self.__value)}, " \
f"ot = {repr(self.__output_type)})"
def __preprocess_double(self, content):
""" Preprocess using the two methods. """
@ -141,18 +153,14 @@ class TextoutTag:
except KeyError:
return default
# ---
# Role-specific base tag classes.
# ---
class TextoutBlockTag(TextoutTag):
pass
class TextoutInlineTag(TextoutTag):
pass
# ---
# Default tag: paragraph.
# ---
class TextoutParagraphTag(TextoutBlockTag):
""" Main tag for basic paragraphs. """
@ -166,22 +174,51 @@ class TextoutParagraphTag(TextoutBlockTag):
return '</p>'
# ---
# Tag extractor.
# Smileys.
# ---
class TextoutTags:
""" Tag manager.
class TextoutSmiley:
""" Base class for smileys. """
aliases = ()
url = None
def __repr__(self):
return f"{self.__class__.__name__}(aliases = {repr(self.aliases)}, " \
f"url = {repr(self.url)})"
# ---
# Options extractor and manager.
# ---
_builtin_module = None
def _get_builtin_module():
""" Get the `.builtin` module. """
global _builtin_module
if _builtin_module == None:
_builtin_module = _importmod('..builtin', __name__)
return _builtin_module
class TextoutOptions:
""" Options manager.
Object responsible for getting the tags. """
def __init__(self, *modules):
def __init__(self, *modules, default = True):
self._aliases = {}
for mod in modules:
self.import_tags(mod)
self._s_aliases = {}
def __extract_tags(self, module):
""" Extract tags from a module. """
if default:
self.import_options(_get_builtin_module())
for mod in modules:
self.import_options(mod)
def __extract_from(self, module):
""" Extract options from a module. """
tags = []
smileys = []
# Obtain the list of properties from the module.
@ -196,32 +233,61 @@ class TextoutTags:
for submodule in (obj for name, obj in ((nm, getattr(module, nm)) \
for nm in ds) if (name == '__init__' or name[0] != '_') \
and _ismod(obj)):
obtained = self.__extract_tags(submodule)
tags += [tag for tag in obtained \
ob_tags, ob_smileys = self.__extract_tags(submodule)
tags += [tag for tag in ob_tags \
if not any(tag is x for x in tags)]
smileys += [sm for sm in ob_smileys \
if not any(sm is x for x in smileys)]
del obtained
# Extract the tags from the current module.
for tag in (obj for name, obj in ((nm, getattr(module, nm)) \
for nm in ds) if name[0] != '_' and _isclass(obj) \
and issubclass(obj, TextoutTag)):
tags.append(tag)
for obj in (obj for name, obj in ((nm, getattr(module, nm)) \
for nm in ds) if name[0] != '_'):
if _isclass(obj) and issubclass(obj, TextoutTag):
tags += [tag for tag in (obj,) \
if not any(tag is x for x in tags)]
if _isclass(obj) and issubclass(obj, TextoutSmiley):
smileys += [sm for sm in (obj,) \
if not any(sm is x for x in smileys)]
return tags
return tags, smileys
def import_tags(self, module):
""" Import tags from a dedicated module. """
def import_options(self, module):
""" Import tags and smileys from a dedicated module. """
if not _ismod(module):
if isinstance(module, str):
module = str(module)
module = _importmod(module,
_getouterframes(_currentframe(), 1)[0].name)
for tag in self.__extract_tags(module):
if not _ismod(module):
raise ValueError(f"expected a module, got: {repr(module)}")
tags, smileys = self.__extract_from(module)
for tag in tags:
for alias in tag.aliases:
self._aliases[alias] = tag
for sm in smileys:
for alias in sm.aliases:
self._s_aliases[alias] = sm
self._htmlsm = None
def get_tag(self, name, value, output_type = 'html', tweaks = {}):
""" Initialize a tag. """
def get_smileys(self):
""" Get the smileys dictionary. """
return self._s_aliases.copy()
def htmlsmileys(self, text):
""" Get the smileys convertor for HTML. """
if not self._htmlsm:
self._htmlsm = _htmlsm(self._s_aliases)
return self._htmlsm.convert(text)
def get_tag(self, name):
""" Get the tag class corresponding to a name. """
return self._aliases[name]
try:
als = self._aliases[name]

View File

@ -9,21 +9,14 @@
import string as _string
from copy import deepcopy as _deepcopy
from html import escape as _htmlescape
from importlib import import_module as _importmod
from .tags import TextoutBlockTag as _TextoutBlockTag, \
TextoutParagraphTag as _TextoutParagraphTag, TextoutTags as _Tags
from .stream import TextoutStream as _TextoutStream
from .smileys import htmlsmileys as _htmlsmileys
from .urls import htmlurls as _htmlurls
from ._options import TextoutBlockTag as _TextoutBlockTag, \
TextoutParagraphTag as _TextoutParagraphTag, TextoutOptions as _Options
from ._stream import TextoutStream as _TextoutStream
from ._html import escape as _htmlescape, urls as _htmlurls
__all__ = ["Translator"]
# Builtin tags.
_builtin_tags = _Tags(_importmod('..builtin_tags', __name__))
# ---
# Tweaks interface.
# ---
@ -143,8 +136,7 @@ class Translator:
You can even chain calls as the `process()` method returns
the output stream object. """
def __init__(self, inp, outp, output_type = 'html', \
tweaks = {}, tags = _builtin_tags):
def __init__(self, inp, outp, output_type, tweaks, options):
""" Initializer. """
if not output_type in ('html', 'lightscript'):
@ -152,7 +144,7 @@ class Translator:
self.output_type = output_type
self.tweaks = _TweaksDictionary(tweaks)
self.tags = tags
self.options = options
self.inp = inp
self.outp = outp
@ -203,7 +195,8 @@ class Translator:
# because it's nicer!
if not self.raw_mode and self.output_type == 'html':
text = _htmlsmileys(_htmlurls(text))
text = _htmlurls(text)
text = self.options.htmlsmileys(text)
return text
@ -712,9 +705,19 @@ class Translator:
# Get the initialized tag with the name and value.
# If the tag is unknown, output the full thing and just go on.
tag = self.tags.get_tag(tagdata.name, tagdata.value,
self.output_type, self.tweaks)
if not tag:
try:
tag = self.options.get_tag(tagdata.name)
except:
self.put_text(tagdata.full)
continue
value = tagdata.value
if value != None and hasattr(tag, 'procvalue') and tag.procvalue:
value = self.process_text(value)
try:
tag = tag(tagdata.name, value, self.output_type, self.tweaks)
except:
self.put_text(tagdata.full)
continue

View File

@ -4,7 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutAlignTag"]

View File

@ -4,8 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag, \
TextoutInlineTag as _TextoutInlineTag
from .. import BlockTag as _TextoutBlockTag, InlineTag as _TextoutInlineTag
__all__ = ["TextoutCodeTag", "TextoutInlineCodeTag", "TextoutNoEvalTag"]

View File

@ -4,7 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from .. import BlockTag as _TextoutBlockTag
from html import escape as _htmlescape
__all__ = ["TextoutImageTag", "TextoutAdminImageTag"]

View File

@ -4,7 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutInlineTag as _TextoutInlineTag
from .. import InlineTag as _TextoutInlineTag
import re as _re
__all__ = ["TextoutLabelTag", "TextoutTargetTag"]

View File

@ -4,7 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutInlineTag as _TextoutInlineTag
from .. import InlineTag as _TextoutInlineTag
from html import escape as _htmlescape
__all__ = ["TextoutLinkTag", "TextoutProfileTag"]

View File

@ -4,7 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutProgressTag"]

View File

@ -4,9 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from html import escape as _htmlescape
from ..smileys import htmlsmileys as _htmlsmileys
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutQuoteTag"]
@ -24,6 +22,7 @@ class TextoutQuoteTag(_TextoutBlockTag):
aliases = ('[quote]',)
superblock = True
notempty = True
procvalue = True
def prepare(self, name, value):
self._value = value
@ -31,8 +30,7 @@ class TextoutQuoteTag(_TextoutBlockTag):
def begin_html(self):
f = '<div class="citation">'
if self._value:
f += '<p><b>{} a écrit:</b></p>' \
.format(_htmlsmileys(_htmlescape(self._value)))
f += '<p><b>{} a écrit:</b></p>'.format(self._value)
return f
def end_html(self):

View File

@ -4,8 +4,8 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutInlineTag as _TextoutInlineTag
import string as _string
from .. import InlineTag as _TextoutInlineTag
__all__ = ["TextoutRotTag"]

View File

@ -4,8 +4,8 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from html import escape as _htmlescape
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutShowTag"]

View File

@ -0,0 +1,141 @@
#!/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 Smiley as _TextoutSmiley
__all__ = ["TwistedSmiley", "EvilSmiley", "SmileSmiley", "WinkSmiley",
"SadSmiley", "GrinSmiley", "HeheSmiley", "CoolSmiley", "Cool2Smiley",
"MadSmiley", "EekSmiley", "MrGreenSmiley", "ShockedSmiley",
"ConfusedSmiley", "EyebrowsSmiley", "CrySmiley", "LolSmiley",
"SorrySmiley", "RollEyesSmiley", "WazaSmiley", "HereSmiley",
"BowSmiley", "GoodSmiley", "LoveSmiley", "OuchSmiley", "FacepalmSmiley",
"InsultsSmiley", "WhatSmiley", "ExclSmiley"]
_prefix = '/images/smileys/'
class TwistedSmiley(_TextoutSmiley):
aliases = ('>:)',)
url = _prefix + 'twisted.gif'
class EvilSmiley(_TextoutSmiley):
aliases = ('>:(', ':grr:')
url = _prefix + 'evil.gif'
class SmileSmiley(_TextoutSmiley):
aliases = (':)',)
url = _prefix + 'smile.gif'
class WinkSmiley(_TextoutSmiley):
aliases = (';)',)
url = _prefix + 'wink.gif'
class SadSmiley(_TextoutSmiley):
aliases = (':(',)
url = _prefix + 'sad.gif'
class GrinSmiley(_TextoutSmiley):
aliases = (':D', ':grin:')
url = _prefix + 'grin.gif'
class HeheSmiley(_TextoutSmiley):
aliases = (':p',)
url = _prefix + 'hehe.gif'
class CoolSmiley(_TextoutSmiley):
aliases = (':cool:',)
url = _prefix + 'cool.gif'
class Cool2Smiley(_TextoutSmiley):
aliases = ('8-)',)
url = _prefix + 'cool2.gif'
class MadSmiley(_TextoutSmiley):
aliases = (':@',)
url = _prefix + 'mad.gif'
class EekSmiley(_TextoutSmiley):
aliases = ('0_0',)
url = _prefix + 'eek.gif'
class MrGreenSmiley(_TextoutSmiley):
aliases = (':E', ':mrgreen:')
url = _prefix + 'mrgreen.gif'
class ShockedSmiley(_TextoutSmiley):
aliases = (':O',)
url = _prefix + 'shocked.gif'
class ConfusedSmiley(_TextoutSmiley):
aliases = (':s', ':oops:')
url = _prefix + 'confused2.gif'
class EyebrowsSmiley(_TextoutSmiley):
aliases = ('^^',)
url = _prefix + 'eyebrows.gif'
class CrySmiley(_TextoutSmiley):
aliases = (":'(", ":cry:")
url = _prefix + 'cry.gif'
# FIXME
#class WhistleSmiley(_TextoutSmiley):
# aliases = (":-°", ':whistle:')
# url = _prefix + 'whistle.gif'
# height = '15px'
class LolSmiley(_TextoutSmiley):
aliases = (":lol:",)
url = _prefix + 'lol.gif'
class SorrySmiley(_TextoutSmiley):
aliases = (":sry:",)
url = _prefix + 'redface.gif'
class RollEyesSmiley(_TextoutSmiley):
aliases = (":mmm:",)
url = _prefix + 'rolleyes.gif'
class WazaSmiley(_TextoutSmiley):
aliases = (":waza:",)
url = _prefix + 'waza.gif'
class HereSmiley(_TextoutSmiley):
aliases = (":here:", ":arrow:")
url = _prefix + 'pointer.gif'
class BowSmiley(_TextoutSmiley):
aliases = (":bow:",)
url = _prefix + 'bow.gif'
class GoodSmiley(_TextoutSmiley):
aliases = (":good:",)
url = _prefix + 'welldone.gif'
class LoveSmiley(_TextoutSmiley):
aliases = (":love:",)
url = _prefix + 'love.gif'
class OuchSmiley(_TextoutSmiley):
aliases = (":aie:",)
url = _prefix + 'banghead2.gif'
class FacepalmSmiley(_TextoutSmiley):
aliases = (":facepalm:",)
url = _prefix + 'facepalm.gif'
class InsultsSmiley(_TextoutSmiley):
aliases = (":argh:",)
url = _prefix + 'insults.gif'
class WhatSmiley(_TextoutSmiley):
aliases = (":?:",)
url = _prefix + 'what.gif'
class ExclSmiley(_TextoutSmiley):
aliases = (":!:",)
url = _prefix + 'excl.gif'
# End of file.

View File

@ -4,9 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from html import escape as _htmlescape
from ..smileys import htmlsmileys as _htmlsmileys
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutSpoilerTag"]
@ -24,6 +22,7 @@ class TextoutSpoilerTag(_TextoutBlockTag):
aliases = ('[spoiler]',)
superblock = True
notempty = True
procvalue = True
def prepare(self, name, value):
self._closed = "Cliquez pour découvrir"
@ -33,8 +32,8 @@ class TextoutSpoilerTag(_TextoutBlockTag):
titles = value.split('|')
if titles[0]:
self._closed = titles[0]
if len(titles) >= 2 and titles[1]:
self._open = titles[1]
if len(titles) >= 2 and (len(titles) > 2 or titles[1]):
self._open = '|'.join(titles[1:])
def begin_html(self):
return '<div class="spoiler">' \
@ -42,8 +41,7 @@ class TextoutSpoilerTag(_TextoutBlockTag):
'\'open\');"><p>{}</p></div>' \
'<div class="title off" onclick="toggleSpoiler(this.parentNode, ' \
'\'close\');"><p>{}</p></div>' \
'<div class="off">'.format(_htmlsmileys(_htmlescape(self._closed)),
_htmlsmileys(_htmlescape(self._open)))
'<div class="off">'.format(self._closed, self._open)
def end_html(self):
return '</div></div>'

View File

@ -4,8 +4,8 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutInlineTag as _TextoutInlineTag
from ..color import get_color
from .. import InlineTag as _TextoutInlineTag
from ..color import get_color as _get_color
__all__ = ["TextoutTextTag"]
@ -96,7 +96,7 @@ class TextoutTextTag(_TextoutInlineTag):
self._strike = True
elif name == "color":
value, props = get_props(value)
self._color = get_color(value)
self._color = _get_color(value)
elif name == "font":
value, props = get_props(value)
assert value in _fonts
@ -117,7 +117,7 @@ class TextoutTextTag(_TextoutInlineTag):
elif name in _fonts:
self._font = name
else:
self._color = get_color(name)
self._color = _get_color(name)
# Gestion des injections CSS.

View File

@ -4,7 +4,7 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutTitleTag"]

View File

@ -4,11 +4,12 @@
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
from ..tags import TextoutBlockTag as _TextoutBlockTag
import re as _re
import urllib.parse as _urlparse
from html import escape as _htmlescape
from .. import BlockTag as _TextoutBlockTag
__all__ = ["TextoutVideoTag"]
_hexcode = _re.compile('[a-zA-Z0-9_]+')

32
textoutpc/builtin/__init__.py Executable file
View File

@ -0,0 +1,32 @@
#!/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.
#******************************************************************************
""" Built-in tags and smileys for the `textoutpc` module.
Some of these options will probably have to move to a separate module
Planète Casio-specific, but still, here we are.
"""
# Tags.
from ._Align import *
from ._Code import *
from ._Image import *
from ._Label import *
from ._Link import *
from ._Progress import *
from ._Quote import *
from ._Rot import *
from ._Show import *
from ._Spoiler import *
from ._Text import *
from ._Title import *
from ._Video import *
# Smileys.
from ._Smileys import *
# End of file.

View File

@ -1,16 +0,0 @@
#!/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.
#******************************************************************************
""" Built-in tags for the `textoutpc` module.
Some of these tags will probably have to move to a separate module
Planète Casio-specific, but still, here we are.
"""
_names = ["Align", "Code", "Image", "Label", "Link", "Progress",
"Quote", "Rot", "Show", "Spoiler", "Text", "Title", "Video"]
for name in _names:
__import__(name, globals(), level=1)
# End of file.

View File

@ -10,8 +10,6 @@
the W3C standards, although it is inspired from it.
"""
from .read import get_color
__all__ = ["get_color"]
from ._read import get_color
# End of file.

View File

@ -10,8 +10,8 @@
import re as _re
import math as _math
from .named import colors as _named_colors
from .sys import hls_to_rgb as _hls_to_rgb, hwb_to_rgb as _hwb_to_rgb
from ._named import colors as _named_colors
from ._sys import hls_to_rgb as _hls_to_rgb, hwb_to_rgb as _hwb_to_rgb
__all__ = ["get_color"]

View File

@ -1,83 +0,0 @@
#!/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.
#******************************************************************************
""" Smiley conversion.
Just convert them.
"""
import regex as _re
from html import escape as _htmlescape
__all__ = ["htmlsmileys"]
# ---
# List of them.
# ---
_Smileys_prefix = "/images/smileys/"
_Smileys = {
'>:)': 'twisted.gif',
'>:(': 'evil.gif',
':)': 'smile.gif',
';)': 'wink.gif',
':(': 'sad.gif',
':D': 'grin.gif',
':p': 'hehe.gif',
'8-)': 'cool2.gif',
':@': 'mad.gif',
'0_0': 'eek.gif',
':E': 'mrgreen.gif',
':O': 'shocked.gif',
':s': 'confused2.gif',
'^^': 'eyebrows.gif',
":'(": 'cry.gif',
# ':-°': ('whistle.gif', 'height: 15px;'),
# Name-based smileys.
':lol:': 'lol.gif',
':oops:': 'confused2.gif',
':grr:': 'evil.gif',
':sry:': 'redface.gif',
':mmm:': 'rolleyes.gif',
':waza:': 'waza.gif',
# ':whistle:': ('whistle.gif', 'height: 15px;'),
':here:': 'pointer.gif',
':bow:': 'bow.gif',
':cool:': 'cool.gif',
':good:': 'welldone.gif',
':love:': 'love.gif',
':aie:': 'banghead2.gif',
':cry:': 'cry.gif',
':facepalm:': 'facepalm.gif',
':argh:': 'insults.gif',
':?:': 'what.gif',
':!:': 'excl.gif',
':arrow:': 'here.gif',
':grin:': 'grin.gif',
}
_Smileys_html = {_htmlescape(a): _Smileys_prefix + b \
for a, b in _Smileys.items()}
def _Smiley_sub_html(m):
return m.group(1) + '<img src="' + _Smileys_html[m.group(2)] \
+ '">' + m.group(3)
_Smiley_html_re = _re.compile('(^|\\s)(' + '|'.join(map(_re.escape,
_Smileys_html.keys())) + ')(\\s|$)')
# ---
# Functions.
# ---
def htmlsmileys(text):
""" HTML smileys """
text = _Smiley_html_re.sub(_Smiley_sub_html, text)
text = _Smiley_html_re.sub(_Smiley_sub_html, text)
return text
# End of file.

View File

@ -1,60 +0,0 @@
#!/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.
#******************************************************************************
""" Autolinking (URL extraction from raw text) in HTML. """
import regex as _re
__all__ = ["htmlurls", "lightscripturls"]
# ---
# Autolinking regex.
# ---
def _sub_html(m):
sp = m.group('sp')
url = m.group('url')
aft = ''
# Hack for the last comma.
if url[-1] == ',':
url, aft = url[:-1], ','
text = '{}<a href="{}">{}</a>{}' \
.format(sp, url, url, aft)
return text
def _sub_lightscript(m):
sp = m.group('sp')
url = m.group('url')
aft = ''
# Hack for the last comma.
if url[-1] == ',':
url, aft = url[:-1], ','
url = url.replace('<', '%3C')
url = url.replace('>', '%3E')
text = '{}<{}>{}'.format(sp, url, aft)
return text
_reg = _re.compile("""\
(?P<sp>^|\s|[[:punct:]])
(?P<url>(https?|ftp):
(?P<ucore>[^\[\]\(\)\s]* (\[(?&ucore)\]?)* (\((?&ucore)\)?)*)*
)
""", _re.VERBOSE | _re.M)
# ---
# Main functions.
# ---
def htmlurls(text):
return _reg.sub(_sub_html, text)
def lightscripturls(text):
return _reg.sub(_sub_lightscript, text)
# End of file.