diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..03ddbf1 --- /dev/null +++ b/client/.gitignore @@ -0,0 +1 @@ +posts/* diff --git a/client/Dockerfile b/client/Dockerfile index 9bda8c3..5da8154 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -1,4 +1,3 @@ FROM nginx as production-stage RUN mkdir /app -COPY /public /app COPY nginx.conf /etc/nginx/nginx.conf diff --git a/client/gen.exs b/client/gen.exs new file mode 100644 index 0000000..c1ab456 --- /dev/null +++ b/client/gen.exs @@ -0,0 +1,73 @@ +# Templates will be filled by posts +index_template = File.read!("./templates/post_index_page.html") +post_template = File.read!("./templates/post_single_page.html") + +post_feed_item_template = File.read!("./templates/post_item.xml") +post_feed_template = File.read!("./templates/posts.xml") + +# Converting handlebars in template to values +fill_template = fn template, pairs -> + Regex.compile!("{{(.*)}}") |> + Regex.replace(template, fn _, key -> if key != "content" do Map.get(pairs, String.to_atom(key)) else + Map.get(pairs, String.to_atom(key)) + # Converting \n to paragraphs + |> String.split("\\n") + |> Enum.reject(fn(x) -> x == "" end) + |> Enum.map(fn paragraph -> ("
" <> paragraph <> "
\n") end) + |> Enum.join("") + end + end) +end + +fill_template_pretemplated = fn template -> &fill_template.(template, &1) end + +post_contents = File.ls!("./posts") + |> Enum.reject(fn(x) -> String.starts_with?(x, ".") end) + |> Enum.map(fn f -> File.read!("./posts/" <> 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) + |> Enum.map(fn c -> + Enum.map(c, fn [k, v] -> %{String.to_atom(k) => v} end) + |> Enum.reduce(%{}, fn(x, acc) -> Map.merge(x, acc) end) + 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() + # Group by month + |> Enum.group_by(fn m -> Map.get(m, :date) |> String.slice(8..15) end) + |> Enum.sort_by(fn {d, _c} -> (length months) * elem(Integer.parse(String.slice(d, 4..7)), 0) + + Enum.find_index(months, &(&1 == String.slice(d, 0..2))) end) + |> Enum.reverse() + |> Enum.map(fn {month, posts} -> "\n" <> paragraph <> "
\n") end) - |> Enum.join("") - end - end)) - |> File.close() - end) - -post_feed = post_contents - |> Enum.sort_by(fn m -> Map.get(m, :date) end) - |> Enum.reverse() - |> Enum.map(fn post -> - Regex.compile!("{{(.*)}}") |> - Regex.replace(post_feed_item_template, fn _, key -> Map.get(post, String.to_atom(key)) - end) - end) - |> Enum.join("\n") - |> (fn items -> (Regex.compile!("{{items}}") |> - Regex.replace(post_feed_template, fn _, __ -> items end)) - end).() - -File.open!("./gen/rss.xml", [:write]) - |> IO.binwrite(post_feed) - |> File.close() diff --git a/client/public/templates/post_index_page.html b/client/templates/post_index_page.html similarity index 100% rename from client/public/templates/post_index_page.html rename to client/templates/post_index_page.html diff --git a/client/public/templates/post_item.xml b/client/templates/post_item.xml similarity index 100% rename from client/public/templates/post_item.xml rename to client/templates/post_item.xml diff --git a/client/public/templates/post_single_page.html b/client/templates/post_single_page.html similarity index 100% rename from client/public/templates/post_single_page.html rename to client/templates/post_single_page.html diff --git a/client/public/templates/posts.xml b/client/templates/posts.xml similarity index 100% rename from client/public/templates/posts.xml rename to client/templates/posts.xml diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..2201185 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,8 @@ +version: "3.3" +services: + client: + build: client/ + ports: + - "5010:80" + volumes: + - "./client/public:/app:rw"