2018-01-02 18:57:04 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from html import escape as _htmlescape
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
__all__ = ["_htmlescape", "TextoutTag", "TextoutBlockTag", "TextoutInlineTag",
|
|
|
|
"TextoutRawTag", "TextoutRawBlockTag", "TextoutRawInlineTag"]
|
2018-01-05 03:31:33 +01:00
|
|
|
|
|
|
|
# ---
|
|
|
|
# Main base tag class.
|
|
|
|
# ---
|
2018-01-05 22:13:30 +01:00
|
|
|
# There are three types of tags:
|
|
|
|
#
|
|
|
|
# - Those who know how to are valid straight when they receive their
|
|
|
|
# name and value (attribute), and include their content without
|
|
|
|
# processing it.
|
|
|
|
# - Those who are valid straight when they receive their name and value,
|
|
|
|
# but process their content.
|
|
|
|
# - Those who don't know if they are valid until they receive their content,
|
|
|
|
# and process it.
|
|
|
|
#
|
|
|
|
# All categories can set the `_prepare()` method to process the name
|
|
|
|
# and value to check their validity and prepare some things.
|
|
|
|
# The second and third category needs to set the `_content()` method
|
|
|
|
# (which the existence will be checked after the `_prepare()` method
|
|
|
|
# is called), which may, as the `_prepare()` method, raise an exception
|
|
|
|
# if there is a problem with the content.
|
|
|
|
# ---
|
|
|
|
# The classes can define the following methods (not including helpers):
|
|
|
|
#
|
|
|
|
# - `_prepare()`: will be called at class initialization. `self.name` and
|
|
|
|
# `self.value` (attribute) will be set. Exception means the tag with
|
|
|
|
# this name/attribute combination is invalid;
|
|
|
|
# - `_content()`: will be called when the content is set. `self.name`,
|
|
|
|
# `self.value` and `self.content` will be set. Exception means the tag
|
|
|
|
# with this name/attribute/content combination is invalid;
|
|
|
|
# - `begin_<fmt>()`: put the beginning of the produced form (by default,
|
|
|
|
# nothing);
|
|
|
|
# - `process_<fmt>()`: put the main content of the produced form (by default,
|
|
|
|
# the raw/escaped content for the format);
|
|
|
|
# - `end_<fmt>()`: put the end of the produced form (by default, nothing).
|
2018-01-02 18:57:04 +01:00
|
|
|
|
|
|
|
class TextoutTag:
|
|
|
|
""" The textout tag base class.
|
|
|
|
Is initialized with these values:
|
2018-01-05 22:13:30 +01:00
|
|
|
|
|
|
|
[<name>]<content>[/<name>]
|
2018-01-02 18:57:04 +01:00
|
|
|
[<name>=<value>]<content>[/<name>] """
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
aliases = ()
|
|
|
|
|
2018-01-19 11:23:19 +01:00
|
|
|
def __init__(self, name, value, ot):
|
2018-01-02 18:57:04 +01:00
|
|
|
""" Initialize the textout tag with the documented members. """
|
|
|
|
|
|
|
|
self.name = name
|
2018-01-19 17:55:43 +01:00
|
|
|
if name[0] + name[-1] == "[]":
|
|
|
|
self._full = "[" + name[1:-1] + ("=" + value if value != None \
|
|
|
|
else "") + "]"
|
|
|
|
else:
|
|
|
|
self._full = name
|
|
|
|
|
2018-01-16 13:34:11 +01:00
|
|
|
if hasattr(self, 'prepare'):
|
|
|
|
self.prepare(name, value)
|
|
|
|
|
2018-01-19 21:23:35 +01:00
|
|
|
if hasattr(self, 'begin_' + ot):
|
2018-01-16 13:34:11 +01:00
|
|
|
self.begin = getattr(self, 'begin_' + ot)
|
2018-01-19 21:23:35 +01:00
|
|
|
if hasattr(self, 'end_' + ot):
|
2018-01-16 13:34:11 +01:00
|
|
|
self.end = getattr(self, 'end_' + ot)
|
2018-01-19 21:23:35 +01:00
|
|
|
if hasattr(self, 'process_' + ot):
|
2018-01-16 13:34:11 +01:00
|
|
|
self.process = getattr(self, 'process_' + ot)
|
2018-01-19 21:23:35 +01:00
|
|
|
if hasattr(self, 'preprocess_' + ot):
|
2018-01-16 13:34:11 +01:00
|
|
|
self.preprocess = getattr(self, 'preprocess_' + ot)
|
2018-01-02 18:57:04 +01:00
|
|
|
|
2018-01-05 03:31:33 +01:00
|
|
|
# ---
|
|
|
|
# Role-specific base tag classes.
|
|
|
|
# ---
|
|
|
|
|
2018-01-05 22:13:30 +01:00
|
|
|
class TextoutRawTag:
|
2018-01-16 13:34:11 +01:00
|
|
|
pass
|
2018-01-05 22:13:30 +01:00
|
|
|
|
2018-01-05 03:31:33 +01:00
|
|
|
class TextoutBlockTag(TextoutTag):
|
|
|
|
pass
|
2018-01-05 22:13:30 +01:00
|
|
|
class TextoutRawBlockTag(TextoutBlockTag, TextoutRawTag):
|
|
|
|
pass
|
2018-01-05 03:31:33 +01:00
|
|
|
|
|
|
|
class TextoutInlineTag(TextoutTag):
|
|
|
|
pass
|
2018-01-05 22:13:30 +01:00
|
|
|
class TextoutRawInlineTag(TextoutInlineTag, TextoutRawTag):
|
|
|
|
pass
|
2018-01-05 03:31:33 +01:00
|
|
|
|
2018-01-02 18:57:04 +01:00
|
|
|
# End of file.
|