pre.hn/lib/pre_dot_hn/link_log.ex

84 lines
2.3 KiB
Elixir

defmodule PreDotHn.LinkLog do
alias PreDotHn.Markdown
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"?>
<feed version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<title>pre.hn - Links</title>
<subtitle>Robert Prehn's Link Log</subtitle>
<link rel="alternative" type="text/html">https://pre.hn/link-log/</link>
<link rel="self" type="application/rss+xml">https://pre.hn/link-log/feed.rss</link>
<%= for link <- @links do %>
<entry>
<title><%= Map.get(link, "emoji", "🔗") %> <%= HtmlSanitizeEx.strip_tags(link["name"]) %></title>
<content type="xhtml" xml:lang="en"
xml:base="https://pre.hn/"><![CDATA[<html><body>
<%= PreDotHn.Markdown.render(link["summary"]) %>
</body></html>]]>
</content>
<id>tag:pre.hn,<%= link["url"] %></id>
<link rel="alternate" type="text/html"><%= link["url"] %></link>
</entry>
<% end %>
</feed>
""",
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="padding-left: 2rem;text-indent: -2rem;" href={link["url"]}>
<%= link["emoji"] || "🔗"%> <%= link["name"] %>
</a>
<div class="link-log-body">
<%= {:safe, Markdown.render(link["summary"])} %>
</div>
</div>
<% end %>
"""
|> rendered_to_string()
%{
title: "Link Log",
slug: "link-log",
date: "2024-03-07",
body: body
}
end
end