pre.hn/lib/lain/link_log.ex

102 lines
2.6 KiB
Elixir

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(
"""
<?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>
<%= Lain.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
)
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