Folder by year/month instead of id under one folder

master
Raymon Zutekouw 3 years ago
parent 04b41fa182
commit 4cebaf550b
No known key found for this signature in database
GPG Key ID: 1E9BCC39EDD1DD53
  1. 27
      client/gen.exs
  2. 4
      client/templates/post_item.xml

@ -67,10 +67,21 @@ post_contents_org =
end)) 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) 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 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.sort_by(fn m -> Map.get(m, :date) |> (fn d -> Enum.find_index(months, &(&1 == String.slice(d, 8..10))) end).() end)
|> Enum.reverse() |> Enum.reverse()
@ -80,7 +91,7 @@ index_file = post_contents
|> Enum.reverse() |> Enum.reverse()
|> Enum.map(fn {month, posts} -> "\n<h1>" <> month <> "</h1>\n" <> (posts |> |> Enum.map(fn {month, posts} -> "\n<h1>" <> month <> "</h1>\n" <> (posts |>
Enum.map(fn post -> "<h2>" <> (Map.get(post, :date) |> String.slice(0..6)) <> Enum.map(fn post -> "<h2>" <> (Map.get(post, :date) |> String.slice(0..6)) <>
" - <a href=\"" <> Map.get(post, :id) <> ".html\">" <> Map.get(post, :title) <> "</a></h2>" " - <a href=\"" <> Map.get(post, :localpath) <> ".html\">" <> Map.get(post, :title) <> "</a></h2>"
end) |> Enum.join("\n")) end) end) |> Enum.join("\n")) end)
|> Enum.join("\n") |> Enum.join("\n")
|> (fn v -> %{index: v} end).() |> (fn v -> %{index: v} end).()
@ -90,9 +101,17 @@ File.open!("./public/gen/index.html", [:write])
|> IO.binwrite(index_file) |> IO.binwrite(index_file)
|> File.close() |> File.close()
prepare_path = fn path ->
File.mkdir_p!(Path.dirname(path))
path
end
post_contents post_contents
|> Enum.each(fn post -> |> Enum.each(fn post -> post
File.open!("./public/gen/" <> Map.get(post, :id) <> ".html", [:write]) |> (fn p -> "./public/gen/" <> Map.get(p, :localpath) <> ".html" end).()
|> prepare_path.()
|> File.open!([:write])
|> IO.binwrite(fill_template.(post_template, post)) |> IO.binwrite(fill_template.(post_template, post))
|> File.close() |> File.close()
end) end)

@ -1,7 +1,7 @@
<item> <item>
<title>{{title}}</title> <title>{{title}}</title>
<link>https://raymon.dev/posts/{{id}}.html</link> <link>https://raymon.dev/posts/{{localpath}}.html</link>
<guid>https://raymon.dev/posts/{{id}}.html</guid> <guid>https://raymon.dev/posts/{{localpath}}.html</guid>
<description> <description>
<![CDATA[ <![CDATA[
{{content}} {{content}}

Loading…
Cancel
Save