add helpers, fix watch

This commit is contained in:
sloane 2024-02-24 13:40:20 -05:00
parent 8bc094afb8
commit f743cca9fe
8 changed files with 48 additions and 67 deletions

View file

@ -6,6 +6,7 @@ defmodule SloaneSH.Assets.Markdown do
type = Keyword.fetch!(opts, :type) type = Keyword.fetch!(opts, :type)
quote do quote do
import SloaneSH.Layouts.Helpers, warn: false
alias SloaneSH.Asset alias SloaneSH.Asset
alias SloaneSH.FrontMatter alias SloaneSH.FrontMatter
alias SloaneSH.Layouts alias SloaneSH.Layouts
@ -49,7 +50,7 @@ defmodule SloaneSH.Assets.Markdown do
end end
defp do_render(ctx, {path, ".eex"}, data, attrs) do defp do_render(ctx, {path, ".eex"}, data, attrs) do
eexed = EEx.eval_string(data, ctx: ctx, attrs: attrs) eexed = eval_eex(data, "#{path}.eex", ctx, attrs)
do_render(ctx, base_and_ext(path), eexed, attrs) do_render(ctx, base_and_ext(path), eexed, attrs)
end end
@ -68,6 +69,15 @@ defmodule SloaneSH.Assets.Markdown do
{base, ext} {base, ext}
end end
defp eval_eex(template, file, ctx, attrs) do
{result, _binding} =
template
|> EEx.compile_string(file: file)
|> Code.eval_quoted([ctx: ctx, attrs: attrs], __ENV__)
result
end
def handle_attrs(cfg, path, data, attrs) do def handle_attrs(cfg, path, data, attrs) do
attrs attrs
end end

View file

@ -3,7 +3,8 @@ defmodule SloaneSH.Layouts do
`EEx` based layouts `EEx` based layouts
""" """
require EEx require EEx
import SloaneSH.Layouts.Partials import SloaneSH.Layouts.Partials, warn: false
import SloaneSH.Layouts.Helpers, warn: false
@layouts_dir Path.join(:code.priv_dir(:sloane_sh), "site/layouts") @layouts_dir Path.join(:code.priv_dir(:sloane_sh), "site/layouts")

View file

@ -0,0 +1,19 @@
defmodule SloaneSH.Layouts.Helpers do
alias SloaneSH.Context
def cx(classes) do
classes
|> Enum.map(fn
{_, _} = t -> t
c -> {c, true}
end)
|> Enum.filter(fn {_, v} -> !!v end)
|> Enum.map_join(" ", fn {class, _} -> class end)
end
def sorted_post_attrs(%Context{} = ctx) do
ctx.posts
|> Enum.map(& &1.attrs)
|> Enum.sort_by(& &1[:date], &Date.compare/2)
end
end

View file

@ -3,6 +3,7 @@ defmodule SloaneSH.Layouts.Partials do
HTML partials for use in HTML layouts HTML partials for use in HTML layouts
""" """
require EEx require EEx
import SloaneSH.Layouts.Helpers, warn: false
EEx.function_from_string( EEx.function_from_string(
:def, :def,
@ -21,14 +22,4 @@ defmodule SloaneSH.Layouts.Partials do
""", """,
[:ctx, :attrs] [:ctx, :attrs]
) )
defp cx(classes) do
classes
|> Enum.map(fn
{_, _} = t -> t
c -> {c, true}
end)
|> Enum.filter(fn {_, v} -> !!v end)
|> Enum.map_join(" ", fn {class, _} -> class end)
end
end end

View file

@ -1,39 +0,0 @@
defmodule SloaneSH.Markdown do
@moduledoc """
Markdown parsing using `Earmark` and `Earmark.Parser`
"""
require Logger
use TypedStruct
alias SloaneSH.Context
alias SloaneSH.FrontMatter
alias __MODULE__
typedstruct do
field :attrs, map(), default: %{}
field :html, String.t(), default: ""
field :text, String.t(), default: ""
end
def transform(%Context{} = ctx, data) when is_binary(data) do
with {:ok, attrs, body} <- FrontMatter.parse(data, ctx),
{:ok, html, msgs} <- Earmark.as_html(body),
{:ok, html_tree} <- Floki.parse_fragment(html) do
for msg <- msgs, do: Logger.warning(msg)
{:ok,
%Markdown{
attrs: attrs,
html: html,
text: Floki.text(html_tree, sep: " ")
}}
else
{:error, _, msgs} ->
for msg <- msgs, do: Logger.error(msg)
:error
_ ->
:error
end
end
end

View file

@ -52,25 +52,15 @@ defmodule SloaneSH.Watch do
end end
@impl GenServer @impl GenServer
def handle_info({:file_event, pid, {path, events}}, %{ctx: ctx, watcher_pid: pid} = state) do def handle_info({:file_event, pid, {path, _events}}, %{ctx: ctx, watcher_pid: pid} = state) do
if String.match?(path, ~r/layouts/) do if String.match?(path, ~r/layouts/) do
recompile_layouts() recompile_layouts()
end
Build.run(ctx) Build.run(ctx)
{:noreply, state}
else
ctx = Context.maybe_add(ctx, path)
if Context.in_context?(ctx, path) do
path = Path.relative_to(path, ctx.config.pages_dir)
Logger.info("File changed: #{path}")
Build.build_page(ctx, path)
end
%{state | ctx: ctx}
{:noreply, state} {:noreply, state}
end end
end
@impl GenServer @impl GenServer
def handle_info({:file_event, pid, :stop}, %{watcher_pid: pid}) do def handle_info({:file_event, pid, :stop}, %{watcher_pid: pid}) do
@ -79,9 +69,10 @@ defmodule SloaneSH.Watch do
end end
defp recompile_layouts do defp recompile_layouts do
helpers_source = Layouts.Helpers.module_info(:compile)[:source] |> List.to_string()
partials_source = Layouts.Partials.module_info(:compile)[:source] |> List.to_string() partials_source = Layouts.Partials.module_info(:compile)[:source] |> List.to_string()
layouts_source = Layouts.module_info(:compile)[:source] |> List.to_string() layouts_source = Layouts.module_info(:compile)[:source] |> List.to_string()
{:ok, _, _} = Kernel.ParallelCompiler.compile([partials_source, layouts_source]) {:ok, _, _} = Kernel.ParallelCompiler.compile([helpers_source, partials_source, layouts_source])
:ok :ok
end end

View file

@ -1,5 +1,6 @@
+++ +++
page_title = "home" page_title = "home"
permalink = "/"
+++ +++
hey, i'm sloane! i'm a professional software engineer and an amateur musician, photographer, wife, chef, and pet mom. hey, i'm sloane! i'm a professional software engineer and an amateur musician, photographer, wife, chef, and pet mom.

View file

@ -0,0 +1,7 @@
<ul>
<%= for post <- sorted_post_attrs(ctx) do %>
<li>
<a href="<%= post[:permalink] %>"><%= post[:title] %></a>
</li>
<% end %>
</ul>