setup empty image processing

This commit is contained in:
sloane 2024-02-24 19:52:57 -05:00
parent 6de9031055
commit a5696c49f2
6 changed files with 33 additions and 2 deletions

View file

@ -0,0 +1,19 @@
defmodule SloaneSH.Assets.Image do
alias SloaneSH.Asset
alias SloaneSH.OutputDirs
@behaviour Asset
@impl Asset
def extensions(_cfg), do: ~w[.jpg .jpeg .png .webp .gif]
@impl Asset
def attrs(_cfg, _path, _data) do
{:ok, %{}}
end
@impl Asset
def render(cfg, _ctx, path, data, _attrs) do
{:ok, [{OutputDirs.image(cfg, path), data}]}
end
end

View file

@ -4,7 +4,7 @@ defmodule SloaneSH.Build do
alias SloaneSH.Context
def run(%Context{} = ctx) do
assets = ctx.posts ++ ctx.pages
assets = ctx.posts ++ ctx.pages ++ ctx.images
File.mkdir_p!(ctx.config.output_dir)

View file

@ -9,6 +9,7 @@ defmodule SloaneSH.Config do
typedstruct do
field :pages_dir, String.t(), enforce: true
field :posts_dir, String.t(), enforce: true
field :images_dir, String.t(), enforce: true
field :output_dir, String.t(), enforce: true
end
@ -18,6 +19,7 @@ defmodule SloaneSH.Config do
%Config{
pages_dir: Path.join(priv, "site/pages"),
posts_dir: Path.join(priv, "site/posts"),
images_dir: Path.join(priv, "site/images"),
output_dir: Path.join(priv, "output")
}
end

View file

@ -10,19 +10,22 @@ defmodule SloaneSH.Context do
alias SloaneSH.Asset
alias SloaneSH.Assets.Page
alias SloaneSH.Assets.Post
alias SloaneSH.Assets.Image
alias __MODULE__
typedstruct do
field :config, Config.t(), enforce: true
field :pages, [Asset.t()], default: []
field :posts, [Asset.t()], default: []
field :images, [Asset.t()], default: []
end
def new(cfg \\ Config.default()) do
pages = load_assets(cfg, Page, cfg.pages_dir)
posts = load_assets(cfg, Post, cfg.posts_dir)
images = load_assets(cfg, Image, cfg.images_dir)
%Context{config: cfg, pages: pages, posts: posts}
%Context{config: cfg, pages: pages, posts: posts, images: images}
end
defp load_assets(cfg, mod, src_dir) do

View file

@ -12,6 +12,13 @@ defmodule SloaneSH.OutputDirs do
cfg.output_dir |> Path.join(path) |> prettify_html_path()
end
def image(cfg, src) do
path = Path.relative_to(src, cfg.images_dir)
path = Path.join("assets/images", path)
cfg.output_dir |> Path.join(path)
end
def prettify_html_path(path) do
file = Path.basename(path)
[without_extension | _] = String.split(file, ".", parts: 2)

View file

Before

Width:  |  Height:  |  Size: 889 B

After

Width:  |  Height:  |  Size: 889 B