From d5d9c101f06ed1bfaeff3e187b8633e731e84df2 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 7 Jul 2018 15:56:30 +0200 Subject: [PATCH] Add factory for generic elements. --- pyltk/__init__.py | 3 +++ pyltk/latexfactory.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 pyltk/latexfactory.py diff --git a/pyltk/__init__.py b/pyltk/__init__.py index 2ed0f11..1b77a06 100644 --- a/pyltk/__init__.py +++ b/pyltk/__init__.py @@ -14,3 +14,6 @@ from .subfloat import subfloat from .table import table, tabledf from .tabular import tabular, tabulardf, hline_element, row_element, multicolumn from .inlines import it, bf, mt +from .latexfactory import factory + +latex = factory() diff --git a/pyltk/latexfactory.py b/pyltk/latexfactory.py new file mode 100644 index 0000000..7a34bc3 --- /dev/null +++ b/pyltk/latexfactory.py @@ -0,0 +1,38 @@ +# -*- encoding: utf-8 -*- + + +from .element import element + + +class generic_element(element): + + templates = { + 'element': '\\{name}{options}{attributes}', + 'options': '[{content}]' + } + + autolabel = False + + def __init__(self, name, *args, parent=None, **kargs): + super().__init__(parent, **kargs) + self.name = name + if not args: + self.attributes = '{}' + else: + self.attributes = '' + for arg in args: + self.attributes += '{{{}}}'.format(arg) + + def content(self): + return self.fmt().format('element', { + 'name': self.name, + 'attributes': self.attributes, + 'options': self.format_options(self.options) + }) + + +class factory: + + def __getattr__(self, name): + return lambda *args, **kargs: \ + generic_element(name, *args, **kargs)