68 lines
1.6 KiB
Python
68 lines
1.6 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
""" Tag helpers.
|
||
|
As we ought to be able to make separate tag modules, this tag is
|
||
|
"""
|
||
|
|
||
|
from inspect import ismodule as _ismod, isclass as _isclass
|
||
|
from .base import TextoutTag as _TextoutTag, TextoutBlockTag, TextoutInlineTag
|
||
|
from .paragraph import TextoutParagraphTag
|
||
|
|
||
|
__all__ = ["TextoutParagraphTag", "TextoutBlockTag", "TextoutInlineTag",
|
||
|
"get_tag"]
|
||
|
|
||
|
# ---
|
||
|
# Gathering of the tags.
|
||
|
# ---
|
||
|
|
||
|
def _extract_tags(module):
|
||
|
""" Load all tags from a module. """
|
||
|
|
||
|
tags = []
|
||
|
|
||
|
# Obtain the list of properties from the module.
|
||
|
|
||
|
try:
|
||
|
ds = module.__all__
|
||
|
except:
|
||
|
ds = dir(module)
|
||
|
|
||
|
# Get the submodules from the module (usually different files in the
|
||
|
# tags module folder).
|
||
|
|
||
|
for submodule in (obj for name, obj in ((nm, getattr(module, nm)) \
|
||
|
for nm in ds) if (name == '__init__' or name[0] != '_') \
|
||
|
and _ismod(obj)):
|
||
|
obtained = _extract_tags(submodule)
|
||
|
tags += [tag for tag in obtained if not any(tag is x for x in tags)]
|
||
|
del obtained
|
||
|
|
||
|
# Extract the tags from the current module.
|
||
|
|
||
|
for tag in (obj for name, obj in ((nm, getattr(module, nm)) for nm in ds) \
|
||
|
if name[0] != '_' and _isclass(obj) and issubclass(obj, _TextoutTag)):
|
||
|
tags.append(tag)
|
||
|
|
||
|
return tags
|
||
|
|
||
|
_tags = _extract_tags(__import__("builtin", \
|
||
|
{'__name__': __name__ + '.__init__'}, level=1))
|
||
|
_aliases = {alias: tag for alias, tag in \
|
||
|
[(alias, tag) for tag in _tags for alias in tag.aliases]}
|
||
|
|
||
|
# ---
|
||
|
# Function to get a tag.
|
||
|
# ---
|
||
|
|
||
|
def get_tag(name, value, output_type = 'html'):
|
||
|
""" Find a tag using its name. """
|
||
|
|
||
|
try:
|
||
|
als = _aliases[name]
|
||
|
als = als(name, value, output_type)
|
||
|
return als
|
||
|
except:
|
||
|
return None
|
||
|
|
||
|
# End of file.
|