Skip to content

Commit

Permalink
Allow templates and groups to be list of nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
keenanlang committed Aug 9, 2024
1 parent c001ce8 commit 3d26861
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
24 changes: 15 additions & 9 deletions gestalt/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,15 @@ def apply(self, generator, data={}):
return output


class ApplyNode(Node):
def __init__(self, layout={}, defaults={}, macros={}, subnode=None, loc=None):
class ApplyNode(GroupNode):
def __init__(self, layout={}, defaults={}, macros={}, subnodes=[], loc=None):
super(ApplyNode, self).__init__("Apply", layout=layout, loc=loc)

self.defaults = defaults
self.macros = macros
self.subnode = subnode
self.subnodes = subnodes

self.tocopy.append("subnode")
self.tocopy.append("subnodes")
self.tocopy.append("macros")
self.tocopy.append("defaults")

Expand Down Expand Up @@ -597,12 +597,18 @@ def apply(self, generator, data={}):

child_macros.update({key : to_assign})


if self.name:
self.subnode.name = self.name
output = generator.generateGroup(self, macros=data)

for child in self:
child_macros.update({
"__parentx__" : data["__parentx__"],
"__parenty__" : data["__parenty__"],
"__parentwidth__" : data["__parentwidth__"],
"__parentheight__" : data["__parentheight__"]})

return self.subnode.apply(generator, data=child_macros)

output.place(child.apply(generator, data=child_macros))

return output

class SpacerNode(Node):
def __init__(self, layout={}, loc=None):
Expand Down
20 changes: 13 additions & 7 deletions gestalt/Stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ def read_node(typ, loader, node):
return Node(typ, loc=node.start_mark, layout=params)

def read_group_node(typ, loader, node):
params = loader.construct_mapping(node, deep=True)
params = {}

try:
params = loader.construct_mapping(node, deep=True)
except Exception as e:
print(repr(e))
params["children"] = loader.construct_sequence(node, deep=True)

return GroupNode(typ, loc=node.start_mark, layout=params)

Expand Down Expand Up @@ -108,16 +114,16 @@ def construct_from_suffix(loader, suffix, node):
def read_template_multi(loader, suffix, node):
params = loader.construct_sequence(node, deep=True)
defaults = {}
template_node = None
template_nodes = []

for item in params:
if isinstance(item, dict):
defaults.update(item)

elif isinstance(item, Node):
template_node = item
template_nodes.append(item)

my_templates[suffix] = (template_node, defaults)
my_templates[suffix] = (template_nodes, defaults)

return None

Expand All @@ -132,9 +138,9 @@ def read_apply_multi(loader, suffix, node):
if suffix not in my_templates:
raise ValueError("Could not find template with name: " + suffix.lstrip(":"))

template_node, defaults = my_templates.get(suffix)
return ApplyNode(defaults=defaults, macros=macros, subnode=template_node, loc=node.start_mark)
template_nodes, defaults = my_templates.get(suffix)

return ApplyNode(defaults=defaults, layout={"children":template_nodes}, macros=macros, loc=node.start_mark)

def read_debug_multi(loader, suffix, node):
ret_node = construct_from_suffix(loader, suffix, node)
Expand Down

0 comments on commit 3d26861

Please sign in to comment.