123 rivejä
2.9 KiB
Python
123 rivejä
2.9 KiB
Python
import json
|
|
|
|
from anytree.importer import DictImporter
|
|
from sys import argv
|
|
|
|
|
|
def build_table_depth_first(node, rows, leaf_count=0):
|
|
|
|
for child in sorted(node.children, key=lambda x: x.height, reverse=True):
|
|
leaf_count = build_table_depth_first(child, rows, leaf_count)
|
|
|
|
colspan = 0
|
|
if node.siblings:
|
|
max_sibling_height = max([s.height for s in node.siblings])
|
|
|
|
if max_sibling_height > node.height:
|
|
colspan = max_sibling_height - node.height + 1
|
|
|
|
if node.is_leaf:
|
|
cur_row = leaf_count
|
|
rows[cur_row].append(f'<th>{node.name}</th>')
|
|
|
|
if colspan > 1:
|
|
rows[cur_row].append(f'<td colspan="{colspan-1}" class="filler"></td>')
|
|
|
|
leaf_count += 1
|
|
|
|
else:
|
|
child_leaves = len(node.leaves)
|
|
cur_row = leaf_count - child_leaves
|
|
rows[cur_row].append(f"<td rowspan='{child_leaves}' colspan='{colspan}' title='{node.description}'>{node.name}</td>")
|
|
|
|
return leaf_count
|
|
|
|
|
|
def generate_steps_depth_first(node, steps=[]):
|
|
for child in sorted(node.children, key=lambda x: x.height, reverse=True):
|
|
steps = generate_steps_depth_first(child, steps)
|
|
|
|
if not node.is_leaf:
|
|
steps.append(node.description if node.description else node.name)
|
|
|
|
return steps
|
|
|
|
|
|
if len(argv) < 2:
|
|
print(f"usage: {argv[0]} <json file>")
|
|
exit()
|
|
|
|
with open(argv[1], 'r') as f:
|
|
recipe = json.loads(f.read())
|
|
|
|
recipe_name = recipe.get("name")
|
|
recipe_desc = recipe.get("description")
|
|
importer = DictImporter()
|
|
sub_recipes = [importer.import_(data) for data in recipe["sub_recipes"]]
|
|
|
|
print("<!DOCTYPE html>")
|
|
print("<html lang='en'>")
|
|
print(f"<head><title>{recipe_name}</title>")
|
|
print("<link rel='stylesheet' type='text/css' href='style.css'>")
|
|
print("</head>")
|
|
print("<body>")
|
|
|
|
print("<div id='title'>")
|
|
print(f"<h1>{recipe_name}</h1>")
|
|
print("</div>")
|
|
|
|
print("<div id='description'>")
|
|
print(f"<p>{recipe_desc}</p>")
|
|
print("</div>")
|
|
|
|
print("<div id='needs'>")
|
|
print("<h2>Needs</h2>")
|
|
print("<ul>")
|
|
|
|
for sub_recipe in sub_recipes:
|
|
for ingredient in sub_recipe.leaves:
|
|
print(f" <li>{ingredient.name}</li>")
|
|
|
|
print("</ul>")
|
|
print("</div>")
|
|
|
|
print("<div id='preparation'>")
|
|
print("<h2>Preparation</h2>")
|
|
print("<ol>")
|
|
|
|
for sub_recipe in sub_recipes:
|
|
tasks = generate_steps_depth_first(sub_recipe, steps=[])
|
|
for task in tasks:
|
|
print(f" <li>{task}</li>")
|
|
|
|
print("</ol>")
|
|
print("</div>")
|
|
|
|
print("<div id='tables'>")
|
|
print("<h2>Tabular Layout</h2>")
|
|
|
|
for sub_recipe in sub_recipes:
|
|
print("<p>")
|
|
print("<table cellspacing='0' border=true>")
|
|
|
|
output_rows = []
|
|
|
|
for i in range(len(sub_recipe.leaves)):
|
|
output_rows.append([])
|
|
|
|
build_table_depth_first(sub_recipe, output_rows)
|
|
|
|
for row in output_rows:
|
|
print("<tr>")
|
|
for cell in row:
|
|
print(f" {cell}")
|
|
print("</tr>")
|
|
|
|
print("</table>")
|
|
print("</p>")
|
|
|
|
print("</div>")
|
|
|
|
print("</body>")
|
|
print("</html>")
|