start layout support

This commit is contained in:
sloane 2024-02-17 08:30:17 -05:00
parent 9b4b4aa44c
commit 5166c17450
5 changed files with 49 additions and 10 deletions

View file

@ -1,10 +1,18 @@
defmodule SloaneSH.Build do defmodule SloaneSH.Build do
require Logger require Logger
require EEx
alias SloaneSH.Context alias SloaneSH.Context
alias SloaneSH.Markdown alias SloaneSH.Markdown
alias SloaneSH.Write 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 def run(%Context{} = ctx) do
ctx ctx
|> build_pages() |> build_pages()
@ -29,7 +37,8 @@ defmodule SloaneSH.Build do
path = Path.join(ctx.config.pages_dir, page) path = Path.join(ctx.config.pages_dir, page)
with {:ok, data} <- File.read(path), 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 :ok <- Write.page(ctx, page, html) do
Logger.info("Built page: #{page}") Logger.info("Built page: #{page}")
else else

View file

@ -20,6 +20,8 @@ defmodule SloaneSH.Watch do
{:ok, watcher_pid} = {:ok, watcher_pid} =
FileSystem.start_link( FileSystem.start_link(
dirs: [ dirs: [
# ctx.config.layouts_dir,
"priv/site/layouts",
ctx.config.pages_dir, ctx.config.pages_dir,
ctx.config.posts_dir ctx.config.posts_dir
] ]
@ -41,17 +43,23 @@ defmodule SloaneSH.Watch do
@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
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 if Context.in_context?(ctx, path) do
path = Path.relative_to(path, ctx.config.pages_dir) path = Path.relative_to(path, ctx.config.pages_dir)
Logger.info("File changed: #{path}") Logger.info("File changed: #{path}")
Build.build_page(ctx, path) Build.build_page(ctx, path)
end
%{state | ctx: ctx}
{:noreply, state}
end end
%{state | ctx: ctx}
{:noreply, state}
end end
@impl GenServer @impl GenServer
@ -59,4 +67,11 @@ defmodule SloaneSH.Watch do
Logger.warning("File watcher stopped") Logger.warning("File watcher stopped")
{:stop, :watcher_stopped, pid} {:stop, :watcher_stopped, pid}
end end
defp recompile_build do
build_source = Build.module_info(:compile)[:source] |> List.to_string()
{:ok, _, _} = Kernel.ParallelCompiler.compile([build_source])
:ok
end
end end

View file

View file

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>sloane.sh</title>
</head>
<body>
<header>
<nav>
<a href="/">home</a>
<a href="/about">about</a>
</nav>
</header>
<%= inner_content %>
</body>
</html>