defmodule Reblog.Engine.Builder do @moduledoc """ Builds particular file associations. """ require Logger @doc """ Build a page provided page details. """ def build(:page, vinyl, context) do build_context = Map.put(context, :title, vinyl.basename) content_rendered = EEx.eval_string(vinyl.contents, assigns: Map.to_list(build_context)) build_context = Map.put(build_context, :body, content_rendered) page_rendered = EEx.eval_string(build_context.base_layout, assigns: Map.to_list(build_context)) {page_rendered, build_context} end @doc """ Build a post provided post details. """ def build(:post, vinyl, context) do [post_date, _] = String.split(vinyl.basename, "_") {post_body, front_matter} = build(:post_body, vinyl) build_context = %{ title: context.name, body: post_body, url: Path.join(["/", context.blog, "#{vinyl.stem}.html"]), date: Date.from_iso8601!(post_date), excerpt: "" } build_context = Map.merge(context, build_context) build_context = Map.merge(build_context, front_matter) post_rendered = EEx.eval_string(build_context.base_layout, assigns: Map.to_list(build_context)) {post_rendered, build_context} end def build(:post_body, vinyl) do Logger.debug(fn -> ["Contents: ", vinyl.contents] end) # required: description, author, email {front_matter, body} = YamlFrontMatter.parse!(vinyl.contents) front_matter = for {k, v} <- front_matter, into: %{}, do: {String.to_atom(k), v} {Earmark.as_html!(body), front_matter} end end