Consolidate generation route builders

This commit is contained in:
2026-06-21 13:06:50 +02:00
parent f8f6a242d6
commit b3d69f0291
2 changed files with 65 additions and 76 deletions

View File

@@ -21,6 +21,7 @@ after each item.
- [x] `rendering/metadata.ex` - [x] `rendering/metadata.ex`
- [x] `rendering/links_and_languages.ex` - [x] `rendering/links_and_languages.ex`
- [x] `generation/validation.ex` - [x] `generation/validation.ex`
- [x] `generation/outputs.ex`
--- ---

View File

@@ -51,114 +51,102 @@ defmodule BDS.Generation.Outputs do
@spec core_route_paths(map(), [map()], String.t() | nil) :: [String.t()] @spec core_route_paths(map(), [map()], String.t() | nil) :: [String.t()]
def core_route_paths(plan, published_list_posts, route_language) do def core_route_paths(plan, published_list_posts, route_language) do
if :core in plan.sections do section_route_paths(plan, :core, fn ->
root_route_paths(route_language, length(published_list_posts), plan.max_posts_per_page) root_route_paths(route_language, length(published_list_posts), plan.max_posts_per_page)
else end)
[]
end
end end
@spec page_route_paths(map(), [map()], String.t() | nil) :: [String.t()] @spec page_route_paths(map(), [map()], String.t() | nil) :: [String.t()]
def page_route_paths(plan, route_posts, route_language) do def page_route_paths(plan, route_posts, route_language) do
if :core in plan.sections do section_route_paths(plan, :core, fn ->
route_posts route_posts
|> Enum.filter(&("page" in (&1.categories || []))) |> Enum.filter(&("page" in (&1.categories || [])))
|> Enum.map(&page_output_path(&1.slug, route_language)) |> Enum.map(&page_output_path(&1.slug, route_language))
else end)
[]
end
end end
@spec single_route_paths(map(), [map()], String.t() | nil) :: [String.t()] @spec single_route_paths(map(), [map()], String.t() | nil) :: [String.t()]
def single_route_paths(plan, route_posts, route_language) do def single_route_paths(plan, route_posts, route_language) do
if :single in plan.sections do section_route_paths(plan, :single, fn ->
Enum.map(route_posts, &route_post_output_path(&1, route_language)) Enum.map(route_posts, &route_post_output_path(&1, route_language))
else end)
[]
end
end end
@spec category_route_paths(map(), map(), String.t() | nil) :: [String.t()] @spec category_route_paths(map(), map(), String.t() | nil) :: [String.t()]
def category_route_paths(plan, posts_by_category, route_language) do def category_route_paths(plan, posts_by_category, route_language) do
if :category in plan.sections do section_route_paths(plan, :category, fn ->
Enum.flat_map(posts_by_category, fn {category, posts} -> archive_collection_route_paths(
post_count = length(posts) posts_by_category,
route_language,
paginated_archive_paths( plan.max_posts_per_page,
route_language, fn category -> ["category", archive_route_segment(category)] end
["category", archive_route_segment(category)], )
post_count, end)
plan.max_posts_per_page
)
end)
else
[]
end
end end
@spec tag_route_paths(map(), map(), String.t() | nil) :: [String.t()] @spec tag_route_paths(map(), map(), String.t() | nil) :: [String.t()]
def tag_route_paths(plan, posts_by_tag, route_language) do def tag_route_paths(plan, posts_by_tag, route_language) do
if :tag in plan.sections do section_route_paths(plan, :tag, fn ->
Enum.flat_map(posts_by_tag, fn {tag, posts} -> archive_collection_route_paths(
post_count = length(posts) posts_by_tag,
route_language,
paginated_archive_paths( plan.max_posts_per_page,
route_language, fn tag -> ["tag", archive_route_segment(tag)] end
["tag", archive_route_segment(tag)], )
post_count, end)
plan.max_posts_per_page
)
end)
else
[]
end
end end
@spec date_route_paths(map(), map(), String.t() | nil) :: [String.t()] @spec date_route_paths(map(), map(), String.t() | nil) :: [String.t()]
def date_route_paths(plan, post_index, route_language) do def date_route_paths(plan, post_index, route_language) do
if :date in plan.sections do section_route_paths(plan, :date, fn ->
year_paths = year_paths =
Enum.flat_map(post_index.posts_by_year, fn {year, posts} -> archive_collection_route_paths(
post_count = length(posts) post_index.posts_by_year,
route_language,
paginated_archive_paths( plan.max_posts_per_page,
route_language, fn year -> [Integer.to_string(year)] end
[Integer.to_string(year)], )
post_count,
plan.max_posts_per_page
)
end)
month_paths = month_paths =
Enum.flat_map(post_index.posts_by_year_month, fn {year_month, posts} -> archive_collection_route_paths(
[year, month] = String.split(year_month, "/", parts: 2) post_index.posts_by_year_month,
post_count = length(posts) route_language,
plan.max_posts_per_page,
paginated_archive_paths( fn year_month -> String.split(year_month, "/", parts: 2) end
route_language, )
[year, month],
post_count,
plan.max_posts_per_page
)
end)
day_paths = day_paths =
Enum.flat_map(post_index.posts_by_year_month_day, fn {year_month_day, posts} -> archive_collection_route_paths(
[year, month, day] = String.split(year_month_day, "/", parts: 3) post_index.posts_by_year_month_day,
post_count = length(posts) route_language,
plan.max_posts_per_page,
paginated_archive_paths( fn year_month_day -> String.split(year_month_day, "/", parts: 3) end
route_language, )
[year, month, day],
post_count,
plan.max_posts_per_page
)
end)
year_paths ++ month_paths ++ day_paths year_paths ++ month_paths ++ day_paths
else end)
[] end
end
defp section_route_paths(plan, section, builder) do
if section in plan.sections, do: builder.(), else: []
end
defp archive_collection_route_paths(
collections,
route_language,
max_posts_per_page,
segment_builder
) do
Enum.flat_map(collections, fn {key, posts} ->
post_count = length(posts)
paginated_archive_paths(
route_language,
segment_builder.(key),
post_count,
max_posts_per_page
)
end)
end end
@spec build_archive_outputs(map(), map(), map()) :: [{String.t(), iodata()}] @spec build_archive_outputs(map(), map(), map()) :: [{String.t(), iodata()}]