Unify generation render fallbacks
This commit is contained in:
1
AUDIT.md
1
AUDIT.md
@@ -25,6 +25,7 @@ after each item.
|
|||||||
- [x] `generation/sitemap.ex`
|
- [x] `generation/sitemap.ex`
|
||||||
- [x] `generation/data.ex`
|
- [x] `generation/data.ex`
|
||||||
- [x] `generation/paths.ex`
|
- [x] `generation/paths.ex`
|
||||||
|
- [x] `generation/renderers.ex`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -47,25 +47,7 @@ defmodule BDS.Generation.Renderers do
|
|||||||
@spec render_archive_page(map(), String.t(), [map()], String.t() | nil, String.t(), map()) ::
|
@spec render_archive_page(map(), String.t(), [map()], String.t() | nil, String.t(), map()) ::
|
||||||
String.t()
|
String.t()
|
||||||
def render_archive_page(plan, title, posts, language, kind, pagination) do
|
def render_archive_page(plan, title, posts, language, kind, pagination) do
|
||||||
fallback = fn ->
|
fallback = fn -> render_inline_list_page(title, posts, language, kind) end
|
||||||
items =
|
|
||||||
posts
|
|
||||||
|> Enum.map(fn post -> ["<li>", post.title, "</li>"] end)
|
|
||||||
|> IO.iodata_to_binary()
|
|
||||||
|
|
||||||
[
|
|
||||||
"<html><body data-kind=\"",
|
|
||||||
kind,
|
|
||||||
"\" data-language=\"",
|
|
||||||
to_string(language),
|
|
||||||
"\"><h1>",
|
|
||||||
title,
|
|
||||||
"</h1><ul>",
|
|
||||||
items,
|
|
||||||
"</ul></body></html>"
|
|
||||||
]
|
|
||||||
|> IO.iodata_to_binary()
|
|
||||||
end
|
|
||||||
|
|
||||||
render_list_output(
|
render_list_output(
|
||||||
plan,
|
plan,
|
||||||
@@ -92,23 +74,7 @@ defmodule BDS.Generation.Renderers do
|
|||||||
@spec render_date_archive_page(map(), String.t(), map(), [map()], String.t() | nil, map()) ::
|
@spec render_date_archive_page(map(), String.t(), map(), [map()], String.t() | nil, map()) ::
|
||||||
String.t()
|
String.t()
|
||||||
def render_date_archive_page(plan, label, archive_context, posts, language, pagination) do
|
def render_date_archive_page(plan, label, archive_context, posts, language, pagination) do
|
||||||
fallback = fn ->
|
fallback = fn -> render_inline_list_page(label, posts, language, "date") end
|
||||||
items =
|
|
||||||
posts
|
|
||||||
|> Enum.map(fn post -> ["<li>", post.title, "</li>"] end)
|
|
||||||
|> IO.iodata_to_binary()
|
|
||||||
|
|
||||||
[
|
|
||||||
"<html><body data-kind=\"date\" data-language=\"",
|
|
||||||
to_string(language),
|
|
||||||
"\"><h1>",
|
|
||||||
label,
|
|
||||||
"</h1><ul>",
|
|
||||||
items,
|
|
||||||
"</ul></body></html>"
|
|
||||||
]
|
|
||||||
|> IO.iodata_to_binary()
|
|
||||||
end
|
|
||||||
|
|
||||||
render_list_output(
|
render_list_output(
|
||||||
plan,
|
plan,
|
||||||
@@ -124,10 +90,7 @@ defmodule BDS.Generation.Renderers do
|
|||||||
@doc "Try the project's post template; on error, fall back to the inline `fallback` thunk."
|
@doc "Try the project's post template; on error, fall back to the inline `fallback` thunk."
|
||||||
@spec render_post_output(String.t(), String.t() | nil, map(), (-> String.t())) :: String.t()
|
@spec render_post_output(String.t(), String.t() | nil, map(), (-> String.t())) :: String.t()
|
||||||
def render_post_output(project_id, template_slug, assigns, fallback) do
|
def render_post_output(project_id, template_slug, assigns, fallback) do
|
||||||
case Rendering.render_post_page(project_id, template_slug, assigns) do
|
render_with_fallback(fn -> Rendering.render_post_page(project_id, template_slug, assigns) end, fallback)
|
||||||
{:ok, rendered} -> rendered
|
|
||||||
{:error, _reason} -> fallback.()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc "Render a list/archive page through the project template, falling back to inline."
|
@doc "Render a list/archive page through the project template, falling back to inline."
|
||||||
@@ -151,30 +114,34 @@ defmodule BDS.Generation.Renderers do
|
|||||||
fallback
|
fallback
|
||||||
)
|
)
|
||||||
when is_binary(project_id) do
|
when is_binary(project_id) do
|
||||||
case Rendering.render_list_page(project_id, %{
|
render_with_fallback(
|
||||||
language: language,
|
fn ->
|
||||||
language_prefix: Paths.language_prefix(language, main_language),
|
Rendering.render_list_page(project_id, %{
|
||||||
page_title: page_title,
|
language: language,
|
||||||
posts: posts,
|
language_prefix: Paths.language_prefix(language, main_language),
|
||||||
archive_context: archive_context,
|
page_title: page_title,
|
||||||
pagination: pagination
|
posts: posts,
|
||||||
}) do
|
archive_context: archive_context,
|
||||||
{:ok, rendered} -> rendered
|
pagination: pagination
|
||||||
{:error, _reason} -> fallback.()
|
})
|
||||||
end
|
end,
|
||||||
|
fallback
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc "Render the project's 404 page via its template, falling back to a static page."
|
@doc "Render the project's 404 page via its template, falling back to a static page."
|
||||||
@spec render_not_found_output(map(), String.t() | nil) :: String.t()
|
@spec render_not_found_output(map(), String.t() | nil) :: String.t()
|
||||||
def render_not_found_output(%{project_id: project_id, language: main_language}, language)
|
def render_not_found_output(%{project_id: project_id, language: main_language}, language)
|
||||||
when is_binary(project_id) do
|
when is_binary(project_id) do
|
||||||
case Rendering.render_not_found_page(project_id, %{
|
render_with_fallback(
|
||||||
language: language,
|
fn ->
|
||||||
language_prefix: Paths.language_prefix(language, main_language)
|
Rendering.render_not_found_page(project_id, %{
|
||||||
}) do
|
language: language,
|
||||||
{:ok, rendered} -> rendered
|
language_prefix: Paths.language_prefix(language, main_language)
|
||||||
{:error, _reason} -> render_not_found_page(language)
|
})
|
||||||
end
|
end,
|
||||||
|
fn -> render_not_found_page(language) end
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc "Static fallback HTML for a 404 page."
|
@doc "Static fallback HTML for a 404 page."
|
||||||
@@ -227,6 +194,33 @@ defmodule BDS.Generation.Renderers do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp render_with_fallback(render_fun, fallback) do
|
||||||
|
case render_fun.() do
|
||||||
|
{:ok, rendered} -> rendered
|
||||||
|
{:error, _reason} -> fallback.()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render_inline_list_page(title, posts, language, kind) do
|
||||||
|
items =
|
||||||
|
posts
|
||||||
|
|> Enum.map(fn post -> ["<li>", post.title, "</li>"] end)
|
||||||
|
|> IO.iodata_to_binary()
|
||||||
|
|
||||||
|
[
|
||||||
|
"<html><body data-kind=\"",
|
||||||
|
kind,
|
||||||
|
"\" data-language=\"",
|
||||||
|
to_string(language),
|
||||||
|
"\"><h1>",
|
||||||
|
title,
|
||||||
|
"</h1><ul>",
|
||||||
|
items,
|
||||||
|
"</ul></body></html>"
|
||||||
|
]
|
||||||
|
|> IO.iodata_to_binary()
|
||||||
|
end
|
||||||
|
|
||||||
defp parse_frontmatter_body(contents) do
|
defp parse_frontmatter_body(contents) do
|
||||||
case String.split(contents, "\n---\n", parts: 2) do
|
case String.split(contents, "\n---\n", parts: 2) do
|
||||||
[_frontmatter, body] -> String.trim_trailing(body, "\n")
|
[_frontmatter, body] -> String.trim_trailing(body, "\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user