defmodule Lain.LinkLog do use Phoenix.Component import Phoenix.LiveViewTest, only: [rendered_to_string: 1] def run(path) do links = links(path) make_feed(links, path) make_stylesheet(path) links |> make_index(path) |> Lain.write_page(path) end def links(path) do link_path = Path.join(path, "link-log.yaml") YamlElixir.read_from_file!(link_path) end def make_feed(links, path) do links = Enum.take(links, 10) assigns = %{links: links} body = EEx.eval_string( """ pre.hn - Links Robert Prehn's Link Log https://pre.hn/link-log/ https://pre.hn/link-log/feed.rss <%= for link <- @links do %> <%= Map.get(link, "emoji", "🔗") %> <%= HtmlSanitizeEx.strip_tags(link["name"]) %> <%= Lain.Markdown.render(link["summary"]) %> ]]> tag:pre.hn,<%= link["url"] %> <%= link["url"] %> <% end %> """, assigns: assigns ) dir_path = Path.join([path, "build", "link-log"]) File.mkdir_p!(dir_path) path = Path.join([dir_path, "feed.rss"]) File.write!(path, body) end def make_stylesheet(path) do tailwind_args = [ "--config=#{Path.join(Path.expand(path), "tailwind.config.js")}", "--input=#{Path.join(Path.expand(path), "assets/app.css")}", "--output=#{Path.join(Path.expand(path), "build/assets/app.css")}", "--minify" ] Application.put_all_env( tailwind: [ version: "3.2.7", default: [ args: tailwind_args, cd: Path.expand(path) ] ] ) Tailwind.install_and_run(:default, tailwind_args) end def make_index(links, path) do path = Path.join([path, "templates", "link.html.heex"]) assigns = %{links: links, path: path} body = ~H""" <%= for link <- @links do %> <%= Lain.render_template(@path, %{link: link}) %> <% end %> """ |> rendered_to_string() %{ title: "Link Log", slug: "link-log", date: "2024-03-07", body: body } end end