pre.hn/lib/pre_dot_hn/link_log.ex
2024-03-07 09:29:15 -06:00

75 lines
1.8 KiB
Elixir

defmodule PreDotHn.LinkLog do
use Phoenix.Component
import Phoenix.LiveViewTest, only: [rendered_to_string: 1]
def run do
links = links()
make_feed(links)
links
|> make_index()
|> PreDotHn.write_page()
end
def links do
link_path = Path.join(["site", "link-log.yaml"])
YamlElixir.read_from_file!(link_path)
end
def make_feed(links) do
links = Enum.take(links, 10)
assigns = %{links: links}
body =
EEx.eval_string("""
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>pre.hn - Links</title>
<description>Robert Prehn's Link Log</description>
<link>https://pre.hn</link>
<atom:link href="https://pre.hn/link-log/feed.rss" rel="self" type="application/rss+xml" />
<%= for link <- @links do %>
<item>
<title><%= HtmlSanitizeEx.strip_tags(link["name"]) %></title>
<description>
<%= link["summary"] %>
</description>
<guid isPermaLink="true"><%= link["url"] %></guid>
</item>
<% end %>
</channel>
</rss>
""", assigns: assigns)
path = Path.join(["priv", "static", "link-log", "feed.rss"])
File.write!(path, body)
end
def make_index(links) do
assigns = %{links: links}
body =
~H"""
<%= for link <- @links do %>
<div style="margin-bottom: 1rem">
<a class="text-ellipsis overflow-hidden inline-block" style="max-width: 40ch" href={link["url"]}><%= link["name"] %></a>
<%= Earmark.as_html!(link["summary"]) |> Phoenix.HTML.raw() %>
</div>
<% end %>
"""
|> rendered_to_string()
%{
title: "Link Log",
slug: "link-log",
date: "2024-03-07",
body: body
}
end
end