From f743cca9feff7e11cff46cb9094bf2a601ef6fd0 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Sat, 24 Feb 2024 13:40:20 -0500 Subject: [PATCH] add helpers, fix watch --- lib/sloane_sh/assets/markdown.ex | 12 ++++++++- lib/sloane_sh/layouts.ex | 3 ++- lib/sloane_sh/layouts/helpers.ex | 19 +++++++++++++ lib/sloane_sh/layouts/partials.ex | 11 +------- lib/sloane_sh/markdown.ex | 39 --------------------------- lib/sloane_sh/watch.ex | 23 +++++----------- priv/site/pages/{index.md => home.md} | 1 + priv/site/pages/posts.html.eex | 7 +++++ 8 files changed, 48 insertions(+), 67 deletions(-) create mode 100644 lib/sloane_sh/layouts/helpers.ex delete mode 100644 lib/sloane_sh/markdown.ex rename priv/site/pages/{index.md => home.md} (95%) create mode 100644 priv/site/pages/posts.html.eex diff --git a/lib/sloane_sh/assets/markdown.ex b/lib/sloane_sh/assets/markdown.ex index be33c15..19ca77e 100644 --- a/lib/sloane_sh/assets/markdown.ex +++ b/lib/sloane_sh/assets/markdown.ex @@ -6,6 +6,7 @@ defmodule SloaneSH.Assets.Markdown do type = Keyword.fetch!(opts, :type) quote do + import SloaneSH.Layouts.Helpers, warn: false alias SloaneSH.Asset alias SloaneSH.FrontMatter alias SloaneSH.Layouts @@ -49,7 +50,7 @@ defmodule SloaneSH.Assets.Markdown do end 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) end @@ -68,6 +69,15 @@ defmodule SloaneSH.Assets.Markdown do {base, ext} 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 attrs end diff --git a/lib/sloane_sh/layouts.ex b/lib/sloane_sh/layouts.ex index 1f1563c..8198124 100644 --- a/lib/sloane_sh/layouts.ex +++ b/lib/sloane_sh/layouts.ex @@ -3,7 +3,8 @@ defmodule SloaneSH.Layouts do `EEx` based layouts """ 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") diff --git a/lib/sloane_sh/layouts/helpers.ex b/lib/sloane_sh/layouts/helpers.ex new file mode 100644 index 0000000..027d128 --- /dev/null +++ b/lib/sloane_sh/layouts/helpers.ex @@ -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 diff --git a/lib/sloane_sh/layouts/partials.ex b/lib/sloane_sh/layouts/partials.ex index fe1fe22..ad435c9 100644 --- a/lib/sloane_sh/layouts/partials.ex +++ b/lib/sloane_sh/layouts/partials.ex @@ -3,6 +3,7 @@ defmodule SloaneSH.Layouts.Partials do HTML partials for use in HTML layouts """ require EEx + import SloaneSH.Layouts.Helpers, warn: false EEx.function_from_string( :def, @@ -21,14 +22,4 @@ defmodule SloaneSH.Layouts.Partials do """, [: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 diff --git a/lib/sloane_sh/markdown.ex b/lib/sloane_sh/markdown.ex deleted file mode 100644 index e8ed4d8..0000000 --- a/lib/sloane_sh/markdown.ex +++ /dev/null @@ -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 diff --git a/lib/sloane_sh/watch.ex b/lib/sloane_sh/watch.ex index 8313e29..ebe7efe 100644 --- a/lib/sloane_sh/watch.ex +++ b/lib/sloane_sh/watch.ex @@ -52,24 +52,14 @@ defmodule SloaneSH.Watch do end @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 recompile_layouts() - 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} end + + Build.run(ctx) + + {:noreply, state} end @impl GenServer @@ -79,9 +69,10 @@ defmodule SloaneSH.Watch do end 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() 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 end diff --git a/priv/site/pages/index.md b/priv/site/pages/home.md similarity index 95% rename from priv/site/pages/index.md rename to priv/site/pages/home.md index 624975a..a213632 100644 --- a/priv/site/pages/index.md +++ b/priv/site/pages/home.md @@ -1,5 +1,6 @@ +++ page_title = "home" +permalink = "/" +++ hey, i'm sloane! i'm a professional software engineer and an amateur musician, photographer, wife, chef, and pet mom. diff --git a/priv/site/pages/posts.html.eex b/priv/site/pages/posts.html.eex new file mode 100644 index 0000000..b69face --- /dev/null +++ b/priv/site/pages/posts.html.eex @@ -0,0 +1,7 @@ +