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

109 lines
3.4 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__all__ = ["TextoutBlockTag", "TextoutParagraphTag", "TextoutInlineTag",
"TextoutRawTag", "TextoutRawBlockTag", "TextoutRawParagraphTag",
"TextoutRawInlineTag"]
# ---
# Main base tag class.
# ---
# 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:
[<name>]<content>[/<name>]
2018-01-02 18:57:04 +01:00
[<name>=<value>]<content>[/<name>] """
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. """
# FIXME: for all of the methods, call the general function first,
# then call the format-specific function, instead of only calling
# one of the two!!
if hasattr(self, 'prepare_' + ot):
self.prepare = getattr(self, 'prepare_' + ot)
if hasattr(self, 'prepare'):
self.prepare(name, value)
2018-01-02 18:57:04 +01:00
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
if hasattr(self, 'begin_' + ot):
2018-01-16 13:34:11 +01:00
self.begin = getattr(self, 'begin_' + ot)
if hasattr(self, 'end_' + ot):
2018-01-16 13:34:11 +01:00
self.end = getattr(self, 'end_' + ot)
if hasattr(self, 'content_' + ot):
self.content = getattr(self, 'content_' + ot)
if hasattr(self, 'default_' + ot):
self.default = getattr(self, 'default_' + ot)
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
# ---
# Role-specific base tag classes.
# ---
class TextoutRawTag:
2018-01-16 13:34:11 +01:00
pass
class TextoutBlockTag(TextoutTag):
pass
class TextoutRawBlockTag(TextoutBlockTag, TextoutRawTag):
pass
class TextoutParagraphTag(TextoutBlockTag):
pass
class TextoutRawParagraphTag(TextoutParagraphTag, TextoutRawTag):
pass
class TextoutInlineTag(TextoutTag):
pass
class TextoutRawInlineTag(TextoutInlineTag, TextoutRawTag):
pass
class TextoutInlineBlockTag(TextoutInlineBlockTag):
pass
class TextoutRawInlineBlockTag(TextoutInlineBlockTag, TextoutRawTag):
pass
2018-01-02 18:57:04 +01:00
# End of file.