Packages
tableau
0.15.1
0.30.0
0.29.0
0.28.0
0.27.1
0.27.0
0.26.1
0.26.0
0.25.1
0.25.0
0.24.1
0.24.0
0.23.1
0.23.0
0.22.0
0.21.0
0.20.1
0.20.0
0.19.0
0.18.0
0.17.1
0.17.0
0.16.0
0.15.3
0.15.2
0.15.1
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.0
0.12.0
0.11.1
0.11.0
0.10.1
0.10.0
0.9.0
0.8.0
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.1
0.3.0
0.2.0
0.1.2
0.1.1
0.1.0
Static site generator for elixir
Current section
Files
Jump to
Current section
Files
lib/tableau/extensions/post_extension/posts/post.ex
defmodule Tableau.PostExtension.Posts.Post do
@moduledoc false
def build(filename, attrs, body) do
{:ok, post_config} =
Tableau.PostExtension.Config.new(Map.new(Application.get_env(:tableau, Tableau.PostExtension, %{})))
Application.put_env(:date_time_parser, :include_zones_from, ~N[2010-01-01T00:00:00])
attrs
|> Map.put(:__tableau_post_extension__, true)
|> Map.put(:body, body)
|> Map.put(:file, filename)
|> Map.put(:layout, Module.concat([attrs[:layout] || post_config.layout]))
|> Map.put_new_lazy(:title, fn ->
with {:ok, document} <- Floki.parse_fragment(body),
[hd | _] <- Floki.find(document, "h1") do
Floki.text(hd)
else
_ -> nil
end
end)
|> Map.put(:date, DateTimeParser.parse_datetime!(attrs.date, assume_time: true, assume_utc: true))
|> build_permalink(post_config)
end
def parse(_file_path, content) do
Tableau.YamlFrontMatter.parse!(content, atoms: true)
end
defp build_permalink(%{permalink: permalink} = attrs, _config) do
permalink
|> transform_permalink(attrs)
|> then(&Map.put(attrs, :permalink, &1))
end
defp build_permalink(attrs, %{permalink: permalink}) when not is_nil(permalink) do
permalink
|> transform_permalink(attrs)
|> then(&Map.put(attrs, :permalink, &1))
end
defp build_permalink(%{file: filename} = attrs, _) do
filename
|> Path.rootname()
|> transform_permalink(attrs)
|> then(&Map.put(attrs, :permalink, &1))
end
defp transform_permalink(path, attrs) do
vars =
attrs
|> Map.new(fn {k, v} -> {":#{k}", v} end)
|> Map.merge(%{
":day" => attrs.date.day |> to_string() |> String.pad_leading(2, "0"),
":month" => attrs.date.month |> to_string() |> String.pad_leading(2, "0"),
":year" => attrs.date.year
})
path
|> String.replace(Map.keys(vars), &to_string(Map.fetch!(vars, &1)))
|> String.replace(" ", "-")
|> String.replace("_", "-")
|> String.replace(~r/[^[:alnum:]\/\-.]/, "")
|> String.downcase()
end
end