Skip to content

Instantly share code, notes, and snippets.

@jakubpawlowicz
Last active February 18, 2022 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubpawlowicz/602d5f1d6940c6b0d9b475b5f4bbffd9 to your computer and use it in GitHub Desktop.
Save jakubpawlowicz/602d5f1d6940c6b0d9b475b5f4bbffd9 to your computer and use it in GitHub Desktop.
Simple Serum (https://dalgona.github.io/Serum/) plugin building RSS of latest posts.

Simple Serum plugin building RSS of latest posts.

Put rss_generator.ex under lib, rss.xml.eex under pages and add RssGenerator to plugins in your serum.exs file.

You may also need to add elixirc_paths: ["lib"] inside mix.exs's project, so Elixir knows it has to compile lib, which is not the Serum's default.

Important!: don't forget to customize the RSS title, description, URL's etc in rss.xml.eex.

p.s. It's code adapted from an unmerged Serum PR

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Jakub Pawlowicz posts</title>
<description>My thoughts on everything</description>
<lastBuildDate><%= Timex.now() |> Timex.format!("%a, %d %b %Y %T GMT", :strftime) %></lastBuildDate>
<atom:link href="https://jakubpawlowicz.com/rss.xml" rel="self" type="application/rss+xml" />
<%= for post <- posts do %>
<item>
<link>https://jakubpawlowicz.com<%= post.url %></link>
<guid>https://jakubpawlowicz.com<%= post.url %></guid>
<title><%= elem(Phoenix.HTML.html_escape(post.title), 1) %></title>
<description><%= elem(Phoenix.HTML.html_escape(post.extras["preview"]), 1) %></description>
<pubDate><%= "#{post.date}T09:00:00Z" |> DateTime.from_iso8601() |> elem(1) |> Timex.format!("%a, %d %b %Y %T GMT", :strftime) %></pubDate>
</item>
<% end %>
</channel>
</rss>
defmodule RssGenerator do
require EEx
@behaviour Serum.Plugin
@rss_path Path.join([File.cwd!(), "pages", "rss.xml.eex"])
def name, do: "Create RSS feed for humans"
def version, do: "1.0.0"
def elixir, do: ">= 1.12.0"
def serum, do: "~> 1.5.0"
def description do
"Create an RSS feed so that humans can read fresh new posts."
end
def implements, do: [build_succeeded: 3]
def build_succeeded(_src, dest, _args) do
posts = Serum.GlobalBindings.get(:all_posts)
dest
|> create_file(posts)
|> Serum.File.write()
|> case do
{:ok, _} -> :ok
{:error, _} = error -> error
end
end
EEx.function_from_file(:defp, :rss_xml, @rss_path, [:posts])
defp create_file(dest, posts) do
%Serum.File{
dest: Path.join(dest, "rss.xml"),
out_data: rss_xml(posts)
}
end
end
%{
site_name: "Jakub Pawlowicz",
site_description: "",
date_format: "{YYYY}-{MM}-{DD}",
base_url: "/",
author: "Jakub Pawlowicz",
author_email: "contact@jakubpawlowicz.com",
posts_path: "log",
tags_path: "log/tags",
plugins: [
Serum.Plugins.PreviewGenerator,
RssGenerator,
{Serum.Plugins.LiveReloader, only: :dev}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment