From 4cebaf550bc1316cba5d0971240029ee6d536a2a Mon Sep 17 00:00:00 2001 From: Raymon Zutekouw <40148684+Raymonzut@users.noreply.github.com> Date: Thu, 18 Nov 2021 20:33:17 +0100 Subject: [PATCH] Folder by year/month instead of id under one folder --- client/gen.exs | 27 +++++++++++++++++++++++---- client/templates/post_item.xml | 4 ++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/client/gen.exs b/client/gen.exs index 9337a62..f5f50a5 100644 --- a/client/gen.exs +++ b/client/gen.exs @@ -67,10 +67,21 @@ post_contents_org = end)) ) +months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] + +dateFolder = fn p -> + Map.get(p, :date) + |> String.slice(8..15) + |> String.split(" ") + |> (fn ([month, year]) -> year <> "/" <> Integer.to_string(Enum.find_index(months, &(&1 == month)) + 1) <> "/" end).() +end +normalize_title = fn title -> String.replace(String.downcase(title), " ", "-") end + +constructLocalPath = fn p -> dateFolder.(p) <> normalize_title.(Map.get(p, :title)) end post_contents = Enum.concat(post_contents_custom, post_contents_org) + |> Enum.map(fn postMap -> Map.merge(postMap, %{localpath: (constructLocalPath.(postMap))}) end) -months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] index_file = post_contents |> Enum.sort_by(fn m -> Map.get(m, :date) |> (fn d -> Enum.find_index(months, &(&1 == String.slice(d, 8..10))) end).() end) |> Enum.reverse() @@ -80,7 +91,7 @@ index_file = post_contents |> Enum.reverse() |> Enum.map(fn {month, posts} -> "\n

" <> month <> "

\n" <> (posts |> Enum.map(fn post -> "

" <> (Map.get(post, :date) |> String.slice(0..6)) <> - " - Map.get(post, :id) <> ".html\">" <> Map.get(post, :title) <> "

" + " - Map.get(post, :localpath) <> ".html\">" <> Map.get(post, :title) <> "" end) |> Enum.join("\n")) end) |> Enum.join("\n") |> (fn v -> %{index: v} end).() @@ -90,9 +101,17 @@ File.open!("./public/gen/index.html", [:write]) |> IO.binwrite(index_file) |> File.close() + +prepare_path = fn path -> + File.mkdir_p!(Path.dirname(path)) + path +end + post_contents - |> Enum.each(fn post -> - File.open!("./public/gen/" <> Map.get(post, :id) <> ".html", [:write]) + |> Enum.each(fn post -> post + |> (fn p -> "./public/gen/" <> Map.get(p, :localpath) <> ".html" end).() + |> prepare_path.() + |> File.open!([:write]) |> IO.binwrite(fill_template.(post_template, post)) |> File.close() end) diff --git a/client/templates/post_item.xml b/client/templates/post_item.xml index fca9f40..9a65398 100644 --- a/client/templates/post_item.xml +++ b/client/templates/post_item.xml @@ -1,7 +1,7 @@ {{title}} - https://raymon.dev/posts/{{id}}.html - https://raymon.dev/posts/{{id}}.html + https://raymon.dev/posts/{{localpath}}.html + https://raymon.dev/posts/{{localpath}}.html