From 2d6e087342a79a465eed160603643088a4e00473 Mon Sep 17 00:00:00 2001 From: Raymon Zutekouw <40148684+Raymonzut@users.noreply.github.com> Date: Wed, 28 Apr 2021 12:34:36 +0200 Subject: [PATCH] Extend post format options with .org files --- client/.gitignore | 5 ++- client/gen.exs | 42 ++++++++++++++++++++- client/posts/ExamplePost | 8 ---- client/posts_custom/ExamplePostCustomFormat | 8 ++++ client/posts_org/ExamplePostOrgFormat.org | 4 ++ 5 files changed, 56 insertions(+), 11 deletions(-) delete mode 100644 client/posts/ExamplePost create mode 100644 client/posts_custom/ExamplePostCustomFormat create mode 100644 client/posts_org/ExamplePostOrgFormat.org diff --git a/client/.gitignore b/client/.gitignore index 03ddbf1..30947c0 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -1 +1,4 @@ -posts/* +posts_custom/* +!posts_custom/ExamplePostCustomFormat +posts_org/* +!posts_org/ExamplePostOrgFormat.org diff --git a/client/gen.exs b/client/gen.exs index 82bfd5c..9337a62 100644 --- a/client/gen.exs +++ b/client/gen.exs @@ -21,9 +21,9 @@ end fill_template_pretemplated = fn template -> &fill_template.(template, &1) end -post_contents = File.ls!("./posts") +post_contents_custom = File.ls!("./posts_custom") |> Enum.reject(fn(x) -> String.starts_with?(x, ".") end) - |> Enum.map(fn f -> File.read!("./posts/" <> f) end) + |> Enum.map(fn f -> File.read!("./posts_custom/" <> f) end) |> Enum.map(fn c -> String.split(c, "\n") end) |> Enum.map(fn c -> Enum.reject(c, fn(x) -> x == "" end) end) |> Enum.map(fn c -> Enum.chunk_every(c, 2) end) @@ -32,6 +32,44 @@ post_contents = File.ls!("./posts") |> Enum.reduce(%{}, fn(x, acc) -> Map.merge(x, acc) end) end) +filterCutFileExtension = fn files -> + fn ext -> + Enum.filter(files, &String.ends_with?(&1, "." <> ext)) + |> Enum.map(&String.replace_suffix(&1, "." <> ext, "")) + |> (fn x -> %{String.to_atom(ext) => x} end).() + end +end + +isMeta = &String.starts_with?(&1, "#+") + +posts_org = + File.ls!("./posts_org") + |> Enum.reject(fn x -> String.starts_with?(x, ".") end) + |> (fn files -> Enum.map(["org", "html"], filterCutFileExtension.(files)) end).() + |> Enum.reduce(%{}, fn x, acc -> Map.merge(x, acc) end) + +post_contents_org = + Enum.filter(Map.get(posts_org, :org), fn el -> Enum.member?(Map.get(posts_org, :html), el) end) + |> Enum.map( + &(File.read!("./posts_org/" <> &1 <> ".org") + |> (fn fileContent -> String.split(fileContent, "\n") end).() + |> Enum.take_while(isMeta) + |> Enum.map(fn metaOrg -> + [head | tail] = String.split(metaOrg, ":") + + %{ + (String.slice(head, 2..-1) |> String.downcase() |> String.to_atom()) => + String.slice(Enum.join(tail, ""), 1..-1) + } + end) + |> Enum.reduce(%{:content => File.read!("./posts_org/" <> &1 <> ".html")}, fn x, acc -> + Map.merge(x, acc) + end)) + ) + + +post_contents = Enum.concat(post_contents_custom, post_contents_org) + 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) diff --git a/client/posts/ExamplePost b/client/posts/ExamplePost deleted file mode 100644 index 12e9d21..0000000 --- a/client/posts/ExamplePost +++ /dev/null @@ -1,8 +0,0 @@ -id -4739537f3d29e1047a45c254 -title -Example post for my new post page! -date -Mon, 11 May 2020 13:25:00 GMT -content -My new posts page is live diff --git a/client/posts_custom/ExamplePostCustomFormat b/client/posts_custom/ExamplePostCustomFormat new file mode 100644 index 0000000..c9d9488 --- /dev/null +++ b/client/posts_custom/ExamplePostCustomFormat @@ -0,0 +1,8 @@ +id +4739537f3d29e1047a45c254 +title +Example post for my new custom-gen post page! +date +Mon, 11 May 2020 13:25:00 GMT +content +My new posts page is live, this post has been generated from a custom format. diff --git a/client/posts_org/ExamplePostOrgFormat.org b/client/posts_org/ExamplePostOrgFormat.org new file mode 100644 index 0000000..e612af3 --- /dev/null +++ b/client/posts_org/ExamplePostOrgFormat.org @@ -0,0 +1,4 @@ +#+ID: 4739537f3d29e1047a45c261 +#+TITLE: Example post for my new org-gen post page! +#+DATE: Wed, 28 Apr 2021 11:25:00 GMT +My new posts page is live, this post has been generated from an org file