fix: issue #3 slow rendering
This commit is contained in:
@@ -84,19 +84,21 @@ defmodule BDS.Rendering.LinksAndLanguages do
|
||||
end
|
||||
|
||||
case Repo.get(Post, linked_post_id) do
|
||||
nil ->
|
||||
nil
|
||||
|
||||
linked_post ->
|
||||
%{
|
||||
href: post_path(linked_post, nil),
|
||||
title: linked_post.title,
|
||||
display_slug: linked_post.slug,
|
||||
language: normalize_language(linked_post.language, main_language)
|
||||
}
|
||||
nil -> nil
|
||||
linked_post -> link_context_for_post(linked_post, main_language)
|
||||
end
|
||||
end
|
||||
|
||||
@spec link_context_for_post(map(), String.t()) :: map()
|
||||
def link_context_for_post(linked_post, main_language) do
|
||||
%{
|
||||
href: post_path(linked_post, nil),
|
||||
title: linked_post.title,
|
||||
display_slug: linked_post.slug,
|
||||
language: normalize_language(linked_post.language, main_language)
|
||||
}
|
||||
end
|
||||
|
||||
@spec language_prefix(String.t() | nil, String.t()) :: String.t()
|
||||
def language_prefix(language, main_language) when language == main_language, do: ""
|
||||
def language_prefix(nil, _main_language), do: ""
|
||||
|
||||
@@ -7,43 +7,25 @@ defmodule BDS.Rendering.ListArchive do
|
||||
alias BDS.Rendering.LinksAndLanguages
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Rendering.PostRendering
|
||||
alias BDS.Rendering.TemplateSelection
|
||||
alias BDS.Rendering.RenderContext
|
||||
use Gettext, backend: BDS.Gettext
|
||||
|
||||
@spec list_assigns(String.t(), map()) :: map()
|
||||
def list_assigns(project_id, assigns) do
|
||||
metadata = RenderMetadata.project_metadata(project_id)
|
||||
template_context = TemplateSelection.template_render_context(project_id)
|
||||
@spec list_assigns(RenderContext.t() | String.t(), map()) :: map()
|
||||
def list_assigns(project_id, assigns) when is_binary(project_id) do
|
||||
list_assigns(RenderContext.build(project_id), assigns)
|
||||
end
|
||||
|
||||
def list_assigns(%RenderContext{} = ctx, assigns) do
|
||||
metadata = ctx.metadata
|
||||
|
||||
language = MapUtils.attr(assigns, :language, metadata.main_language || "en")
|
||||
|
||||
main_language = metadata.main_language || language
|
||||
archive_context = MapUtils.attr(assigns, :archive_context, %{})
|
||||
|
||||
canonical_post_paths =
|
||||
LinksAndLanguages.canonical_post_path_by_slug(project_id, main_language)
|
||||
|
||||
canonical_media_paths = LinksAndLanguages.canonical_media_path_by_source_path(project_id)
|
||||
|
||||
input_posts = MapUtils.attr(assigns, :posts, [])
|
||||
|
||||
post_ids =
|
||||
input_posts
|
||||
|> Enum.map(&MapUtils.attr(&1, :id))
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> Enum.uniq()
|
||||
|
||||
post_records = PostRendering.load_post_records_batch(post_ids)
|
||||
|
||||
posts =
|
||||
normalize_list_posts(
|
||||
input_posts,
|
||||
post_records,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context
|
||||
)
|
||||
posts = normalize_list_posts(ctx, input_posts, language)
|
||||
|
||||
pagination =
|
||||
normalize_pagination(MapUtils.attr(assigns, :pagination), posts)
|
||||
@@ -73,7 +55,7 @@ defmodule BDS.Rendering.ListArchive do
|
||||
assign_override(assigns, :html_theme_attribute, "html_theme_attribute", nil),
|
||||
blog_languages: RenderMetadata.blog_languages(metadata, language),
|
||||
alternate_links: [],
|
||||
menu_items: RenderMetadata.menu_items(project_id),
|
||||
menu_items: ctx.menu_items,
|
||||
calendar_initial_year: calendar_initial_year_from_posts(posts),
|
||||
calendar_initial_month: calendar_initial_month_from_posts(posts),
|
||||
archive_context: normalized_archive_context,
|
||||
@@ -92,8 +74,8 @@ defmodule BDS.Rendering.ListArchive do
|
||||
total_pages: pagination.total_pages,
|
||||
total_items: pagination.total_items,
|
||||
items_per_page: pagination.items_per_page,
|
||||
canonical_post_path_by_slug: canonical_post_paths,
|
||||
canonical_media_path_by_source_path: canonical_media_paths,
|
||||
canonical_post_path_by_slug: ctx.canonical_post_paths,
|
||||
canonical_media_path_by_source_path: ctx.canonical_media_paths,
|
||||
post_data_json_by_id:
|
||||
Enum.into(posts, %{}, fn post -> {post.id, PostRendering.post_data_json_value(post)} end),
|
||||
day_blocks: day_blocks,
|
||||
@@ -103,9 +85,13 @@ defmodule BDS.Rendering.ListArchive do
|
||||
}
|
||||
end
|
||||
|
||||
@spec not_found_assigns(String.t(), map()) :: map()
|
||||
def not_found_assigns(project_id, assigns) do
|
||||
metadata = RenderMetadata.project_metadata(project_id)
|
||||
@spec not_found_assigns(RenderContext.t() | String.t(), map()) :: map()
|
||||
def not_found_assigns(project_id, assigns) when is_binary(project_id) do
|
||||
not_found_assigns(RenderContext.build(project_id), assigns)
|
||||
end
|
||||
|
||||
def not_found_assigns(%RenderContext{} = ctx, assigns) do
|
||||
metadata = ctx.metadata
|
||||
|
||||
language = MapUtils.attr(assigns, :language, metadata.main_language || "en")
|
||||
|
||||
@@ -129,7 +115,7 @@ defmodule BDS.Rendering.ListArchive do
|
||||
html_theme_attribute:
|
||||
assign_override(assigns, :html_theme_attribute, "html_theme_attribute", nil),
|
||||
blog_languages: RenderMetadata.blog_languages(metadata, language),
|
||||
menu_items: RenderMetadata.menu_items(project_id),
|
||||
menu_items: ctx.menu_items,
|
||||
alternate_links: [],
|
||||
not_found_message:
|
||||
assign_override(
|
||||
@@ -149,17 +135,10 @@ defmodule BDS.Rendering.ListArchive do
|
||||
}
|
||||
end
|
||||
|
||||
defp normalize_list_posts(
|
||||
posts,
|
||||
post_records,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context
|
||||
) do
|
||||
defp normalize_list_posts(ctx, posts, language) do
|
||||
Enum.map(posts, fn post ->
|
||||
post_id = MapUtils.attr(post, :id)
|
||||
post_record = Map.get(post_records, post_id)
|
||||
post_record = Map.get(ctx.record_by_id, post_id)
|
||||
|
||||
raw_content =
|
||||
Map.get(
|
||||
@@ -172,14 +151,7 @@ defmodule BDS.Rendering.ListArchive do
|
||||
id: MapUtils.attr(post, :id),
|
||||
slug: MapUtils.attr(post, :slug),
|
||||
title: MapUtils.attr(post, :title),
|
||||
content:
|
||||
PostRendering.render_post_content(
|
||||
raw_content,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context
|
||||
),
|
||||
content: PostRendering.cached_post_content(ctx, raw_content, language),
|
||||
raw_content: raw_content,
|
||||
excerpt: MapUtils.attr(post, :excerpt, record_value(post_record, :excerpt)),
|
||||
author: MapUtils.attr(post, :author, record_value(post_record, :author)),
|
||||
|
||||
@@ -103,6 +103,13 @@ defmodule BDS.Rendering.Metadata do
|
||||
order_by: [asc: translation.language]
|
||||
)
|
||||
|
||||
alternate_links_from(post, translations, main_language)
|
||||
end
|
||||
|
||||
@spec alternate_links_from(Post.t() | nil, [Translation.t()], String.t()) :: [map()]
|
||||
def alternate_links_from(nil, _translations, _main_language), do: []
|
||||
|
||||
def alternate_links_from(%Post{} = post, translations, main_language) do
|
||||
[
|
||||
%{
|
||||
href: LinksAndLanguages.post_path(post, nil),
|
||||
|
||||
@@ -7,49 +7,34 @@ defmodule BDS.Rendering.PostRendering do
|
||||
alias BDS.Rendering.Labels
|
||||
alias BDS.Rendering.LinksAndLanguages
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Rendering.TemplateSelection
|
||||
alias BDS.Rendering.RenderContext
|
||||
alias BDS.MapUtils
|
||||
alias BDS.Posts.Post
|
||||
alias BDS.Posts.PostMedia
|
||||
alias BDS.Posts.Translation
|
||||
alias BDS.Media.Media, as: MediaRecord
|
||||
alias BDS.Repo
|
||||
|
||||
@spec post_assigns(String.t(), map()) :: map()
|
||||
def post_assigns(project_id, assigns) do
|
||||
metadata = RenderMetadata.project_metadata(project_id)
|
||||
template_context = TemplateSelection.template_render_context(project_id)
|
||||
@spec post_assigns(RenderContext.t() | String.t(), map()) :: map()
|
||||
def post_assigns(project_id, assigns) when is_binary(project_id) do
|
||||
post_assigns(RenderContext.build(project_id), assigns)
|
||||
end
|
||||
|
||||
def post_assigns(%RenderContext{} = ctx, assigns) do
|
||||
metadata = ctx.metadata
|
||||
|
||||
language = MapUtils.attr(assigns, :language) || metadata.main_language || "en"
|
||||
|
||||
main_language = metadata.main_language || language
|
||||
post_record = Map.get(assigns, :_post_record) || load_post_record(assigns)
|
||||
canonical_post = canonical_post_record(post_record)
|
||||
post_record = Map.get(assigns, :_post_record) || load_post_record(ctx, assigns)
|
||||
canonical_post = canonical_post_record(ctx, post_record)
|
||||
post_id = canonical_post_id(post_record, assigns)
|
||||
post_categories = record_list(post_record, :categories)
|
||||
post_tags = record_list(post_record, :tags)
|
||||
|
||||
canonical_post_paths =
|
||||
LinksAndLanguages.canonical_post_path_by_slug(project_id, main_language)
|
||||
|
||||
canonical_media_paths = LinksAndLanguages.canonical_media_path_by_source_path(project_id)
|
||||
raw_content = MapUtils.attr(assigns, :content)
|
||||
rendered_content = cached_post_content(ctx, raw_content, language, post_id)
|
||||
|
||||
rendered_content =
|
||||
render_post_content(
|
||||
raw_content,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context,
|
||||
post_id
|
||||
)
|
||||
|
||||
incoming_links =
|
||||
LinksAndLanguages.link_contexts(project_id, post_id, :incoming, main_language)
|
||||
|
||||
outgoing_links =
|
||||
LinksAndLanguages.link_contexts(project_id, post_id, :outgoing, main_language)
|
||||
incoming_links = RenderContext.link_contexts(ctx, post_id, :incoming)
|
||||
outgoing_links = RenderContext.link_contexts(ctx, post_id, :outgoing)
|
||||
|
||||
post_assigns =
|
||||
assigns
|
||||
@@ -81,30 +66,59 @@ defmodule BDS.Rendering.PostRendering do
|
||||
html_theme_attribute:
|
||||
assign_override(assigns, :html_theme_attribute, "html_theme_attribute", nil),
|
||||
blog_languages: RenderMetadata.blog_languages(metadata, language),
|
||||
alternate_links: RenderMetadata.alternate_links(canonical_post, project_id, main_language),
|
||||
menu_items: RenderMetadata.menu_items(project_id),
|
||||
alternate_links:
|
||||
RenderMetadata.alternate_links_from(
|
||||
canonical_post,
|
||||
ctx_translations(ctx, canonical_post),
|
||||
main_language
|
||||
),
|
||||
menu_items: ctx.menu_items,
|
||||
calendar_initial_year: RenderMetadata.calendar_initial_year(post_record),
|
||||
calendar_initial_month: RenderMetadata.calendar_initial_month(post_record),
|
||||
post_categories: post_categories,
|
||||
post_tags: post_tags,
|
||||
tag_color_by_name: RenderMetadata.tag_color_by_name(project_id),
|
||||
tag_color_by_name: ctx.tag_color_by_name,
|
||||
backlinks: RenderMetadata.backlinks(incoming_links),
|
||||
canonical_post_path_by_slug: canonical_post_paths,
|
||||
canonical_media_path_by_source_path: canonical_media_paths,
|
||||
post_data_json_by_id: post_data_json(post_assigns, post_record),
|
||||
post: build_post_context(post_assigns, post_record, incoming_links, outgoing_links),
|
||||
canonical_post_path_by_slug: ctx.canonical_post_paths,
|
||||
canonical_media_path_by_source_path: ctx.canonical_media_paths,
|
||||
post_data_json_by_id:
|
||||
post_data_json(ctx, post_assigns, post_record, incoming_links, outgoing_links),
|
||||
post:
|
||||
build_post_context(ctx, post_assigns, post_record, incoming_links, outgoing_links),
|
||||
labels: Labels.for_language(language)
|
||||
}
|
||||
end
|
||||
|
||||
@spec load_post_record(map()) :: Post.t() | Translation.t() | nil
|
||||
def load_post_record(assigns) do
|
||||
case MapUtils.attr(assigns, :id) do
|
||||
nil -> nil
|
||||
post_id -> Repo.get(Post, post_id) || Repo.get(Translation, post_id)
|
||||
end
|
||||
@doc """
|
||||
Render post markdown through the context's canonical URL maps, memoizing by
|
||||
content, language, and post id so the same content is only rendered once per
|
||||
full-site render (posts appear on many list/archive pages).
|
||||
"""
|
||||
@spec cached_post_content(RenderContext.t(), term(), String.t(), term()) :: String.t()
|
||||
def cached_post_content(%RenderContext{} = ctx, content, language, post_id \\ nil) do
|
||||
raw = to_string(content || "")
|
||||
|
||||
RenderContext.memoize(
|
||||
ctx,
|
||||
{:rendered_markdown, :erlang.md5(raw), language, post_id},
|
||||
fn ->
|
||||
render_post_content(
|
||||
raw,
|
||||
ctx.canonical_post_paths,
|
||||
ctx.canonical_media_paths,
|
||||
language,
|
||||
ctx.template_context,
|
||||
post_id
|
||||
)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
defp ctx_translations(_ctx, nil), do: []
|
||||
|
||||
defp ctx_translations(%RenderContext{} = ctx, %Post{id: post_id}),
|
||||
do: Map.get(ctx.translations_by_post, post_id, [])
|
||||
|
||||
@spec load_post_records_batch([String.t()]) :: %{String.t() => Post.t() | Translation.t() | nil}
|
||||
def load_post_records_batch([]), do: %{}
|
||||
|
||||
@@ -135,38 +149,35 @@ defmodule BDS.Rendering.PostRendering do
|
||||
Map.merge(posts, Map.merge(translations, nil_entries))
|
||||
end
|
||||
|
||||
defp canonical_post_record(%Translation{translation_for: post_id}), do: Repo.get(Post, post_id)
|
||||
defp canonical_post_record(%Post{} = post), do: post
|
||||
defp canonical_post_record(_other), do: nil
|
||||
defp load_post_record(%RenderContext{} = ctx, assigns) do
|
||||
case MapUtils.attr(assigns, :id) do
|
||||
nil -> nil
|
||||
post_id -> Map.get(ctx.record_by_id, post_id)
|
||||
end
|
||||
end
|
||||
|
||||
defp canonical_post_record(%RenderContext{} = ctx, %Translation{translation_for: post_id}) do
|
||||
case Map.get(ctx.record_by_id, post_id) do
|
||||
%Post{} = post -> post
|
||||
_other -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp canonical_post_record(_ctx, %Post{} = post), do: post
|
||||
defp canonical_post_record(_ctx, _other), do: nil
|
||||
|
||||
defp canonical_post_id(%Translation{translation_for: post_id}, _assigns), do: post_id
|
||||
defp canonical_post_id(%Post{id: post_id}, _assigns), do: post_id
|
||||
defp canonical_post_id(_post_record, assigns), do: MapUtils.attr(assigns, :id)
|
||||
|
||||
defp post_data_json(assigns, post_record) do
|
||||
defp post_data_json(ctx, assigns, post_record, incoming_links, outgoing_links) do
|
||||
id = MapUtils.attr(assigns, :id)
|
||||
|
||||
if is_binary(id) do
|
||||
incoming_links =
|
||||
LinksAndLanguages.link_contexts(
|
||||
Map.get(post_record || %{}, :project_id),
|
||||
canonical_post_id(post_record, assigns),
|
||||
:incoming,
|
||||
Map.get(post_record || %{}, :language)
|
||||
)
|
||||
|
||||
outgoing_links =
|
||||
LinksAndLanguages.link_contexts(
|
||||
Map.get(post_record || %{}, :project_id),
|
||||
canonical_post_id(post_record, assigns),
|
||||
:outgoing,
|
||||
Map.get(post_record || %{}, :language)
|
||||
)
|
||||
|
||||
%{
|
||||
id =>
|
||||
post_data_json_value(
|
||||
build_post_context(assigns, post_record, incoming_links, outgoing_links)
|
||||
build_post_context(ctx, assigns, post_record, incoming_links, outgoing_links)
|
||||
)
|
||||
}
|
||||
else
|
||||
@@ -194,7 +205,7 @@ defmodule BDS.Rendering.PostRendering do
|
||||
end
|
||||
end
|
||||
|
||||
defp build_post_context(assigns, post_record, incoming_links, outgoing_links) do
|
||||
defp build_post_context(ctx, assigns, post_record, incoming_links, outgoing_links) do
|
||||
%{
|
||||
id: MapUtils.attr(assigns, :id),
|
||||
slug: MapUtils.attr(assigns, :slug),
|
||||
@@ -228,7 +239,7 @@ defmodule BDS.Rendering.PostRendering do
|
||||
MapUtils.attr(assigns, :template_slug)
|
||||
),
|
||||
do_not_translate: record_value(post_record, :do_not_translate, false),
|
||||
linked_media: linked_media_images(assigns),
|
||||
linked_media: linked_media_images(ctx, assigns),
|
||||
outgoing_links: outgoing_links,
|
||||
incoming_links: incoming_links
|
||||
}
|
||||
@@ -260,19 +271,11 @@ defmodule BDS.Rendering.PostRendering do
|
||||
)
|
||||
end
|
||||
|
||||
defp linked_media_images(assigns) do
|
||||
defp linked_media_images(%RenderContext{} = ctx, assigns) do
|
||||
post_id = MapUtils.attr(assigns, :id)
|
||||
|
||||
if is_binary(post_id) do
|
||||
Repo.all(
|
||||
from pm in PostMedia,
|
||||
join: m in MediaRecord,
|
||||
on: pm.media_id == m.id,
|
||||
where: pm.post_id == ^post_id,
|
||||
where: like(m.mime_type, "image/%"),
|
||||
order_by: [asc: pm.sort_order, asc: pm.media_id],
|
||||
select: m
|
||||
)
|
||||
Map.get(ctx.linked_media_by_post, post_id, [])
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
201
lib/bds/rendering/render_context.ex
Normal file
201
lib/bds/rendering/render_context.ex
Normal file
@@ -0,0 +1,201 @@
|
||||
defmodule BDS.Rendering.RenderContext do
|
||||
@moduledoc """
|
||||
Load-once bundle of everything page rendering needs.
|
||||
|
||||
Mirrors the old app's generation worker design: all project-wide data
|
||||
(metadata, menu, canonical URL maps, link graphs, media, template roots) is
|
||||
loaded up front in a handful of queries, and per-page rendering then works
|
||||
from immutable in-memory data instead of re-querying the database and
|
||||
re-reading config files for every page. An ETS-backed `memoize/3` cache holds
|
||||
parsed template ASTs, post bodies, and rendered markdown for the lifetime of
|
||||
the context.
|
||||
|
||||
The ETS cache table is owned by the process that calls `build/1` and is
|
||||
reclaimed automatically when that process exits, so a context must not
|
||||
outlive its creator.
|
||||
"""
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias BDS.Media.Media, as: MediaRecord
|
||||
alias BDS.Metadata, as: ProjectMetadata
|
||||
alias BDS.Posts.Link
|
||||
alias BDS.Posts.Post
|
||||
alias BDS.Posts.PostMedia
|
||||
alias BDS.Posts.Translation
|
||||
alias BDS.Projects
|
||||
alias BDS.Rendering.FileSystem
|
||||
alias BDS.Rendering.Filters
|
||||
alias BDS.Rendering.LinksAndLanguages
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Repo
|
||||
alias BDS.StarterTemplates
|
||||
alias BDS.Tags.Tag
|
||||
|
||||
defstruct [
|
||||
:project,
|
||||
:project_id,
|
||||
:project_data_dir,
|
||||
:metadata,
|
||||
:main_language,
|
||||
:menu_items,
|
||||
:tag_color_by_name,
|
||||
:canonical_post_paths,
|
||||
:canonical_media_paths,
|
||||
:file_system,
|
||||
:template_context,
|
||||
:record_by_id,
|
||||
:incoming_links_by_post,
|
||||
:outgoing_links_by_post,
|
||||
:translations_by_post,
|
||||
:linked_media_by_post,
|
||||
:tag_template_slug_by_name,
|
||||
:cache
|
||||
]
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@spec build(String.t()) :: t()
|
||||
def build(project_id) when is_binary(project_id) do
|
||||
project = Projects.get_project!(project_id)
|
||||
{:ok, metadata} = ProjectMetadata.get_project_metadata(project_id)
|
||||
main_language = metadata.main_language || "en"
|
||||
|
||||
posts = Repo.all(from post in Post, where: post.project_id == ^project_id)
|
||||
post_by_id = Map.new(posts, &{&1.id, &1})
|
||||
|
||||
translations =
|
||||
Repo.all(from translation in Translation, where: translation.project_id == ^project_id)
|
||||
|
||||
{incoming_links_by_post, outgoing_links_by_post} =
|
||||
build_link_context_maps(project_id, post_by_id, main_language)
|
||||
|
||||
file_system = FileSystem.new(StarterTemplates.template_roots(project))
|
||||
|
||||
%__MODULE__{
|
||||
project: project,
|
||||
project_id: project_id,
|
||||
project_data_dir: Projects.project_data_dir(project),
|
||||
metadata: metadata,
|
||||
main_language: main_language,
|
||||
menu_items: RenderMetadata.menu_items(project_id),
|
||||
tag_color_by_name: RenderMetadata.tag_color_by_name(project_id),
|
||||
canonical_post_paths:
|
||||
LinksAndLanguages.canonical_post_path_by_slug(project_id, main_language),
|
||||
canonical_media_paths: LinksAndLanguages.canonical_media_path_by_source_path(project_id),
|
||||
file_system: file_system,
|
||||
template_context:
|
||||
Liquex.Context.new(%{},
|
||||
static_environment: %{},
|
||||
filter_module: Filters,
|
||||
file_system: file_system
|
||||
),
|
||||
record_by_id: Map.merge(Map.new(translations, &{&1.id, &1}), post_by_id),
|
||||
incoming_links_by_post: incoming_links_by_post,
|
||||
outgoing_links_by_post: outgoing_links_by_post,
|
||||
translations_by_post: build_translations_by_post(translations),
|
||||
linked_media_by_post: build_linked_media_by_post(project_id),
|
||||
tag_template_slug_by_name: build_tag_template_slugs(project_id),
|
||||
cache: :ets.new(:bds_render_context_cache, [:set, :public, {:read_concurrency, true}])
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Return the cached value for `key`, computing and caching it with `fun` on the
|
||||
first call. Safe to call from concurrently rendering processes; concurrent
|
||||
first calls may compute twice but always return a consistent value.
|
||||
"""
|
||||
@spec memoize(t(), term(), (-> value)) :: value when value: term()
|
||||
def memoize(%__MODULE__{cache: cache}, key, fun) when is_function(fun, 0) do
|
||||
case :ets.lookup(cache, key) do
|
||||
[{^key, value}] ->
|
||||
value
|
||||
|
||||
[] ->
|
||||
value = fun.()
|
||||
:ets.insert(cache, {key, value})
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
@spec link_contexts(t(), String.t() | nil, :incoming | :outgoing) :: [map()]
|
||||
def link_contexts(_ctx, nil, _direction), do: []
|
||||
|
||||
def link_contexts(%__MODULE__{} = ctx, post_id, :incoming),
|
||||
do: Map.get(ctx.incoming_links_by_post, post_id, [])
|
||||
|
||||
def link_contexts(%__MODULE__{} = ctx, post_id, :outgoing),
|
||||
do: Map.get(ctx.outgoing_links_by_post, post_id, [])
|
||||
|
||||
## --- internals -----------------------------------------------------------
|
||||
|
||||
defp build_link_context_maps(project_id, post_by_id, main_language) do
|
||||
links =
|
||||
Repo.all(
|
||||
from link in Link,
|
||||
join: source in Post,
|
||||
on: link.source_post_id == source.id,
|
||||
where: source.project_id == ^project_id,
|
||||
order_by: [asc: link.created_at]
|
||||
)
|
||||
|
||||
incoming =
|
||||
links
|
||||
|> Enum.group_by(& &1.target_post_id)
|
||||
|> Map.new(fn {post_id, post_links} ->
|
||||
{post_id, link_contexts_for(post_links, & &1.source_post_id, post_by_id, main_language)}
|
||||
end)
|
||||
|
||||
outgoing =
|
||||
links
|
||||
|> Enum.group_by(& &1.source_post_id)
|
||||
|> Map.new(fn {post_id, post_links} ->
|
||||
{post_id, link_contexts_for(post_links, & &1.target_post_id, post_by_id, main_language)}
|
||||
end)
|
||||
|
||||
{incoming, outgoing}
|
||||
end
|
||||
|
||||
defp link_contexts_for(links, linked_id_fun, post_by_id, main_language) do
|
||||
links
|
||||
|> Enum.map(fn link ->
|
||||
case Map.get(post_by_id, linked_id_fun.(link)) do
|
||||
nil -> nil
|
||||
linked_post -> LinksAndLanguages.link_context_for_post(linked_post, main_language)
|
||||
end
|
||||
end)
|
||||
|> Enum.reject(&is_nil/1)
|
||||
end
|
||||
|
||||
defp build_translations_by_post(translations) do
|
||||
translations
|
||||
|> Enum.filter(&(&1.status == :published))
|
||||
|> Enum.sort_by(& &1.language)
|
||||
|> Enum.group_by(& &1.translation_for)
|
||||
end
|
||||
|
||||
defp build_linked_media_by_post(project_id) do
|
||||
Repo.all(
|
||||
from post_media in PostMedia,
|
||||
join: media in MediaRecord,
|
||||
on: post_media.media_id == media.id,
|
||||
where: post_media.project_id == ^project_id,
|
||||
where: like(media.mime_type, "image/%"),
|
||||
order_by: [asc: post_media.post_id, asc: post_media.sort_order, asc: post_media.media_id],
|
||||
select: {post_media.post_id, media}
|
||||
)
|
||||
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
|
||||
end
|
||||
|
||||
defp build_tag_template_slugs(project_id) do
|
||||
Repo.all(
|
||||
from tag in Tag,
|
||||
where:
|
||||
tag.project_id == ^project_id and
|
||||
not is_nil(tag.post_template_slug) and
|
||||
tag.post_template_slug != "",
|
||||
select: {tag.name, tag.post_template_slug}
|
||||
)
|
||||
|> Map.new()
|
||||
end
|
||||
end
|
||||
@@ -8,13 +8,19 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
alias BDS.Projects
|
||||
alias BDS.Rendering.FileSystem
|
||||
alias BDS.Rendering.Filters
|
||||
alias BDS.Rendering.RenderContext
|
||||
alias BDS.Repo
|
||||
alias BDS.StarterTemplates
|
||||
alias BDS.Tags.Tag
|
||||
alias BDS.Templates.Template
|
||||
|
||||
@spec resolve_post_template_slug(String.t(), [String.t()], [String.t()]) ::
|
||||
@spec resolve_post_template_slug(RenderContext.t() | String.t(), [String.t()], [String.t()]) ::
|
||||
String.t() | nil
|
||||
def resolve_post_template_slug(%RenderContext{} = ctx, tag_names, category_names) do
|
||||
Enum.find_value(tag_names, &Map.get(ctx.tag_template_slug_by_name, &1)) ||
|
||||
resolve_from_category_settings(ctx.metadata.category_settings || %{}, category_names)
|
||||
end
|
||||
|
||||
def resolve_post_template_slug(project_id, tag_names, category_names) do
|
||||
resolve_from_tags(project_id, tag_names) ||
|
||||
resolve_from_categories(project_id, category_names)
|
||||
@@ -40,8 +46,12 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
|
||||
defp resolve_from_categories(project_id, category_names) do
|
||||
{:ok, state} = Metadata.get_project_metadata(project_id)
|
||||
settings = state.category_settings || %{}
|
||||
resolve_from_category_settings(state.category_settings || %{}, category_names)
|
||||
end
|
||||
|
||||
defp resolve_from_category_settings(_settings, []), do: nil
|
||||
|
||||
defp resolve_from_category_settings(settings, category_names) do
|
||||
Enum.find_value(category_names, fn cat_name ->
|
||||
case Map.get(settings, cat_name) do
|
||||
%{"post_template_slug" => slug} when is_binary(slug) and slug != "" -> slug
|
||||
@@ -50,14 +60,22 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
end)
|
||||
end
|
||||
|
||||
@spec load_template_source(String.t(), atom(), String.t() | nil) ::
|
||||
@spec load_template_source(RenderContext.t() | String.t(), atom(), String.t() | nil) ::
|
||||
{:ok, String.t()} | {:error, term()}
|
||||
def load_template_source(project_id, kind, slug) do
|
||||
project = Projects.get_project!(project_id)
|
||||
def load_template_source(%RenderContext{} = ctx, kind, slug) do
|
||||
RenderContext.memoize(ctx, {:template_source, kind, slug}, fn ->
|
||||
do_load_template_source(ctx.project, ctx.project_id, kind, slug)
|
||||
end)
|
||||
end
|
||||
|
||||
def load_template_source(project_id, kind, slug) when is_binary(project_id) do
|
||||
do_load_template_source(Projects.get_project!(project_id), project_id, kind, slug)
|
||||
end
|
||||
|
||||
defp do_load_template_source(project, project_id, kind, slug) do
|
||||
case select_template(project_id, kind, slug) do
|
||||
%Template{} = template ->
|
||||
case published_template_body(template) do
|
||||
case published_template_body(project, template) do
|
||||
{:ok, _source} = ok ->
|
||||
ok
|
||||
|
||||
@@ -105,11 +123,10 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
limit: 1
|
||||
end
|
||||
|
||||
defp published_template_body(%Template{content: content}) when is_binary(content),
|
||||
defp published_template_body(_project, %Template{content: content}) when is_binary(content),
|
||||
do: {:ok, content}
|
||||
|
||||
defp published_template_body(%Template{} = template) do
|
||||
project = Projects.get_project!(template.project_id)
|
||||
defp published_template_body(project, %Template{} = template) do
|
||||
full_path = Path.join(Projects.project_data_dir(project), template.file_path)
|
||||
|
||||
case File.read(full_path) do
|
||||
@@ -124,11 +141,16 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
end
|
||||
end
|
||||
|
||||
@spec render_template(String.t(), String.t(), map()) ::
|
||||
@spec render_template(RenderContext.t() | String.t(), String.t(), map()) ::
|
||||
{:ok, String.t()} | {:error, String.t()}
|
||||
def render_template(project_id, source, assigns) do
|
||||
with {:ok, template_ast} <- Liquex.parse(source, BDS.Rendering.LiquidParser),
|
||||
{:ok, _rendered} = ok <- safe_liquex_render(template_ast, project_id, assigns) do
|
||||
def render_template(%RenderContext{} = ctx, source, assigns) do
|
||||
parse_result =
|
||||
RenderContext.memoize(ctx, {:template_ast, :erlang.md5(source)}, fn ->
|
||||
Liquex.parse(source, BDS.Rendering.LiquidParser)
|
||||
end)
|
||||
|
||||
with {:ok, template_ast} <- parse_result,
|
||||
{:ok, _rendered} = ok <- safe_liquex_render(template_ast, ctx.file_system, assigns) do
|
||||
ok
|
||||
else
|
||||
{:error, reason, line} when is_integer(line) -> {:error, "#{reason} at line #{line}"}
|
||||
@@ -136,14 +158,25 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
end
|
||||
end
|
||||
|
||||
defp safe_liquex_render(template_ast, project_id, assigns) do
|
||||
def render_template(project_id, source, assigns) when is_binary(project_id) do
|
||||
project = Projects.get_project!(project_id)
|
||||
file_system = FileSystem.new(StarterTemplates.template_roots(project))
|
||||
|
||||
with {:ok, template_ast} <- Liquex.parse(source, BDS.Rendering.LiquidParser),
|
||||
{:ok, _rendered} = ok <- safe_liquex_render(template_ast, file_system, assigns) do
|
||||
ok
|
||||
else
|
||||
{:error, reason, line} when is_integer(line) -> {:error, "#{reason} at line #{line}"}
|
||||
{:error, _message} = error -> error
|
||||
end
|
||||
end
|
||||
|
||||
defp safe_liquex_render(template_ast, file_system, assigns) do
|
||||
context =
|
||||
Liquex.Context.new(assigns,
|
||||
static_environment: assigns,
|
||||
filter_module: Filters,
|
||||
file_system: FileSystem.new(StarterTemplates.template_roots(project))
|
||||
file_system: file_system
|
||||
)
|
||||
|
||||
{result, _context} = Liquex.render!(template_ast, context)
|
||||
@@ -183,7 +216,10 @@ defmodule BDS.Rendering.TemplateSelection do
|
||||
defp bundled_template_slug(_kind, slug) when is_binary(slug) and slug != "", do: slug
|
||||
defp bundled_template_slug(kind, _slug), do: StarterTemplates.default_slug(kind)
|
||||
|
||||
@spec template_render_context(String.t()) :: Liquex.Context.t()
|
||||
@spec template_render_context(RenderContext.t() | String.t()) :: Liquex.Context.t()
|
||||
def template_render_context(%RenderContext{template_context: template_context}),
|
||||
do: template_context
|
||||
|
||||
def template_render_context(project_id) do
|
||||
project = Projects.get_project!(project_id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user