From 5166c17450aab17c352a6fbbfe1f3992f369c0b5 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Sat, 17 Feb 2024 08:30:17 -0500 Subject: [PATCH] start layout support --- lib/sloane_sh/build.ex | 11 ++++++++++- lib/sloane_sh/watch.ex | 33 +++++++++++++++++++++++--------- priv/site/layouts/pages.html.eex | 0 priv/site/layouts/posts.html.eex | 0 priv/site/layouts/root.html.eex | 15 +++++++++++++++ 5 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 priv/site/layouts/pages.html.eex create mode 100644 priv/site/layouts/posts.html.eex create mode 100644 priv/site/layouts/root.html.eex diff --git a/lib/sloane_sh/build.ex b/lib/sloane_sh/build.ex index c295258..9130216 100644 --- a/lib/sloane_sh/build.ex +++ b/lib/sloane_sh/build.ex @@ -1,10 +1,18 @@ defmodule SloaneSH.Build do require Logger + require EEx alias SloaneSH.Context alias SloaneSH.Markdown alias SloaneSH.Write + @layouts_dir Path.join(:code.priv_dir(:sloane_sh), "site/layouts") + + EEx.function_from_file(:def, :root_layout, Path.join(@layouts_dir, "root.html.eex"), [ + :ctx, + :inner_content + ]) + def run(%Context{} = ctx) do ctx |> build_pages() @@ -29,7 +37,8 @@ defmodule SloaneSH.Build do path = Path.join(ctx.config.pages_dir, page) with {:ok, data} <- File.read(path), - {:ok, html} <- Markdown.transform(ctx, data), + {:ok, inner_content} <- Markdown.transform(ctx, data), + html = root_layout(ctx, inner_content), :ok <- Write.page(ctx, page, html) do Logger.info("Built page: #{page}") else diff --git a/lib/sloane_sh/watch.ex b/lib/sloane_sh/watch.ex index 93e7011..cfaa5e1 100644 --- a/lib/sloane_sh/watch.ex +++ b/lib/sloane_sh/watch.ex @@ -20,6 +20,8 @@ defmodule SloaneSH.Watch do {:ok, watcher_pid} = FileSystem.start_link( dirs: [ + # ctx.config.layouts_dir, + "priv/site/layouts", ctx.config.pages_dir, ctx.config.posts_dir ] @@ -41,17 +43,23 @@ defmodule SloaneSH.Watch do @impl GenServer def handle_info({:file_event, pid, {path, events}}, %{ctx: ctx, watcher_pid: pid} = state) do - ctx = Context.maybe_add(ctx, path) + if String.match?(path, ~r/layouts/) do + recompile_build() + 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) + 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 - - %{state | ctx: ctx} - - {:noreply, state} end @impl GenServer @@ -59,4 +67,11 @@ defmodule SloaneSH.Watch do Logger.warning("File watcher stopped") {:stop, :watcher_stopped, pid} end + + defp recompile_build do + build_source = Build.module_info(:compile)[:source] |> List.to_string() + {:ok, _, _} = Kernel.ParallelCompiler.compile([build_source]) + + :ok + end end diff --git a/priv/site/layouts/pages.html.eex b/priv/site/layouts/pages.html.eex new file mode 100644 index 0000000..e69de29 diff --git a/priv/site/layouts/posts.html.eex b/priv/site/layouts/posts.html.eex new file mode 100644 index 0000000..e69de29 diff --git a/priv/site/layouts/root.html.eex b/priv/site/layouts/root.html.eex new file mode 100644 index 0000000..eb2a0d4 --- /dev/null +++ b/priv/site/layouts/root.html.eex @@ -0,0 +1,15 @@ + + + + sloane.sh + + +
+ +
+ <%= inner_content %> + +