2
0
Fork 0
textout/textoutpc/tags/base.py

147 lines
3.7 KiB
Python
Executable File

#!/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.
#******************************************************************************
""" Base class for textout tags. For your class to be used as a textout tag,
you have to make it inherit one of these (usually `TextoutBlockTag`
or `TextoutInlineTag`). """
import inspect as _inspect
from functools import partial as _p
from inspect import getargspec as _getargspec
__all__ = ["TextoutTag", "TextoutBlockTag", "TextoutInlineTag",
"TextoutParagraphTag"]
def _getargscount(func):
try:
return len(_inspect.getfullargspec(func).args)
except:
return len(_inspect.getargspec(func).args)
# ---
# Main base tag class.
# For more about defining a tag, see `/TAGS.md`.
# ---
class TextoutTag:
""" The textout tag base class.
Is initialized with these values:
<name (only special)><content><name>
[<name (with brackets)>]<content>[/<name>]
[<name (with brackets)>=<value>]<content>[/<name>] """
aliases = ()
def __init__(self, name, value, ot, tweaks):
""" Initialize the textout tag with the documented members. """
# Store internal data.
self.__output_type = ot
self.__tweaks = tweaks
self.output_type = ot
# Call both prepare functions.
if hasattr(self, 'prepare'):
try:
assert _getargscount(self.prepare) == 4
args = (name, value, ot)
except:
args = (name, value)
self.prepare(*args)
if hasattr(self, 'prepare_' + ot):
prep = getattr(self, 'prepare_' + ot)
try:
assert len(_getargspec(prep).args) == 4
args = (name, value, ot)
except:
args = (name, value)
prep(*args)
# Prepare the preprocessing elements.
if 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
else:
self.__after_preprocess()
if hasattr(self, 'default_' + ot):
self.default = getattr(self, 'default_' + ot)
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))
def tweak(self, key, default = None):
try:
return self.__tweaks[key]
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. """
notempty = True
def begin_html(self):
return '<p>'
def end_html(self):
return '</p>'
# End of file.