2024-02-16 22:28:13 -05:00
|
|
|
defmodule SloaneSH.Build do
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
alias SloaneSH.Context
|
2024-02-23 22:15:36 -05:00
|
|
|
alias SloaneSH.Layouts
|
2024-02-16 22:28:13 -05:00
|
|
|
alias SloaneSH.Markdown
|
|
|
|
alias SloaneSH.Write
|
|
|
|
|
|
|
|
def run(%Context{} = ctx) do
|
|
|
|
ctx
|
|
|
|
|> build_pages()
|
|
|
|
|> build_posts()
|
2024-02-24 09:04:35 -05:00
|
|
|
|> copy_img()
|
2024-02-16 22:28:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_pages(%Context{} = ctx) do
|
|
|
|
Logger.info("Building pages...")
|
|
|
|
for page <- ctx.pages, do: build_page(ctx, page)
|
|
|
|
|
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_posts(%Context{} = ctx) do
|
|
|
|
Logger.info("Building posts...")
|
|
|
|
for post <- ctx.posts, do: build_post(ctx, post)
|
|
|
|
|
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_page(%Context{} = ctx, page) do
|
|
|
|
path = Path.join(ctx.config.pages_dir, page)
|
|
|
|
|
|
|
|
with {:ok, data} <- File.read(path),
|
2024-02-23 22:15:36 -05:00
|
|
|
{:ok, md} <- Markdown.transform(ctx, data),
|
|
|
|
contents = Layouts.page_layout(ctx, md.attrs, md.html),
|
|
|
|
html = Layouts.root_layout(ctx, md.attrs, contents),
|
2024-02-16 22:28:13 -05:00
|
|
|
:ok <- Write.page(ctx, page, html) do
|
|
|
|
Logger.info("Built page: #{page}")
|
|
|
|
else
|
|
|
|
err -> Logger.error("Failed to build page #{page}: #{inspect(err)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_post(%Context{} = ctx, post) do
|
|
|
|
path = Path.join(ctx.config.posts_dir, post)
|
|
|
|
|
|
|
|
with {:ok, data} <- File.read(path),
|
2024-02-23 22:15:36 -05:00
|
|
|
{:ok, md} <- Markdown.transform(ctx, data),
|
2024-02-23 22:41:26 -05:00
|
|
|
contents = Layouts.post_layout(ctx, md.attrs, md.html),
|
|
|
|
html = Layouts.root_layout(ctx, md.attrs, contents),
|
|
|
|
:ok <- Write.post(ctx, post, html) do
|
2024-02-16 22:28:13 -05:00
|
|
|
Logger.info("Built post: #{post}")
|
|
|
|
else
|
|
|
|
err -> Logger.error("Failed to build post #{post}: #{inspect(err)}")
|
|
|
|
end
|
|
|
|
end
|
2024-02-24 09:04:35 -05:00
|
|
|
|
|
|
|
def copy_img(%Context{} = ctx) do
|
|
|
|
|
|
|
|
ctx
|
|
|
|
end
|
2024-02-16 22:28:13 -05:00
|
|
|
end
|