Packages
tableau
0.25.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/common.ex
defmodule Tableau.Extension.Common do
@moduledoc false
@doc """
Expand content paths from a wildcard.
"""
def paths(wildcard) do
wildcard |> Path.wildcard() |> Enum.sort()
end
@doc """
Build content entries from a list of paths.
Content should contain YAML frontmatter, with the body from the file passed to the callback.
"""
def entries(paths, callback) do
for path <- paths do
{front_matter, body} = Tableau.YamlFrontMatter.parse!(File.read!(path), atoms: true)
"." <> ext = Path.extname(path)
callback.(%{path: path, ext: String.to_atom(ext), front_matter: front_matter, pre_convert_body: body})
end
end
@doc """
Builds a permalink from a template and frontmatter and/or config.
Frontmatter keys are substituted for colon prefixed template keys of the same name.
If no permalink template is provided, the permalink will be derived from the file path.
If the frontamtter contains a `:date` key, it is broken down into `:day`, `:month`, and `:year`
components and those can be used in the permalink template.
"""
def build_permalink(%{permalink: permalink} = front_matter, _config) do
permalink
|> transform_permalink(front_matter)
|> then(&Map.put(front_matter, :permalink, &1))
end
def build_permalink(front_matter, %{permalink: permalink}) when not is_nil(permalink) do
permalink
|> transform_permalink(front_matter)
|> then(&Map.put(front_matter, :permalink, &1))
end
def build_permalink(%{file: filename} = front_matter, config) do
filename
|> Path.rootname()
|> then(fn rootname ->
for dir <- List.wrap(config.dir), reduce: rootname do
rootname ->
String.replace_prefix(rootname, dir, "")
end
end)
|> transform_permalink(front_matter)
|> then(&Map.put(front_matter, :permalink, &1))
end
defp transform_permalink(path, front_matter) do
vars =
front_matter
|> Map.new(fn {k, v} -> {":#{k}", v} end)
|> then(fn vars ->
if is_struct(front_matter[:date], DateTime) do
Map.merge(vars, %{
":day" => front_matter.date.day |> to_string() |> String.pad_leading(2, "0"),
":month" => front_matter.date.month |> to_string() |> String.pad_leading(2, "0"),
":year" => front_matter.date.year
})
else
vars
end
end)
String.replace(path, Map.keys(vars), fn key ->
vars |> Map.fetch!(key) |> to_string() |> Slug.slugify(ignore: ["."])
end)
end
end