diff --git a/comfyrecipes/parsing.py b/comfyrecipes/parsing.py index a044c43..d702d27 100644 --- a/comfyrecipes/parsing.py +++ b/comfyrecipes/parsing.py @@ -205,6 +205,14 @@ class IngredientInstance(Element): price=price, ) +class StepSection(Element): + def __init__(self, ctx: Context, title: str, steps: List[str]) -> None: + self.title = title + self.steps = steps + + @classmethod + def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self: + return cls(ctx, dct["section"], dct["steps"]) class Recipe(Element): def __init__( @@ -214,7 +222,7 @@ class Recipe(Element): ingredients: List[IngredientInstance], subrecipes: List["Recipe"], price: Optional["PriceDB"], - steps: List[str], + stepsections: List[StepSection], ) -> None: super().__init__(ctx) self.srcpath = "" @@ -223,7 +231,7 @@ class Recipe(Element): self.ingredients = ingredients self.subrecipes = subrecipes self.price = price - self.steps = steps + self.stepsections = stepsections @classmethod def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self: @@ -268,16 +276,24 @@ class Recipe(Element): currency=currency, ) - steps = [] + stepsections: List[StepSection] = [] if "steps" in dct: - steps = dct["steps"] + defaultstepsection = StepSection(ctx, "default", []) + stepsections = [defaultstepsection] + for step in dct["steps"]: + if isinstance(step, str): + defaultstepsection.steps.append(step) + if isinstance(step, dict): + section = StepSection.from_dict(ctx, step) + stepsections.append(section) + return cls( ctx=ctx, title=dct["title"], ingredients=ingredients, subrecipes=subrecipes, price=price, - steps=steps, + stepsections=stepsections, ) class Conversion(Element): diff --git a/comfyrecipes/schemas/recipe.json b/comfyrecipes/schemas/recipe.json index 0f690ae..a8f9876 100644 --- a/comfyrecipes/schemas/recipe.json +++ b/comfyrecipes/schemas/recipe.json @@ -26,7 +26,23 @@ }, "steps": { "type": "array", - "items": { "type": "string" } + "items": { + "oneOf": [ + { "type": "string" }, + { + "type": "object", + "additionalProperties": false, + "required": [ "section" ], + "properties": { + "section": { "type": "string" }, + "steps": { + "type": "array", + "items": {"type": "string"} + } + } + } + ] + } }, "subrecipes": { "type": "array", diff --git a/comfyrecipes/templates/recipe.html b/comfyrecipes/templates/recipe.html index 01d3c57..552920d 100644 --- a/comfyrecipes/templates/recipe.html +++ b/comfyrecipes/templates/recipe.html @@ -24,11 +24,16 @@ {% endfor %} {% if rec.price != None %}price: {{price(rec.price)}}{%endif%} {% endif %} -{% if rec.steps|length != 0 %} -

Steps

-{% for step in rec.steps %} -
  • {{ step }}
  • -{% endfor %} +{% if rec.stepsections|length != 0 %} +

    Steps

    + {% for stepsection in rec.stepsections %} + {% if stepsection.title != "default" %} +

    {{stepsection.title}}

    + {% endif %} + {% for step in stepsection.steps %} +
  • {{ step }}
  • + {% endfor %} + {% endfor %} {% endif %}
    {%- endmacro %}