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

161 lines
4.7 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2018-01-22 20:33:25 +01:00
from functools import partial as _p
__all__ = ["TextoutRawTag",
"TextoutBlockTag", "TextoutRawBlockTag",
"TextoutParagraphTag", "TextoutRawParagraphTag",
"TextoutInlineTag", "TextoutRawInlineTag",
"TextoutInlineBlockTag", "TextoutRawInlineBlockTag"]
# ---
# 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-22 20:33:25 +01:00
@property
def name(self):
return self.__name
@property
def _full(self):
return self.__full
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. """
2018-01-22 20:33:25 +01:00
# Store internal data.
self.__output_type = ot
self.__name = name
if name[0] + name[-1] == "[]":
self.__full = "[" + name[1:-1] + ("=" + value if value != None \
else "") + "]"
else:
self.__full = name
# Call both prepare functions.
if hasattr(self, 'prepare_' + ot):
self.prepare = getattr(self, 'prepare_' + ot)
if hasattr(self, 'prepare'):
self.prepare(name, value)
2018-01-22 20:33:25 +01:00
# Prepare the preprocessing elements.
if not hasattr(self, 'preprocess'):
if hasattr(self, 'preprocess_' + ot):
self.__preprocess0 = self.preprocess
self.preprocess = self.__preprocess_double
elif hasattr(self, 'preprocess_' + ot):
self.preprocess = getattr(self, 'preprocess_' + ot)
if hasattr(self, 'preprocess'):
self.__preprocess2 = self.preprocess
self.preprocess = self.__preprocess_and_prepare
2018-01-19 17:55:43 +01:00
else:
2018-01-22 20:33:25 +01:00
self.__after_preprocess()
if hasattr(self, 'default_' + ot):
self.default = getattr(self, 'default_' + ot)
2018-01-22 20:33:25 +01:00
def __preprocess_double(self, content):
""" Preprocess using the two methods. """
ct = self.__preprocess0(content)
if ct != None: content = ct; del ct
ct = self.__preprocess1(content)
if ct != None: content = ct; del ct
return content
def __preprocess_and_prepare(self, content):
""" Preprocess and do the things after. """
ret = self.__preprocess2(content)
self.__after_preprocess()
return ret
def __out(self, name):
""" Generic function to call two output functions of the same type. """
getattr(self, '__' + name)()
getattr(self, name + '_' + self.__output_type)()
def __after_preprocess(self):
ot = self.__output_type
for otype in ('begin', 'content', 'end'):
if hasattr(self, otype):
if hasattr(self, otype + '_' + ot):
setattr(self, '__' + otype, getattr(self, otype))
setattr(self, otype, _p(self.__out, otype))
elif hasattr(self, otype + '_' + ot):
setattr(self, otype, getattr(self, otype + '_' + 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
2018-01-22 20:33:25 +01:00
class TextoutInlineBlockTag(TextoutTag):
pass
2018-01-22 20:33:25 +01:00
class TextoutRawInlineBlockTag(TextoutInlineBlockTag, TextoutRawTag):
pass
2018-01-22 20:33:25 +01:00
class TextoutInlineTag(TextoutInlineBlockTag):
pass
2018-01-22 20:33:25 +01:00
class TextoutRawInlineTag(TextoutInlineBlockTag, TextoutRawTag):
pass
2018-01-02 18:57:04 +01:00
# End of file.