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
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

View file

@ -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,6 +43,11 @@ defmodule SloaneSH.Watch do
@impl GenServer
def handle_info({:file_event, pid, {path, events}}, %{ctx: ctx, watcher_pid: pid} = state) do
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
@ -53,10 +60,18 @@ defmodule SloaneSH.Watch do
{:noreply, state}
end
end
@impl GenServer
def handle_info({:file_event, pid, :stop}, %{watcher_pid: pid}) 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

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>