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

147 lines
3.7 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/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`). """
2018-01-02 18:57:04 +01:00
2018-02-11 20:58:38 +01:00
import inspect as _inspect
2018-01-22 20:33:25 +01:00
from functools import partial as _p
from inspect import getargspec as _getargspec
2018-01-22 20:33:25 +01:00
__all__ = ["TextoutTag", "TextoutBlockTag", "TextoutInlineTag",
"TextoutParagraphTag"]
2018-02-11 20:58:38 +01:00
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`.
# ---
2018-01-02 18:57:04 +01:00
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>] """
2018-01-02 18:57:04 +01:00
aliases = ()
def __init__(self, name, value, ot, tweaks):
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.__tweaks = tweaks
self.output_type = ot
2018-01-22 20:33:25 +01:00
# Call both prepare functions.
if hasattr(self, 'prepare'):
try:
2018-02-11 20:58:38 +01:00
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)
2018-01-22 20:33:25 +01:00
# Prepare the preprocessing elements.
if hasattr(self, 'preprocess'):
2018-01-22 20:33:25 +01:00
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
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>'
2018-01-02 18:57:04 +01:00
# End of file.