Skip to content

Commit

Permalink
Added injections
Browse files Browse the repository at this point in the history
  • Loading branch information
oksome committed Nov 19, 2013
1 parent d4e526a commit ac3fdfd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions element.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import copy


def build(element):
if hasattr(element, 'build'):
return element.build()
else:
return str(element), {}


class Element(object):

def __init__(self, tagname, inner, **kwargs):
def __init__(self, tagname, inner, injections=None, **kwargs):
self.tagname = tagname
self.inner = inner
self._injections = injections if injections else []
if 'class_' in kwargs:
kwargs['class'] = kwargs.pop('class_')
self.args = kwargs
Expand All @@ -36,6 +46,13 @@ def build(self):
def __str__(self):
return self.build()

def injections(self):
result = copy.copy(self._injections)
for i in self.inner:
if hasattr(i, 'injections'):
result += i.injections()
return result

def __iter__(self):
'Hack to be returned to CherryPy with no prior conversion'
class TagIterator(object):
Expand Down Expand Up @@ -69,4 +86,4 @@ def build(self):
class HTMLElement(Element):

def build(self):
return '<!doctype html>\n' + Element.build(self)
return '<!doctype html>\n' + Element.build(self)
2 changes: 2 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ def test_page():
t.p('Paragraph'),
)
).build()
assert r == '<!doctype html>\n<html >\n<body >\n<h1 >\nTitle\n</h1>\n<p >\nParagraph\n</p>\n</body>\n</html>'

0 comments on commit ac3fdfd

Please sign in to comment.