chore: added more @spec
This commit is contained in:
@@ -40,7 +40,13 @@ defmodule BDS.Generation.Data do
|
||||
post_snapshot_candidates
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.reduce(%{}, fn {post, index}, acc ->
|
||||
:ok = report_snapshot_stage_progress(on_snapshot_progress, :posts, index, length(post_snapshot_candidates))
|
||||
:ok =
|
||||
report_snapshot_stage_progress(
|
||||
on_snapshot_progress,
|
||||
:posts,
|
||||
index,
|
||||
length(post_snapshot_candidates)
|
||||
)
|
||||
|
||||
case published_post_snapshot(project_data_dir, post) do
|
||||
nil -> acc
|
||||
@@ -54,7 +60,9 @@ defmodule BDS.Generation.Data do
|
||||
|> then(fn published ->
|
||||
draft_candidates
|
||||
|> merge_generation_snapshots(snapshots_by_id)
|
||||
|> Enum.reduce(Map.new(published, &{&1.id, &1}), fn post, acc -> Map.put(acc, post.id, post) end)
|
||||
|> Enum.reduce(Map.new(published, &{&1.id, &1}), fn post, acc ->
|
||||
Map.put(acc, post.id, post)
|
||||
end)
|
||||
|> Map.values()
|
||||
end)
|
||||
|> Enum.sort_by(&{-(&1.created_at || 0), -(&1.published_at || 0), to_string(&1.slug)})
|
||||
@@ -100,7 +108,12 @@ defmodule BDS.Generation.Data do
|
||||
end
|
||||
|
||||
@spec resolve_posts_for_language([map()], String.t() | nil, map(), String.t() | nil) :: [map()]
|
||||
def resolve_posts_for_language(posts, target_language, translations_by_post_language, main_language) do
|
||||
def resolve_posts_for_language(
|
||||
posts,
|
||||
target_language,
|
||||
translations_by_post_language,
|
||||
main_language
|
||||
) do
|
||||
target = String.downcase(to_string(target_language || ""))
|
||||
main = String.downcase(to_string(main_language || ""))
|
||||
|
||||
@@ -126,22 +139,42 @@ defmodule BDS.Generation.Data do
|
||||
|
||||
@spec build_generation_post_index([map()]) :: map()
|
||||
def build_generation_post_index(posts) do
|
||||
Enum.reduce(posts, %{posts_by_category: %{}, posts_by_tag: %{}, posts_by_year: %{}, posts_by_year_month: %{}, posts_by_year_month_day: %{}}, fn post, acc ->
|
||||
{year, month_value, day_value} = local_date_parts!(post.created_at)
|
||||
month = String.pad_leading(Integer.to_string(month_value), 2, "0")
|
||||
day = String.pad_leading(Integer.to_string(day_value), 2, "0")
|
||||
year_month = "#{year}/#{month}"
|
||||
year_month_day = "#{year}/#{month}/#{day}"
|
||||
Enum.reduce(
|
||||
posts,
|
||||
%{
|
||||
posts_by_category: %{},
|
||||
posts_by_tag: %{},
|
||||
posts_by_year: %{},
|
||||
posts_by_year_month: %{},
|
||||
posts_by_year_month_day: %{}
|
||||
},
|
||||
fn post, acc ->
|
||||
{year, month_value, day_value} = local_date_parts!(post.created_at)
|
||||
month = String.pad_leading(Integer.to_string(month_value), 2, "0")
|
||||
day = String.pad_leading(Integer.to_string(day_value), 2, "0")
|
||||
year_month = "#{year}/#{month}"
|
||||
year_month_day = "#{year}/#{month}/#{day}"
|
||||
|
||||
acc
|
||||
|> append_generation_index(:posts_by_year, year, post)
|
||||
|> append_generation_index(:posts_by_year_month, year_month, post)
|
||||
|> append_generation_index(:posts_by_year_month_day, year_month_day, post)
|
||||
|> then(fn indexed ->
|
||||
indexed = Enum.reduce(post.categories || [], indexed, &append_generation_index(&2, :posts_by_category, &1, post))
|
||||
Enum.reduce(post.tags || [], indexed, &append_generation_index(&2, :posts_by_tag, &1, post))
|
||||
end)
|
||||
end)
|
||||
acc
|
||||
|> append_generation_index(:posts_by_year, year, post)
|
||||
|> append_generation_index(:posts_by_year_month, year_month, post)
|
||||
|> append_generation_index(:posts_by_year_month_day, year_month_day, post)
|
||||
|> then(fn indexed ->
|
||||
indexed =
|
||||
Enum.reduce(
|
||||
post.categories || [],
|
||||
indexed,
|
||||
&append_generation_index(&2, :posts_by_category, &1, post)
|
||||
)
|
||||
|
||||
Enum.reduce(
|
||||
post.tags || [],
|
||||
indexed,
|
||||
&append_generation_index(&2, :posts_by_tag, &1, post)
|
||||
)
|
||||
end)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
## --- internals -----------------------------------------------------------
|
||||
@@ -168,9 +201,11 @@ defmodule BDS.Generation.Data do
|
||||
"page" => %{render_in_lists: false, show_title: true}
|
||||
}
|
||||
|
||||
Enum.reduce(Map.get(plan, :category_settings, %{}) || %{}, defaults, fn {category, settings}, acc ->
|
||||
Enum.reduce(Map.get(plan, :category_settings, %{}) || %{}, defaults, fn {category, settings},
|
||||
acc ->
|
||||
Map.put(acc, category, %{
|
||||
render_in_lists: category_setting_flag(settings, :render_in_lists, "render_in_lists", true),
|
||||
render_in_lists:
|
||||
category_setting_flag(settings, :render_in_lists, "render_in_lists", true),
|
||||
show_title: category_setting_flag(settings, :show_title, "show_title", true)
|
||||
})
|
||||
end)
|
||||
@@ -207,23 +242,30 @@ defmodule BDS.Generation.Data do
|
||||
{:ok, contents} ->
|
||||
{:ok, %{fields: fields}} = Frontmatter.parse_document(contents)
|
||||
|
||||
%Post{fallback_post |
|
||||
id: DocumentFields.get(fields, "id", fallback_post.id),
|
||||
title: DocumentFields.get(fields, "title", fallback_post.title) || "",
|
||||
slug: DocumentFields.fetch!(fields, "slug"),
|
||||
excerpt: Map.get(fields, "excerpt"),
|
||||
content: nil,
|
||||
status: :published,
|
||||
author: Map.get(fields, "author"),
|
||||
language: Map.get(fields, "language", fallback_post.language),
|
||||
do_not_translate: DocumentFields.get(fields, "doNotTranslate", fallback_post.do_not_translate || false),
|
||||
template_slug: DocumentFields.get(fields, "templateSlug", fallback_post.template_slug),
|
||||
created_at: DocumentFields.get(fields, "createdAt", fallback_post.created_at),
|
||||
updated_at: DocumentFields.get(fields, "updatedAt", fallback_post.updated_at),
|
||||
published_at: DocumentFields.get(fields, "publishedAt", fallback_post.published_at),
|
||||
file_path: fallback_post.file_path,
|
||||
tags: Map.get(fields, "tags", fallback_post.tags || []),
|
||||
categories: Map.get(fields, "categories", fallback_post.categories || [])
|
||||
%Post{
|
||||
fallback_post
|
||||
| id: DocumentFields.get(fields, "id", fallback_post.id),
|
||||
title: DocumentFields.get(fields, "title", fallback_post.title) || "",
|
||||
slug: DocumentFields.fetch!(fields, "slug"),
|
||||
excerpt: Map.get(fields, "excerpt"),
|
||||
content: nil,
|
||||
status: :published,
|
||||
author: Map.get(fields, "author"),
|
||||
language: Map.get(fields, "language", fallback_post.language),
|
||||
do_not_translate:
|
||||
DocumentFields.get(
|
||||
fields,
|
||||
"doNotTranslate",
|
||||
fallback_post.do_not_translate || false
|
||||
),
|
||||
template_slug:
|
||||
DocumentFields.get(fields, "templateSlug", fallback_post.template_slug),
|
||||
created_at: DocumentFields.get(fields, "createdAt", fallback_post.created_at),
|
||||
updated_at: DocumentFields.get(fields, "updatedAt", fallback_post.updated_at),
|
||||
published_at: DocumentFields.get(fields, "publishedAt", fallback_post.published_at),
|
||||
file_path: fallback_post.file_path,
|
||||
tags: Map.get(fields, "tags", fallback_post.tags || []),
|
||||
categories: Map.get(fields, "categories", fallback_post.categories || [])
|
||||
}
|
||||
|
||||
{:error, _reason} ->
|
||||
@@ -231,13 +273,20 @@ defmodule BDS.Generation.Data do
|
||||
end
|
||||
end
|
||||
|
||||
defp build_generation_route_posts(project_id, project_data_dir, published_posts, on_snapshot_progress) do
|
||||
defp build_generation_route_posts(
|
||||
project_id,
|
||||
project_data_dir,
|
||||
published_posts,
|
||||
on_snapshot_progress
|
||||
) do
|
||||
source_post_ids = Enum.map(published_posts, & &1.id)
|
||||
|
||||
translation_candidates =
|
||||
Repo.all(
|
||||
from translation in Translation,
|
||||
where: translation.project_id == ^project_id and translation.translation_for in ^source_post_ids,
|
||||
where:
|
||||
translation.project_id == ^project_id and
|
||||
translation.translation_for in ^source_post_ids,
|
||||
where: translation.status in [:published, :draft],
|
||||
order_by: [asc: translation.translation_for, asc: translation.language]
|
||||
)
|
||||
@@ -246,7 +295,13 @@ defmodule BDS.Generation.Data do
|
||||
translation_candidates
|
||||
|> Enum.with_index(1)
|
||||
|> Enum.reduce(%{}, fn {translation, index}, acc ->
|
||||
:ok = report_snapshot_stage_progress(on_snapshot_progress, :translations, index, length(translation_candidates))
|
||||
:ok =
|
||||
report_snapshot_stage_progress(
|
||||
on_snapshot_progress,
|
||||
:translations,
|
||||
index,
|
||||
length(translation_candidates)
|
||||
)
|
||||
|
||||
case published_translation_snapshot(project_data_dir, translation) do
|
||||
nil -> acc
|
||||
@@ -288,18 +343,20 @@ defmodule BDS.Generation.Data do
|
||||
{:ok, contents} ->
|
||||
{:ok, %{fields: fields}} = Frontmatter.parse_document(contents)
|
||||
|
||||
%Translation{fallback_translation |
|
||||
id: DocumentFields.get(fields, "id", fallback_translation.id),
|
||||
translation_for: DocumentFields.fetch!(fields, "translationFor"),
|
||||
language: DocumentFields.fetch!(fields, "language"),
|
||||
title: DocumentFields.get(fields, "title", fallback_translation.title) || "",
|
||||
excerpt: Map.get(fields, "excerpt", fallback_translation.excerpt),
|
||||
content: nil,
|
||||
status: :published,
|
||||
created_at: DocumentFields.get(fields, "createdAt", fallback_translation.created_at),
|
||||
updated_at: DocumentFields.get(fields, "updatedAt", fallback_translation.updated_at),
|
||||
published_at: DocumentFields.get(fields, "publishedAt", fallback_translation.published_at),
|
||||
file_path: fallback_translation.file_path
|
||||
%Translation{
|
||||
fallback_translation
|
||||
| id: DocumentFields.get(fields, "id", fallback_translation.id),
|
||||
translation_for: DocumentFields.fetch!(fields, "translationFor"),
|
||||
language: DocumentFields.fetch!(fields, "language"),
|
||||
title: DocumentFields.get(fields, "title", fallback_translation.title) || "",
|
||||
excerpt: Map.get(fields, "excerpt", fallback_translation.excerpt),
|
||||
content: nil,
|
||||
status: :published,
|
||||
created_at: DocumentFields.get(fields, "createdAt", fallback_translation.created_at),
|
||||
updated_at: DocumentFields.get(fields, "updatedAt", fallback_translation.updated_at),
|
||||
published_at:
|
||||
DocumentFields.get(fields, "publishedAt", fallback_translation.published_at),
|
||||
file_path: fallback_translation.file_path
|
||||
}
|
||||
|
||||
{:error, _reason} ->
|
||||
|
||||
@@ -25,8 +25,16 @@ defmodule BDS.Generation.Outputs do
|
||||
end)
|
||||
end
|
||||
|
||||
@spec build_validation_route_paths(map(), [map()], [map()], map(), String.t() | nil) :: [String.t()]
|
||||
def build_validation_route_paths(plan, route_posts, published_list_posts, post_index, route_language) do
|
||||
@spec build_validation_route_paths(map(), [map()], [map()], map(), String.t() | nil) :: [
|
||||
String.t()
|
||||
]
|
||||
def build_validation_route_paths(
|
||||
plan,
|
||||
route_posts,
|
||||
published_list_posts,
|
||||
post_index,
|
||||
route_language
|
||||
) do
|
||||
[
|
||||
core_route_paths(plan, published_list_posts, route_language),
|
||||
page_route_paths(plan, route_posts, route_language),
|
||||
@@ -250,7 +258,9 @@ defmodule BDS.Generation.Outputs do
|
||||
Enum.flat_map(posts_by_tag, fn {tag, posts} ->
|
||||
tag_slug = archive_route_segment(tag)
|
||||
|
||||
build_paginated_archive_outputs(plan, languages, ["tag", tag_slug], posts, fn page_posts, language, pagination ->
|
||||
build_paginated_archive_outputs(plan, languages, ["tag", tag_slug], posts, fn page_posts,
|
||||
language,
|
||||
pagination ->
|
||||
render_archive_page(plan, tag, page_posts, language, "tag", pagination)
|
||||
end)
|
||||
end)
|
||||
@@ -260,23 +270,31 @@ defmodule BDS.Generation.Outputs do
|
||||
def build_date_outputs(plan, post_index, languages) do
|
||||
year_outputs =
|
||||
Enum.flat_map(post_index.posts_by_year, fn {year, posts} ->
|
||||
build_paginated_archive_outputs(plan, languages, [Integer.to_string(year)], posts, fn page_posts, language, pagination ->
|
||||
render_date_archive_page(
|
||||
plan,
|
||||
Integer.to_string(year),
|
||||
%{kind: "year", year: year},
|
||||
page_posts,
|
||||
language,
|
||||
pagination
|
||||
)
|
||||
end)
|
||||
build_paginated_archive_outputs(
|
||||
plan,
|
||||
languages,
|
||||
[Integer.to_string(year)],
|
||||
posts,
|
||||
fn page_posts, language, pagination ->
|
||||
render_date_archive_page(
|
||||
plan,
|
||||
Integer.to_string(year),
|
||||
%{kind: "year", year: year},
|
||||
page_posts,
|
||||
language,
|
||||
pagination
|
||||
)
|
||||
end
|
||||
)
|
||||
end)
|
||||
|
||||
month_outputs =
|
||||
Enum.flat_map(post_index.posts_by_year_month, fn {year_month, posts} ->
|
||||
[year, month] = String.split(year_month, "/", parts: 2)
|
||||
|
||||
build_paginated_archive_outputs(plan, languages, [year, month], posts, fn page_posts, language, pagination ->
|
||||
build_paginated_archive_outputs(plan, languages, [year, month], posts, fn page_posts,
|
||||
language,
|
||||
pagination ->
|
||||
render_date_archive_page(
|
||||
plan,
|
||||
"#{year}-#{month}",
|
||||
@@ -292,11 +310,18 @@ defmodule BDS.Generation.Outputs do
|
||||
Enum.flat_map(post_index.posts_by_year_month_day, fn {year_month_day, posts} ->
|
||||
[year, month, day] = String.split(year_month_day, "/", parts: 3)
|
||||
|
||||
build_paginated_archive_outputs(plan, languages, [year, month, day], posts, fn page_posts, language, pagination ->
|
||||
build_paginated_archive_outputs(plan, languages, [year, month, day], posts, fn page_posts,
|
||||
language,
|
||||
pagination ->
|
||||
render_date_archive_page(
|
||||
plan,
|
||||
"#{year}-#{month}-#{day}",
|
||||
%{kind: "day", year: String.to_integer(year), month: String.to_integer(month), day: String.to_integer(day)},
|
||||
%{
|
||||
kind: "day",
|
||||
year: String.to_integer(year),
|
||||
month: String.to_integer(month),
|
||||
day: String.to_integer(day)
|
||||
},
|
||||
page_posts,
|
||||
language,
|
||||
pagination
|
||||
@@ -323,19 +348,32 @@ defmodule BDS.Generation.Outputs do
|
||||
Enum.flat_map(additional_languages, fn localized_language ->
|
||||
localized_prefix = route_language(plan.language, localized_language)
|
||||
localized_source_posts = Map.get(localized_posts_by_language, localized_language, [])
|
||||
localized_posts = build_list_posts(plan.base_url, localized_source_posts, localized_prefix)
|
||||
|
||||
localized_posts =
|
||||
build_list_posts(plan.base_url, localized_source_posts, localized_prefix)
|
||||
|
||||
build_root_outputs(plan, localized_language, localized_posts) ++
|
||||
[
|
||||
{Path.join(localized_language, "404.html"), render_not_found_output(plan, localized_language)},
|
||||
{Path.join(localized_language, "feed.xml"), render_feed(plan, localized_language, localized_source_posts)},
|
||||
{Path.join(localized_language, "atom.xml"), render_atom(plan, localized_language, localized_source_posts)}
|
||||
{Path.join(localized_language, "404.html"),
|
||||
render_not_found_output(plan, localized_language)},
|
||||
{Path.join(localized_language, "feed.xml"),
|
||||
render_feed(plan, localized_language, localized_source_posts)},
|
||||
{Path.join(localized_language, "atom.xml"),
|
||||
render_atom(plan, localized_language, localized_source_posts)}
|
||||
]
|
||||
end)
|
||||
end
|
||||
|
||||
@spec build_page_outputs(String.t(), String.t(), [map()], map(), map()) :: [{String.t(), iodata()}]
|
||||
def build_page_outputs(project_id, main_language, published_posts, translations_by_post_language, localized_posts_by_language) do
|
||||
@spec build_page_outputs(String.t(), String.t(), [map()], map(), map()) :: [
|
||||
{String.t(), iodata()}
|
||||
]
|
||||
def build_page_outputs(
|
||||
project_id,
|
||||
main_language,
|
||||
published_posts,
|
||||
translations_by_post_language,
|
||||
localized_posts_by_language
|
||||
) do
|
||||
page_outputs =
|
||||
published_posts
|
||||
|> Enum.filter(&("page" in (&1.categories || [])))
|
||||
@@ -355,7 +393,14 @@ defmodule BDS.Generation.Outputs do
|
||||
language: canonical_variant.language,
|
||||
excerpt: canonical_variant.excerpt
|
||||
},
|
||||
fn -> render_post_page(canonical_variant.title, body, post.slug, canonical_variant.language) end
|
||||
fn ->
|
||||
render_post_page(
|
||||
canonical_variant.title,
|
||||
body,
|
||||
post.slug,
|
||||
canonical_variant.language
|
||||
)
|
||||
end
|
||||
)}
|
||||
end)
|
||||
|
||||
@@ -404,13 +449,22 @@ defmodule BDS.Generation.Outputs do
|
||||
plan.project_name,
|
||||
page_posts,
|
||||
%{kind: "core"},
|
||||
pagination_for_page(page_number, total_pages, length(posts), plan.max_posts_per_page, route_language, []),
|
||||
pagination_for_page(
|
||||
page_number,
|
||||
total_pages,
|
||||
length(posts),
|
||||
plan.max_posts_per_page,
|
||||
route_language,
|
||||
[]
|
||||
),
|
||||
fn -> render_home(plan, language) end
|
||||
)}
|
||||
end)
|
||||
end
|
||||
|
||||
@spec build_paginated_archive_outputs(map(), [String.t()], [String.t()], [map()], (... -> iodata())) :: [{String.t(), iodata()}]
|
||||
@spec build_paginated_archive_outputs(map(), [String.t()], [String.t()], [map()], (... ->
|
||||
iodata())) ::
|
||||
[{String.t(), iodata()}]
|
||||
def build_paginated_archive_outputs(plan, languages, segments, posts, render_fun) do
|
||||
total_pages = page_count(length(posts), plan.max_posts_per_page)
|
||||
|
||||
@@ -425,13 +479,22 @@ defmodule BDS.Generation.Outputs do
|
||||
render_fun.(
|
||||
page_posts,
|
||||
language,
|
||||
pagination_for_page(page_number, total_pages, length(posts), plan.max_posts_per_page, route_language, segments)
|
||||
pagination_for_page(
|
||||
page_number,
|
||||
total_pages,
|
||||
length(posts),
|
||||
plan.max_posts_per_page,
|
||||
route_language,
|
||||
segments
|
||||
)
|
||||
)}
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
@spec build_single_outputs(String.t(), String.t(), [map()], map(), map()) :: [{String.t(), iodata()}]
|
||||
@spec build_single_outputs(String.t(), String.t(), [map()], map(), map()) :: [
|
||||
{String.t(), iodata()}
|
||||
]
|
||||
def build_single_outputs(
|
||||
project_id,
|
||||
main_language,
|
||||
@@ -457,7 +520,12 @@ defmodule BDS.Generation.Outputs do
|
||||
excerpt: canonical_variant.excerpt
|
||||
},
|
||||
fn ->
|
||||
render_post_page(canonical_variant.title, body, post.slug, canonical_variant.language)
|
||||
render_post_page(
|
||||
canonical_variant.title,
|
||||
body,
|
||||
post.slug,
|
||||
canonical_variant.language
|
||||
)
|
||||
end
|
||||
)}
|
||||
end)
|
||||
|
||||
@@ -19,10 +19,13 @@ defmodule BDS.Generation.Pagefind do
|
||||
|> Enum.flat_map(fn language ->
|
||||
route_language = route_language(plan.language, language)
|
||||
pages = pages_for_language(html_outputs, route_language)
|
||||
prefix = if route_language in [nil, ""], do: ["pagefind"], else: [route_language, "pagefind"]
|
||||
|
||||
prefix =
|
||||
if route_language in [nil, ""], do: ["pagefind"], else: [route_language, "pagefind"]
|
||||
|
||||
[
|
||||
{Path.join(prefix ++ ["index.json"]), Jason.encode!(%{"language" => language, "pages" => pages})},
|
||||
{Path.join(prefix ++ ["index.json"]),
|
||||
Jason.encode!(%{"language" => language, "pages" => pages})},
|
||||
{Path.join(prefix ++ ["pagefind-ui.js"]), ui_js(language)},
|
||||
{Path.join(prefix ++ ["pagefind-ui.css"]), ui_css()}
|
||||
]
|
||||
|
||||
@@ -49,18 +49,37 @@ defmodule BDS.Generation.Paths do
|
||||
def root_output_path(nil, 1), do: "index.html"
|
||||
def root_output_path("", 1), do: "index.html"
|
||||
def root_output_path(route_language, 1), do: Path.join(route_language, "index.html")
|
||||
def root_output_path(nil, page_number), do: Path.join(["page", Integer.to_string(page_number), "index.html"])
|
||||
|
||||
def root_output_path(nil, page_number),
|
||||
do: Path.join(["page", Integer.to_string(page_number), "index.html"])
|
||||
|
||||
def root_output_path("", page_number), do: root_output_path(nil, page_number)
|
||||
def root_output_path(route_language, page_number), do: Path.join([route_language, "page", Integer.to_string(page_number), "index.html"])
|
||||
|
||||
def root_output_path(route_language, page_number),
|
||||
do: Path.join([route_language, "page", Integer.to_string(page_number), "index.html"])
|
||||
|
||||
@spec page_output_path(String.t(), language()) :: String.t()
|
||||
def page_output_path(slug, nil), do: Path.join([slug, "index.html"])
|
||||
def page_output_path(slug, ""), do: page_output_path(slug, nil)
|
||||
def page_output_path(slug, language), do: Path.join([language, slug, "index.html"])
|
||||
|
||||
@spec pagination_for_page(pos_integer(), pos_integer(), non_neg_integer(), pos_integer(), language(), [String.t()]) ::
|
||||
@spec pagination_for_page(
|
||||
pos_integer(),
|
||||
pos_integer(),
|
||||
non_neg_integer(),
|
||||
pos_integer(),
|
||||
language(),
|
||||
[String.t()]
|
||||
) ::
|
||||
map()
|
||||
def pagination_for_page(page_number, total_pages, total_items, items_per_page, route_language, segments) do
|
||||
def pagination_for_page(
|
||||
page_number,
|
||||
total_pages,
|
||||
total_items,
|
||||
items_per_page,
|
||||
route_language,
|
||||
segments
|
||||
) do
|
||||
%{
|
||||
current_page: page_number,
|
||||
total_pages: total_pages,
|
||||
@@ -75,8 +94,12 @@ defmodule BDS.Generation.Paths do
|
||||
|
||||
@spec archive_or_root_href(language(), [String.t()], integer()) :: String.t()
|
||||
def archive_or_root_href(_route_language, _segments, page_number) when page_number < 1, do: ""
|
||||
def archive_or_root_href(route_language, [], page_number), do: root_page_href(route_language, page_number)
|
||||
def archive_or_root_href(route_language, segments, page_number), do: archive_href(route_language, segments, page_number)
|
||||
|
||||
def archive_or_root_href(route_language, [], page_number),
|
||||
do: root_page_href(route_language, page_number)
|
||||
|
||||
def archive_or_root_href(route_language, segments, page_number),
|
||||
do: archive_href(route_language, segments, page_number)
|
||||
|
||||
@spec root_page_href(language(), integer()) :: String.t()
|
||||
def root_page_href(route_language, page_number) when page_number <= 1 do
|
||||
@@ -147,7 +170,9 @@ defmodule BDS.Generation.Paths do
|
||||
|
||||
@spec archive_route_segment(any()) :: String.t()
|
||||
def archive_route_segment(nil), do: ""
|
||||
def archive_route_segment(value), do: value |> to_string() |> URI.encode(&URI.char_unreserved?/1)
|
||||
|
||||
def archive_route_segment(value),
|
||||
do: value |> to_string() |> URI.encode(&URI.char_unreserved?/1)
|
||||
|
||||
@spec normalize_base_url(String.t() | nil) :: String.t() | nil
|
||||
def normalize_base_url(nil), do: nil
|
||||
|
||||
@@ -44,7 +44,8 @@ defmodule BDS.Generation.Renderers do
|
||||
end
|
||||
|
||||
@doc "Render an archive page (category, tag, year) with pagination."
|
||||
@spec render_archive_page(map(), String.t(), [map()], String.t() | nil, String.t(), map()) :: String.t()
|
||||
@spec render_archive_page(map(), String.t(), [map()], String.t() | nil, String.t(), map()) ::
|
||||
String.t()
|
||||
def render_archive_page(plan, title, posts, language, kind, pagination) do
|
||||
fallback = fn ->
|
||||
items =
|
||||
@@ -130,7 +131,15 @@ defmodule BDS.Generation.Renderers do
|
||||
end
|
||||
|
||||
@doc "Render a list/archive page through the project template, falling back to inline."
|
||||
@spec render_list_output(map(), String.t() | nil, String.t(), [map()], map(), map(), (-> String.t())) ::
|
||||
@spec render_list_output(
|
||||
map(),
|
||||
String.t() | nil,
|
||||
String.t(),
|
||||
[map()],
|
||||
map(),
|
||||
map(),
|
||||
(-> String.t())
|
||||
) ::
|
||||
String.t()
|
||||
def render_list_output(
|
||||
%{project_id: project_id, language: main_language},
|
||||
|
||||
@@ -34,17 +34,20 @@ defmodule BDS.Generation.Sitemap do
|
||||
build_hreflang_links(plan.base_url, "/", plan.language, all_languages)
|
||||
)
|
||||
] ++
|
||||
Enum.map(Paths.root_pagination_pages(length(published_list_posts), plan.max_posts_per_page), fn page_number ->
|
||||
page_path = "/page/#{page_number}"
|
||||
Enum.map(
|
||||
Paths.root_pagination_pages(length(published_list_posts), plan.max_posts_per_page),
|
||||
fn page_number ->
|
||||
page_path = "/page/#{page_number}"
|
||||
|
||||
url_entry(
|
||||
Paths.url_for_path(plan.base_url, page_path),
|
||||
latest_post_updated_at,
|
||||
"daily",
|
||||
"0.9",
|
||||
build_hreflang_links(plan.base_url, page_path, plan.language, all_languages)
|
||||
)
|
||||
end) ++
|
||||
url_entry(
|
||||
Paths.url_for_path(plan.base_url, page_path),
|
||||
latest_post_updated_at,
|
||||
"daily",
|
||||
"0.9",
|
||||
build_hreflang_links(plan.base_url, page_path, plan.language, all_languages)
|
||||
)
|
||||
end
|
||||
) ++
|
||||
Enum.map(translatable_posts, fn post ->
|
||||
post_path = Paths.relative_path_to_url_path(Paths.post_output_path(post))
|
||||
|
||||
@@ -100,28 +103,34 @@ defmodule BDS.Generation.Sitemap do
|
||||
build_hreflang_links(plan.base_url, year_path, plan.language, all_languages)
|
||||
)
|
||||
end) ++
|
||||
Enum.map(Enum.sort_by(post_index.posts_by_year_month, &elem(&1, 0), :desc), fn {year_month, _posts} ->
|
||||
month_path = "/#{year_month}"
|
||||
Enum.map(
|
||||
Enum.sort_by(post_index.posts_by_year_month, &elem(&1, 0), :desc),
|
||||
fn {year_month, _posts} ->
|
||||
month_path = "/#{year_month}"
|
||||
|
||||
url_entry(
|
||||
Paths.url_for_path(plan.base_url, month_path),
|
||||
latest_post_updated_at,
|
||||
"monthly",
|
||||
"0.5",
|
||||
build_hreflang_links(plan.base_url, month_path, plan.language, all_languages)
|
||||
)
|
||||
end) ++
|
||||
Enum.map(Enum.sort_by(post_index.posts_by_year_month_day, &elem(&1, 0), :desc), fn {year_month_day, _posts} ->
|
||||
day_path = "/#{year_month_day}"
|
||||
url_entry(
|
||||
Paths.url_for_path(plan.base_url, month_path),
|
||||
latest_post_updated_at,
|
||||
"monthly",
|
||||
"0.5",
|
||||
build_hreflang_links(plan.base_url, month_path, plan.language, all_languages)
|
||||
)
|
||||
end
|
||||
) ++
|
||||
Enum.map(
|
||||
Enum.sort_by(post_index.posts_by_year_month_day, &elem(&1, 0), :desc),
|
||||
fn {year_month_day, _posts} ->
|
||||
day_path = "/#{year_month_day}"
|
||||
|
||||
url_entry(
|
||||
Paths.url_for_path(plan.base_url, day_path),
|
||||
latest_post_updated_at,
|
||||
"monthly",
|
||||
"0.4",
|
||||
build_hreflang_links(plan.base_url, day_path, plan.language, all_languages)
|
||||
)
|
||||
end) ++
|
||||
url_entry(
|
||||
Paths.url_for_path(plan.base_url, day_path),
|
||||
latest_post_updated_at,
|
||||
"monthly",
|
||||
"0.4",
|
||||
build_hreflang_links(plan.base_url, day_path, plan.language, all_languages)
|
||||
)
|
||||
end
|
||||
) ++
|
||||
Enum.map(Enum.sort_by(post_index.posts_by_category, &elem(&1, 0)), fn {category, _posts} ->
|
||||
category_path = "/category/#{Paths.archive_route_segment(category)}"
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ defmodule BDS.Generation.Validation do
|
||||
relative_path_to_url_path: 1,
|
||||
url_path_to_relative_index_path: 1
|
||||
]
|
||||
|
||||
import BDS.Generation.Progress, only: [report_validation_compare_progress: 3]
|
||||
import BDS.Generation.Sitemap, only: [extract_locs: 1, loc_to_project_path: 2]
|
||||
|
||||
@@ -20,7 +21,11 @@ defmodule BDS.Generation.Validation do
|
||||
end
|
||||
|
||||
@spec build_post_timestamp_checks(String.t(), [map()], map()) :: [map()]
|
||||
def build_post_timestamp_checks(project_data_dir, published_route_posts, generated_file_updated_at) do
|
||||
def build_post_timestamp_checks(
|
||||
project_data_dir,
|
||||
published_route_posts,
|
||||
generated_file_updated_at
|
||||
) do
|
||||
Enum.map(published_route_posts, fn post ->
|
||||
relative_path = BDS.Generation.Paths.post_output_path(post)
|
||||
|
||||
@@ -69,13 +74,19 @@ defmodule BDS.Generation.Validation do
|
||||
|> Enum.map(&loc_to_project_path(&1, params.base_url))
|
||||
|> Enum.reduce(MapSet.new(), &MapSet.put(&2, normalize_url_path(&1)))
|
||||
|> then(fn expected_paths ->
|
||||
Enum.reduce(Map.get(params, :additional_expected_paths, []), expected_paths, fn path, acc ->
|
||||
Enum.reduce(Map.get(params, :additional_expected_paths, []), expected_paths, fn path,
|
||||
acc ->
|
||||
MapSet.put(acc, normalize_url_path(path))
|
||||
end)
|
||||
end)
|
||||
|
||||
{existing_html_path_set, zero_byte_html_path_set} =
|
||||
collect_html_index_paths(index_paths, params.html_dir, params.on_progress, total_compare_steps)
|
||||
collect_html_index_paths(
|
||||
index_paths,
|
||||
params.html_dir,
|
||||
params.on_progress,
|
||||
total_compare_steps
|
||||
)
|
||||
|
||||
missing_url_paths =
|
||||
expected_path_set
|
||||
@@ -119,11 +130,14 @@ defmodule BDS.Generation.Validation do
|
||||
acc
|
||||
|
||||
true ->
|
||||
html_path = Path.join(params.html_dir, url_path_to_relative_index_path(normalized_url_path))
|
||||
html_path =
|
||||
Path.join(params.html_dir, url_path_to_relative_index_path(normalized_url_path))
|
||||
|
||||
case {File.stat(html_path, time: :posix), File.stat(check.post_file_path, time: :posix)} do
|
||||
case {File.stat(html_path, time: :posix),
|
||||
File.stat(check.post_file_path, time: :posix)} do
|
||||
{{:ok, html_stat}, {:ok, post_stat}} ->
|
||||
effective_generated_at_ms = max(mtime_ms(html_stat), check.generated_updated_at_ms || 0)
|
||||
effective_generated_at_ms =
|
||||
max(mtime_ms(html_stat), check.generated_updated_at_ms || 0)
|
||||
|
||||
if mtime_ms(post_stat) > effective_generated_at_ms do
|
||||
MapSet.put(acc, normalized_url_path)
|
||||
@@ -233,7 +247,18 @@ defmodule BDS.Generation.Validation do
|
||||
nil ->
|
||||
case Regex.run(~r|^/(\d{4})/(\d{2})/(\d{2})/([^/]+)$|, path) do
|
||||
[_, year, month, day, slug] ->
|
||||
update_in(plan.requested_post_routes, &[ %{year: String.to_integer(year), month: String.to_integer(month), day: String.to_integer(day), slug: slug} | &1 ])
|
||||
update_in(
|
||||
plan.requested_post_routes,
|
||||
&[
|
||||
%{
|
||||
year: String.to_integer(year),
|
||||
month: String.to_integer(month),
|
||||
day: String.to_integer(day),
|
||||
slug: slug
|
||||
}
|
||||
| &1
|
||||
]
|
||||
)
|
||||
|
||||
nil ->
|
||||
case Regex.run(~r|^/(\d{4})/(\d{2})(?:/page/\d+)?$|, path) do
|
||||
@@ -281,29 +306,43 @@ defmodule BDS.Generation.Validation do
|
||||
end)
|
||||
|
||||
enriched =
|
||||
Enum.reduce(initial_plan.requested_post_routes, %{initial_plan | requested_post_routes: targeted_post_routes}, fn route, acc ->
|
||||
case Enum.find(published_posts, &post_matches_route?(&1, route)) do
|
||||
nil ->
|
||||
acc
|
||||
|> update_in([:requested_years], &MapSet.put(&1, route.year))
|
||||
|> update_in([:requested_year_months], &MapSet.put(&1, route_month_key(route.year, route.month)))
|
||||
|> Map.put(:request_root_routes, true)
|
||||
Enum.reduce(
|
||||
initial_plan.requested_post_routes,
|
||||
%{initial_plan | requested_post_routes: targeted_post_routes},
|
||||
fn route, acc ->
|
||||
case Enum.find(published_posts, &post_matches_route?(&1, route)) do
|
||||
nil ->
|
||||
acc
|
||||
|> update_in([:requested_years], &MapSet.put(&1, route.year))
|
||||
|> update_in(
|
||||
[:requested_year_months],
|
||||
&MapSet.put(&1, route_month_key(route.year, route.month))
|
||||
)
|
||||
|> Map.put(:request_root_routes, true)
|
||||
|
||||
post ->
|
||||
{year, month, _day} = local_date_parts!(post.created_at)
|
||||
post ->
|
||||
{year, month, _day} = local_date_parts!(post.created_at)
|
||||
|
||||
acc
|
||||
|> update_in([:requested_category_slugs], fn set ->
|
||||
Enum.reduce(post.categories || [], set, &MapSet.put(&2, archive_route_segment(&1)))
|
||||
end)
|
||||
|> update_in([:requested_tag_slugs], fn set ->
|
||||
Enum.reduce(post.tags || [], set, &MapSet.put(&2, archive_route_segment(&1)))
|
||||
end)
|
||||
|> update_in([:requested_years], &MapSet.put(&1, year))
|
||||
|> update_in([:requested_year_months], &MapSet.put(&1, route_month_key(year, month)))
|
||||
|> Map.put(:request_root_routes, true)
|
||||
acc
|
||||
|> update_in([:requested_category_slugs], fn set ->
|
||||
Enum.reduce(
|
||||
post.categories || [],
|
||||
set,
|
||||
&MapSet.put(&2, archive_route_segment(&1))
|
||||
)
|
||||
end)
|
||||
|> update_in([:requested_tag_slugs], fn set ->
|
||||
Enum.reduce(post.tags || [], set, &MapSet.put(&2, archive_route_segment(&1)))
|
||||
end)
|
||||
|> update_in([:requested_years], &MapSet.put(&1, year))
|
||||
|> update_in(
|
||||
[:requested_year_months],
|
||||
&MapSet.put(&1, route_month_key(year, month))
|
||||
)
|
||||
|> Map.put(:request_root_routes, true)
|
||||
end
|
||||
end
|
||||
end)
|
||||
)
|
||||
|
||||
language_plans =
|
||||
initial_plan.language_plans
|
||||
@@ -314,8 +353,10 @@ defmodule BDS.Generation.Validation do
|
||||
|
||||
%{
|
||||
enriched
|
||||
| requested_category_slugs: MapSet.intersection(enriched.requested_category_slugs, available_category_slugs),
|
||||
requested_tag_slugs: MapSet.intersection(enriched.requested_tag_slugs, available_tag_slugs),
|
||||
| requested_category_slugs:
|
||||
MapSet.intersection(enriched.requested_category_slugs, available_category_slugs),
|
||||
requested_tag_slugs:
|
||||
MapSet.intersection(enriched.requested_tag_slugs, available_tag_slugs),
|
||||
language_plans: language_plans
|
||||
}
|
||||
end
|
||||
@@ -351,13 +392,15 @@ defmodule BDS.Generation.Validation do
|
||||
{nil, path}
|
||||
end
|
||||
|
||||
_other -> {nil, path}
|
||||
_other ->
|
||||
{nil, path}
|
||||
end
|
||||
end
|
||||
|
||||
@spec targeted_output?(String.t(), map(), String.t() | nil, [String.t()]) :: boolean()
|
||||
def targeted_output?(relative_path, targeted_plan, main_language, additional_languages) do
|
||||
{language, stripped_path} = extract_relative_output_language(relative_path, additional_languages)
|
||||
{language, stripped_path} =
|
||||
extract_relative_output_language(relative_path, additional_languages)
|
||||
|
||||
plan =
|
||||
case language do
|
||||
@@ -384,7 +427,11 @@ defmodule BDS.Generation.Validation do
|
||||
end
|
||||
end
|
||||
|
||||
defp targeted_output_for_plan?(_relative_path, %{requires_fallback_section_render: true}, _main?), do: true
|
||||
defp targeted_output_for_plan?(
|
||||
_relative_path,
|
||||
%{requires_fallback_section_render: true},
|
||||
_main?
|
||||
), do: true
|
||||
|
||||
defp targeted_output_for_plan?(relative_path, plan, _main?) do
|
||||
cond do
|
||||
@@ -400,8 +447,18 @@ defmodule BDS.Generation.Validation do
|
||||
MapSet.member?(plan.requested_tag_slugs, slug)
|
||||
|
||||
Regex.match?(~r|^(\d{4})/(\d{2})/(\d{2})/([^/]+)/index\.html$|, relative_path) ->
|
||||
[_, year, month, day, slug] = Regex.run(~r|^(\d{4})/(\d{2})/(\d{2})/([^/]+)/index\.html$|, relative_path)
|
||||
MapSet.member?(plan.requested_post_routes, route_key(String.to_integer(year), String.to_integer(month), String.to_integer(day), slug))
|
||||
[_, year, month, day, slug] =
|
||||
Regex.run(~r|^(\d{4})/(\d{2})/(\d{2})/([^/]+)/index\.html$|, relative_path)
|
||||
|
||||
MapSet.member?(
|
||||
plan.requested_post_routes,
|
||||
route_key(
|
||||
String.to_integer(year),
|
||||
String.to_integer(month),
|
||||
String.to_integer(day),
|
||||
slug
|
||||
)
|
||||
)
|
||||
|
||||
Regex.match?(~r|^(\d{4})/(\d{2})/index\.html$|, relative_path) ->
|
||||
[_, year, month] = Regex.run(~r|^(\d{4})/(\d{2})/index\.html$|, relative_path)
|
||||
|
||||
Reference in New Issue
Block a user