sloane.sh/lib/sloane_sh/output_dirs.ex
2024-02-24 19:52:57 -05:00

47 lines
1.1 KiB
Elixir

defmodule SloaneSH.OutputDirs do
def page(cfg, src) do
path = Path.relative_to(src, cfg.pages_dir)
cfg.output_dir |> Path.join(path) |> prettify_html_path()
end
def post(cfg, src) do
path = Path.relative_to(src, cfg.posts_dir)
path = Path.join("post", path)
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)
suffix =
if without_extension == "index" do
"index.html"
else
Path.join(without_extension, "index.html")
end
String.replace_suffix(path, file, suffix)
end
def to_permalink(cfg, output_path) do
output_path
|> Path.relative_to(cfg.output_dir)
|> String.trim_trailing("index.html")
|> String.replace_prefix("", "/")
|> String.trim_trailing("/")
end
def from_permalink(cfg, permalink) do
Path.join([cfg.output_dir, permalink, "/index.html"])
end
end