Packages
tableau
0.7.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/mix/tasks/tableau.build.ex
defmodule Mix.Tasks.Tableau.Build do
use Mix.Task
require Logger
@moduledoc "Task to build the tableau site"
@shortdoc "Builds the site"
@config Application.compile_env(:tableau, :config, %{}) |> Map.new()
@impl Mix.Task
def run(argv) do
{:ok, config} = Tableau.Config.new(@config)
token = %{site: %{config: config}}
Mix.Task.run("app.start", ["--preload-modules"])
{opts, _argv} = OptionParser.parse!(argv, strict: [out: :string])
out = Keyword.get(opts, :out, "_site")
mods = :code.all_available()
token =
for module <- pre_build_extensions(mods), reduce: token do
token ->
config_mod = Module.concat([module, Config])
raw_config =
Application.get_env(:tableau, module, %{}) |> Map.new()
if raw_config[:enabled] do
{:ok, config} =
raw_config
|> config_mod.new()
{:ok, key} = Tableau.Extension.key(module)
token = put_in(token[key], config)
case module.run(token) do
{:ok, token} ->
token
:error ->
Logger.error("#{inspect(module)} failed to run")
token
end
else
token
end
end
mods = :code.all_available()
graph = Tableau.Graph.new(mods)
File.mkdir_p!(out)
pages =
for mod <- Graph.vertices(graph), {:ok, :page} == Tableau.Graph.Node.type(mod) do
{mod, Map.new(mod.__tableau_opts__() || [])}
end
{page_mods, pages} = Enum.unzip(pages)
token = put_in(token.site[:pages], pages)
for mod <- page_mods do
content = Tableau.Document.render(graph, mod, token)
permalink = mod.__tableau_permalink__()
dir = Path.join(out, permalink)
File.mkdir_p!(dir)
File.write!(Path.join(dir, "index.html"), content)
end
if File.exists?(config.include_dir) do
File.cp_r!(config.include_dir, out)
end
for module <- post_write_extensions(mods), reduce: token do
token ->
config_mod = Module.concat([module, Config])
raw_config =
Application.get_env(:tableau, module, %{}) |> Map.new()
if raw_config[:enabled] do
{:ok, config} =
raw_config
|> config_mod.new()
{:ok, key} = Tableau.Extension.key(module)
token = put_in(token[key], config)
case module.run(token) do
{:ok, token} ->
token
:error ->
Logger.error("#{inspect(module)} failed to run")
token
end
else
token
end
end
end
defp pre_build_extensions(modules) do
for {mod, _, _} <- modules,
mod = Module.concat([to_string(mod)]),
match?({:ok, :pre_build}, Tableau.Extension.type(mod)) do
mod
end
|> Enum.sort_by(& &1.__tableau_extension_priority__())
end
defp post_write_extensions(modules) do
for {mod, _, _} <- modules,
mod = Module.concat([to_string(mod)]),
match?({:ok, :post_write}, Tableau.Extension.type(mod)) do
mod
end
|> Enum.sort_by(& &1.__tableau_extension_priority__())
end
end