Extract template filling: pair usage consistant

master
Raymonzut 4 years ago
parent 2a128f49ab
commit e9d50f1392
No known key found for this signature in database
GPG Key ID: 1E9BCC39EDD1DD53
  1. 45
      client/public/gen.ex

@ -5,6 +5,22 @@ post_template = File.read!("./templates/post_single_page.html")
post_feed_item_template = File.read!("./templates/post_item.xml") post_feed_item_template = File.read!("./templates/post_item.xml")
post_feed_template = File.read!("./templates/posts.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 -> ("<p>" <> paragraph <> "</p>\n") end)
|> Enum.join("")
end
end)
end
fill_template_pretemplated = fn template -> &fill_template.(template, &1) end
post_contents = File.ls!("./posts") post_contents = File.ls!("./posts")
|> Enum.reject(fn(x) -> String.starts_with?(x, ".") end) |> 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/" <> f) end)
@ -26,43 +42,28 @@ index_file = post_contents
Enum.map(fn post -> "<h2>" <> (Map.get(post, :date) |> String.slice(0..9)) <> Enum.map(fn post -> "<h2>" <> (Map.get(post, :date) |> String.slice(0..9)) <>
" - <a href=\"" <> Map.get(post, :id) <> ".html\">" <> Map.get(post, :title) <> "</a></h2>" " - <a href=\"" <> Map.get(post, :id) <> ".html\">" <> Map.get(post, :title) <> "</a></h2>"
end) |> Enum.join("\n")) end) end) |> Enum.join("\n")) end)
|> (fn template -> Regex.replace(Regex.compile!("{{index}}"), index_template, fn _, __ -> template end) end).() |> Enum.join("\n")
|> (fn v -> %{index: v} end).()
|> fill_template_pretemplated.(index_template).()
File.open!("./gen/index.html", [:write]) File.open!("./gen/index.html", [:write])
|> IO.binwrite(index_file) |> IO.binwrite(index_file)
|> File.close() |> File.close()
post_contents post_contents
|> Enum.each(fn post -> |> Enum.each(fn post ->
File.open!("./gen/" <> Map.get(post, :id) <> ".html", [:write]) File.open!("./gen/" <> Map.get(post, :id) <> ".html", [:write])
|> IO.binwrite( |> IO.binwrite(fill_template.(post_template, post))
# Converting handlebars to values
Regex.compile!("{{(.*)}}") |>
Regex.replace(post_template, fn _, key -> if key != "content" do Map.get(post, String.to_atom(key)) else
Map.get(post, String.to_atom(key))
# Converting \n to paragraphs
|> String.split("\\n")
|> Enum.reject(fn(x) -> x == "" end)
|> Enum.map(fn paragraph -> ("<p>" <> paragraph <> "</p>\n") end)
|> Enum.join("")
end
end))
|> File.close() |> File.close()
end) end)
post_feed = post_contents post_feed = post_contents
|> Enum.sort_by(fn m -> Map.get(m, :date) end) |> Enum.sort_by(fn m -> Map.get(m, :date) end)
|> Enum.reverse() |> Enum.reverse()
|> Enum.map(fn post -> |> Enum.map(fill_template_pretemplated.(post_feed_item_template))
Regex.compile!("{{(.*)}}") |>
Regex.replace(post_feed_item_template, fn _, key -> Map.get(post, String.to_atom(key))
end)
end)
|> Enum.join("\n") |> Enum.join("\n")
|> (fn items -> (Regex.compile!("{{items}}") |> |> (fn v -> %{items: v} end).()
Regex.replace(post_feed_template, fn _, __ -> items end)) |> fill_template_pretemplated.(post_feed_template).()
end).()
File.open!("./gen/rss.xml", [:write]) File.open!("./gen/rss.xml", [:write])
|> IO.binwrite(post_feed) |> IO.binwrite(post_feed)

Loading…
Cancel
Save