Skip to content
Merged
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
35 changes: 17 additions & 18 deletions lib/tableau/extensions/post_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ defmodule Tableau.PostExtension do
@type post :: %{
body: String.t(),
file: String.t(),
renderer: fun(),
layout: module(),
date: DateTime.t()
}
Expand Down Expand Up @@ -112,49 +113,47 @@ defmodule Tableau.PostExtension do
|> Common.paths()
end)
|> Common.entries(fn %{path: path, front_matter: front_matter, pre_convert_body: body, ext: ext} ->
{
build(path, front_matter, body, config),
fn assigns ->
converter =
case front_matter[:converter] do
nil -> converters[ext]
converter -> Module.concat([converter])
end

converter.convert(path, front_matter, body, assigns)
end
}
build(path, front_matter, body, config, fn assigns ->
converter =
case front_matter[:converter] do
nil -> converters[ext]
converter -> Module.concat([converter])
end

converter.convert(path, front_matter, body, assigns)
end)
end)
|> Enum.sort_by(fn {post, _} -> post.date end, {:desc, DateTime})
|> Enum.sort_by(fn post -> post.date end, {:desc, DateTime})
|> then(fn posts ->
if config.future do
posts
else
Enum.reject(posts, fn {post, _} -> DateTime.after?(post.date, DateTime.utc_now()) end)
Enum.reject(posts, fn post -> DateTime.after?(post.date, DateTime.utc_now()) end)
end
end)

graph =
Tableau.Graph.insert(
token.graph,
Enum.map(posts, fn {post, renderer} ->
%Tableau.Page{parent: post.layout, permalink: post.permalink, template: renderer, opts: post}
Enum.map(posts, fn post ->
%Tableau.Page{parent: post.layout, permalink: post.permalink, template: post.renderer, opts: post}
end)
)

{:ok,
token
|> Map.put(:posts, posts |> Enum.unzip() |> elem(0))
|> Map.put(:posts, posts)
|> Map.put(:graph, graph)}
end

defp build(filename, attrs, body, posts_config) do
defp build(filename, attrs, body, posts_config, renderer) do
Application.put_env(:date_time_parser, :include_zones_from, ~N[2010-01-01T00:00:00])

attrs
|> Map.put(:__tableau_post_extension__, true)
|> Map.put(:body, body)
|> Map.put(:file, filename)
|> Map.put(:renderer, renderer)
|> Map.put(:layout, Module.concat([attrs[:layout] || posts_config.layout]))
|> Map.put(:date, DateTimeParser.parse_datetime!(attrs.date, assume_time: true, assume_utc: true))
|> Common.build_permalink(posts_config)
Expand Down
2 changes: 1 addition & 1 deletion lib/tableau/extensions/rss_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ defmodule Tableau.RSSExtension do
<link>#{URI.merge(url, post.permalink)}</link>
<pubDate>#{Calendar.strftime(post.date, "%a, %d %b %Y %X %Z")}</pubDate>
<guid>#{URI.merge(url, post.permalink)}</guid>
<description><![CDATA[ #{post.body} ]]></description>
<description><![CDATA[ #{post.renderer.(token)} ]]></description>
</item>
"""
end
Expand Down
15 changes: 10 additions & 5 deletions test/support/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
defmodule Tableau.Support.Helpers do
@moduledoc false
def post(idx, overrides) do
body = """
## Welcome to Post #{idx}

Here, we post like crazy.
"""

base = %{
title: "Post #{idx}",
permalink: "/posts/post-#{1}",
date: DateTime.utc_now(),
body: """
## Welcome to Post #{idx}

Here, we post like crazy.
"""
body: body,
renderer: fn _assigns ->
String.upcase(overrides[:body] || body)
end
}

Map.merge(base, Map.new(overrides))
Expand Down
36 changes: 18 additions & 18 deletions test/tableau/extensions/rss_extension_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ defmodule Tableau.RSSExtensionTest do
assert File.exists?(feed_path)
feed_content = File.read!(feed_path)

assert feed_content =~ "Post 1"
refute feed_content =~ "Post 2"
refute feed_content =~ "Post 3"
assert feed_content =~ "POST 1"
refute feed_content =~ "POST 2"
refute feed_content =~ "POST 3"
end
end

Expand Down Expand Up @@ -118,17 +118,17 @@ defmodule Tableau.RSSExtensionTest do
assert File.exists?(feed_path)
feed_content = File.read!(feed_path)

assert feed_content =~ "Post 1"
refute feed_content =~ "Post 2"
refute feed_content =~ "Post 3"
assert feed_content =~ "POST 1"
refute feed_content =~ "POST 2"
refute feed_content =~ "POST 3"
# only contains posts with category as "casual"
casual_path = Path.join(tmp_dir, "casual.xml")
assert File.exists?(casual_path)
casual_content = File.read!(casual_path)

refute casual_content =~ "Post 2"
refute casual_content =~ "Post 1"
assert casual_content =~ "Post 3"
refute casual_content =~ "POST 2"
refute casual_content =~ "POST 1"
assert casual_content =~ "POST 3"
end

test "includes everything if there is no includes key", %{token: token, tmp_dir: tmp_dir} do
Expand All @@ -149,9 +149,9 @@ defmodule Tableau.RSSExtensionTest do
assert File.exists?(feed_path)
feed_content = File.read!(feed_path)

assert feed_content =~ "Post 1"
assert feed_content =~ "Post 2"
assert feed_content =~ "Post 3"
assert feed_content =~ "POST 1"
assert feed_content =~ "POST 2"
assert feed_content =~ "POST 3"
end

test "excludes various frontmatter", %{token: token, tmp_dir: tmp_dir} do
Expand Down Expand Up @@ -180,18 +180,18 @@ defmodule Tableau.RSSExtensionTest do
assert File.exists?(not_posts_path)
not_posts_content = File.read!(not_posts_path)

refute not_posts_content =~ "Post 1"
assert not_posts_content =~ "Post 2"
assert not_posts_content =~ "Post 3"
refute not_posts_content =~ "POST 1"
assert not_posts_content =~ "POST 2"
assert not_posts_content =~ "POST 3"

# only contains posts without category as "bar"
not_casual_path = Path.join(tmp_dir, "not_casual.xml")
assert File.exists?(not_casual_path)
not_casual_content = File.read!(not_casual_path)

assert not_casual_content =~ "Post 2"
assert not_casual_content =~ "Post 1"
refute not_casual_content =~ "Post 3"
assert not_casual_content =~ "POST 2"
assert not_casual_content =~ "POST 1"
refute not_casual_content =~ "POST 3"
end
end
end