I'm trying to recursively extract the DOM tree saved in the graph Database to granularly rewrite them as either HTML, JSON or other template files. The motivation I needed to have an easy way of extracting and controlling DOM nodes, rewriting them into any needed templating format which can be integrated into CMS or MVC development where variable interpolation is paramount.
My attempt using a recursion fails because I'm confused about what to use as the next node parent or child.
What I need to do is visit having the following tree, I need to create an equivalent HTML file using for now either beautifulsoup or other utility.
html
head
title
body
div
div
ul
li
With the following code, without a recursive function, I'm able to get the immediate children resulting in the value of:
|> parent Id: 844424930131969 --- parent tag HTML
child id 844424930131970 child tag: head
child id 844424930131973 child tag: body
or the following html file:
<html>
<head>
</head>
<body>
</body>
</html>
How do I pass d
to be able to be part of the next recursion where I can obtain its children?
cursor = ag.execCypher("MATCH (n:node {tag: 'html'}) RETURN n")
t = [x[0].id for x in cursor]
print(t[0])
def graph_dom(t_id):
parent = ag.execCypher("MATCH (n:node) WHERE id(n) = %s RETURN n", params=(t_id,))
p = [x[0] for x in parent]
pt = p[0]["tag"]
pid = p[0].id
print(f"|> parent Id: {pid} --- parent tag {pt}")
parent_tag = soup.new_tag(name=p[0]["tag"])
soup.append(parent_tag)
children = ag.execCypher("MATCH (v:node)-[R:connect]->(V2) WHERE id(v) = %s RETURN V2", params=(p[0].id,))
for d in children:
children_tag = soup.new_tag(name=d[0]["tag"])
parent_tag.append(children_tag)
dt = d[0]["tag"]
did = d[0].id
print(f"child id {did} child tag: {dt}")
# graph_dom() # I need to add a recursive argument
graph_dom(t[0])
file_soup = soup.prettify()
with open("helloworld.html", "w") as file:
file.write(str(file_soup))