Ich habe eine Funktion, die einen HTML-Text analysiert, um die Open Graph-Attribute wie unten zu erhalten.Wie kann Elixir Stream verwendet werden, um mehrere Objekte in dieser Situation zu verarbeiten?
Ich bin mir nicht sicher, wie man Stream nutzt, damit das Parsen nur einmal durchgeführt werden kann - und das ist sogar möglich?
def og(body) do
image = attribute_content(body, "meta[property=og:image]")
title = attribute_content(body, "meta[property=og:title]")
site_name = attribute_content(body, "meta[property=og:site_name]")
desc = attribute_content(body, "meta[property=og:description]")
type = attribute_content(body, "meta[property=og:type]")
url = attribute_content(body, "meta[property=og:url]")
author = attribute_content(body, "meta[name=author]")
%{image: image, title: title, type: type,
site_title: site_title, url: url, site_name: site_name,
description: desc, author: author}
end
@doc """
Parse html body for the target element and return the content.
"""
defp attribute_content(body, target) do
Floki.find(body, target) |> Floki.attribute("content") |> List.first
end
Was ist 'attribute_content'? – Dogbert
Nur eine private Hilfsfunktion, um den Attribut-Inhalt zu erhalten. Editierte die ursprüngliche Frage und fügte die Funktion für Klarheit hinzu. –