Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSS 2.0 + atom support #174

Open
wants to merge 6 commits into
base: v1/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:

strategy:
matrix:
otp-version: ['22.3', '23.3', '24.0']
elixir-version: ['1.13.3']
otp-version: ['26.x', '25.x', '24.x']
elixir-version: ['1.15', '1.16']

env:
MIX_ENV: test
Expand All @@ -23,10 +23,10 @@ jobs:
elixir-version: ${{ matrix.elixir-version}}

- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Cache Dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: |
_build
Expand Down
2 changes: 1 addition & 1 deletion lib/serum/markdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Serum.Markdown do
@spec to_html(binary(), Project.t()) :: binary()
def to_html(markdown, proj) do
markdown
|> Earmark.as_html!()
|> Earmark.as_html!(code_class_prefix: "lang-")
|> process_links(proj)
end

Expand Down
95 changes: 95 additions & 0 deletions lib/serum/plugin/rss_generator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
defmodule Serum.Plugins.RssGenerator do
@moduledoc """
A Serum plugin that creates an RSS feed

## Using the Plugin

# serum.exs:
%{
server_root: "https://example.io",
plugins: [
{Serum.Plugins.RssGenerator, only: :prod}
]
}
"""

@behaviour Serum.Plugin

serum_ver = Version.parse!(Mix.Project.config()[:version])
serum_req = "~> #{serum_ver.major}.#{serum_ver.minor}"

require EEx
alias Serum.GlobalBindings
alias Serum.Page
alias Serum.Post

def name, do: "Create RSS feed for humans"
def version, do: "1.2.0"
def elixir, do: ">= 1.8.0"
def serum, do: unquote(serum_req)

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
{pages, posts} = get_items(args[:for])

dest
|> create_file(pages, posts)
|> Serum.File.write()
|> case do
{:ok, _} -> :ok
{:error, _} = error -> error
end
end

@spec get_items(term()) :: {[Page.t()], [Post.t()]}
defp get_items(arg)
defp get_items(nil), do: get_items([:posts])
defp get_items(arg) when not is_list(arg), do: get_items([arg])

defp get_items(arg) do
pages = if :pages in arg, do: GlobalBindings.get(:all_pages), else: []
posts = if :posts in arg, do: GlobalBindings.get(:all_posts), else: []

{pages, posts}
end

rss_path =
:serum
|> :code.priv_dir()
|> Path.join("build_resources")
|> Path.join("rss.xml.eex")

EEx.function_from_file(:defp, :rss_xml, rss_path, [
:pages,
:posts,
:transformer,
:bindings
])

@spec create_file(binary(), [Page.t()], [Post.t()]) :: Serum.File.t()
defp create_file(dest, pages, posts) do
%Serum.File{
dest: Path.join(dest, "rss.xml"),
out_data: rss_xml(pages, posts, &to_rfc822_format/1, bindings())
}
end

defp to_rfc822_format(_now) do
# reference to https://www.w3.org/TR/NOTE-datetime
# 10 Mar 21 22:43:37 UTC
# NaiveDateTime.from_erl!()
# Timex.now()
# |> Timex.format!("%d %b %y %T Z", :strftime)
# "10 Mar 21 22:43:37 UTC"
end

defp bindings do
:site
|> GlobalBindings.get()
end
end
19 changes: 10 additions & 9 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ defmodule Serum.Mixfile do
defp deps do
[
{:earmark, "~> 1.4"},
{:file_system, "0.2.10"},
{:microscope, ">= 1.4.0"},
{:file_system, "~> 1.0", override: true},
{:microscope, "~> 1.4.0"},
{:timex, "~> 3.7"},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:excoveralls, "~> 0.14", only: [:test], runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:floki, "0.33.1"},
{:ex_doc, "~> 0.28", only: :dev, runtime: false},
{:mix_test_watch, "~> 1.0", only: :dev, runtime: false},
{:mox, "~> 1.0", only: :test}
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:excoveralls, "~> 0.18", only: [:test], runtime: false},
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
{:floki, "~> 0.35"},
{:ex_doc, "~> 0.31", only: :dev, runtime: false},
{:mix_test_watch, "~> 1.1", only: :dev, runtime: false},
{:mox, "~> 1.1", only: :test}
]
end

Expand Down Expand Up @@ -87,6 +87,7 @@ defmodule Serum.Mixfile do
"Built-in Plugins": [
Serum.Plugins.LiveReloader,
Serum.Plugins.PreviewGenerator,
Serum.Plugins.RssGenerator,
Serum.Plugins.SitemapGenerator,
Serum.Plugins.TableOfContents
],
Expand Down
Loading