from wordbridge.html import HtmlBuilder

html = HtmlBuilder()

def top_level_element(tag_name):
    return TopLevelElement(tag_name)

class TopLevelElement(object):
    def __init__(self, tag_name):
        self._tag_name = tag_name
    
    def start(self, html_stack):
        html_stack.open_element(self._tag_name)
        
    def end(self, html_stack):
        html_stack.close_element()
        

def unordered_list():
    return UnorderedList()

class UnorderedList(object):
    def start(self, html_stack):
        current_element = html_stack.current_element()
        if current_element is None or current_element.tag_name != "ul":
            html_stack.open_element("ul")
        html_stack.open_element("li")
        
    def end(self, html_stack):
        html_stack.close_element()

@@ -104,133 +104,269 @@
 urn 
-TopLevelElement(tag_name)%0A%0Aclass TopLevelElement(object):%0A    def __init__(self, tag_name):%0A        self._tag_name = 
+Style(%0A        on_start=_sequence(_clear_stack, _open_element(tag_name)),%0A        on_end=_clear_stack%0A    )%0A%0Adef _clear_stack(html_stack):%0A    while html_stack.current_element() is not None:%0A        html_stack.close_element()%0A        %0Adef _open_element(
 tag_name
 %0A   
@@ -361,21 +361,18 @@
 tag_name
-%0A    
+):
 %0A    def
@@ -368,36 +368,30 @@
 e):%0A    def 
-start(self, 
+apply(
 html_stack):
@@ -427,14 +427,8 @@
 ent(
-self._
 tag_
@@ -441,87 +441,162 @@
     
+%0A
     
+return apply
 %0A    
+%0A
 def 
-end(self, html_stack):%0A        html_stack.close_element()%0A        
+_sequence(*funcs):%0A    def apply(html_stack):%0A        for func in funcs:%0A            func(html_stack)%0A        %0A    return apply
 %0A%0Ade
@@ -967,28 +967,320 @@
  html_stack.close_element()%0A
+%0Aclass Style(object):%0A    def __init__(self, on_start, on_end):%0A        self._on_start = on_start%0A        self._on_end = on_end%0A        %0A    def start(self, html_stack):%0A        return self._on_start(html_stack)%0A        %0A    def end(self, html_stack):%0A        return self._on_end(html_stack)%0A

Build up style from composable functions