add a markdown source field to recipe

This commit is contained in:
Emi Vasilek 2023-11-21 03:12:41 +01:00
parent cb541423c7
commit 247fd37560
4 changed files with 41 additions and 1 deletions

View file

@ -6,6 +6,37 @@ import comfyrecipes.settings as settings
import comfyrecipes.issues as issues
import jsonschema
import lxml.html.clean
import mistune
class SafeHTML:
def __init__(self, clean_html: str) -> None:
self.html = clean_html
@classmethod
def from_html(cls, dirty_html: str) -> Self:
cleaner = lxml.html.clean.Cleaner(
allow_tags=["a", "b", "em", "i", "strong"],
safe_attrs_only=True,
safe_attrs=["href"],
)
html = cleaner.clean_html(dirty_html).strip()
assert isinstance(html, str)
assert html.startswith("<div>")
assert html.endswith("</div>")
html = html[5:-6]
return cls(html)
@classmethod
def from_markdown(cls, markdowntxt: str) -> Self:
dirty_html = mistune.html(markdowntxt)
assert isinstance(dirty_html, str)
return cls.from_html(dirty_html)
class Context:
def __init__(self) -> None:
@ -219,6 +250,7 @@ class Recipe(Element):
self,
ctx: Context,
title: str,
source: Optional[SafeHTML],
ingredients: List[IngredientInstance],
subrecipes: List["Recipe"],
price: Optional["PriceDB"],
@ -232,6 +264,7 @@ class Recipe(Element):
self.subrecipes = subrecipes
self.price = price
self.stepsections = stepsections
self.source = source
@classmethod
def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self:
@ -287,9 +320,14 @@ class Recipe(Element):
section = StepSection.from_dict(ctx, step)
stepsections.append(section)
source = None
if "source" in dct:
source = SafeHTML.from_markdown(dct["source"])
return cls(
ctx=ctx,
title=dct["title"],
source=source,
ingredients=ingredients,
subrecipes=subrecipes,
price=price,