From 03a403ae6a5170b55ff934f262379a6a63b6a2d9 Mon Sep 17 00:00:00 2001 From: Gabriel Poca Date: Fri, 23 Apr 2021 18:06:07 +0100 Subject: [PATCH] Add temple and update spec for Preprocessor --- lib/still/preprocessor.ex | 36 ++++++++++++++++++++++++++------ lib/still/preprocessor/temple.ex | 11 ++++++++++ mix.exs | 1 + mix.lock | 3 ++- priv/site/_includes/main.exs | 31 +++++++++++++++++++++++++++ priv/site/_includes/main.slime | 22 ------------------- priv/site/_includes/marquee.exs | 17 +++++++++++++++ priv/site/index.slime | 2 +- 8 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 lib/still/preprocessor/temple.ex create mode 100644 priv/site/_includes/main.exs delete mode 100644 priv/site/_includes/main.slime create mode 100644 priv/site/_includes/marquee.exs diff --git a/lib/still/preprocessor.ex b/lib/still/preprocessor.ex index 65677656..bce8d0bb 100644 --- a/lib/still/preprocessor.ex +++ b/lib/still/preprocessor.ex @@ -51,13 +51,15 @@ defmodule Still.Preprocessor do Save, AddLayout, AddContent, - Image + Image, + Temple } @default_preprocessors %{ ".slim" => [AddContent, EEx, Frontmatter, Slime, OutputPath, AddLayout, Save], ".slime" => [AddContent, EEx, Frontmatter, Slime, OutputPath, AddLayout, Save], ".eex" => [AddContent, EEx, Frontmatter, OutputPath, AddLayout, Save], + ".exs" => [AddContent, Frontmatter, Temple, EEx, OutputPath, AddLayout, Save], ".css" => [AddContent, EEx, CSSMinify, OutputPath, URLFingerprinting, AddLayout, Save], ".js" => [AddContent, EEx, JS, OutputPath, URLFingerprinting, AddLayout, Save], ".md" => [AddContent, EEx, Frontmatter, Markdown, OutputPath, AddLayout, Save], @@ -141,11 +143,33 @@ defmodule Still.Preprocessor do end) end - @callback render(SourceFile.t()) :: SourceFile.t() + @type render_result :: + {:cont, SourceFile.t()} + | {:halt, SourceFile.t()} + | SourceFile.t() + + @callback render(SourceFile.t()) :: render_result() + @callback after_render(SourceFile.t()) :: SourceFile.t() @optional_callbacks render: 1, after_render: 1 + @doc """ + Identity function to silence pattern matching warnings from + Dialyzer. + + Each preprocessor implements the `run/1` and `render/1` functions, so Dialyzer + knows whether all the branches in `case` statement are essential. They never + are, and most processors only return one type. So there's always going to a + warning there that's not helpful. Using this identity function that's only + defined once, we force Dialyzer to accept that those branches in `case` are + necessary. + """ + @spec handle_return(result :: render_result()) :: render_result() + def handle_return(value) do + value + end + defmacro __using__(_opts) do quote do @behaviour Still.Preprocessor @@ -162,6 +186,7 @@ defmodule Still.Preprocessor do def run(source_file, next_preprocessors) do source_file |> render() + |> Still.Preprocessor.handle_return() |> case do {:cont, source_file} -> source_file @@ -200,7 +225,7 @@ defmodule Still.Preprocessor do Returns the resulting #{Still.SourceFile}. """ - @spec after_render(SourceFile.t()) :: SourceFile.t() + @impl true def after_render(source_file), do: source_file @doc """ @@ -208,11 +233,10 @@ defmodule Still.Preprocessor do Returns the resulting #{Still.SourceFile}. """ - @spec render(SourceFile.t()) :: - {:cont, SourceFile.t()} | {:halt, SourceFile.t()} | SourceFile.t() + @impl true def render(source_file), do: source_file - defoverridable render: 1, after_render: 1 + defoverridable(Still.Preprocessor) end end end diff --git a/lib/still/preprocessor/temple.ex b/lib/still/preprocessor/temple.ex new file mode 100644 index 00000000..dae8781d --- /dev/null +++ b/lib/still/preprocessor/temple.ex @@ -0,0 +1,11 @@ +defmodule Still.Preprocessor.Temple do + alias Still.{Preprocessor, SourceFile} + + use Preprocessor + + @impl true + def render(%{content: content} = file) do + {content, _} = Code.eval_string(content, [], __ENV__) + %SourceFile{file | content: content, extension: ".html"} + end +end diff --git a/mix.exs b/mix.exs index 2a68bb99..33bcb346 100644 --- a/mix.exs +++ b/mix.exs @@ -71,6 +71,7 @@ defmodule Still.MixProject do {:plug, "~> 1.10"}, {:plug_cowboy, "~> 2.3"}, {:slime, "~> 1.2"}, + {:temple, "~> 0.6.0-rc.0"}, {:timex, "~> 3.5"}, {:yaml_elixir, "~> 2.4"} ] diff --git a/mix.lock b/mix.lock index e5d06f14..a8059335 100644 --- a/mix.lock +++ b/mix.lock @@ -6,7 +6,7 @@ "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"}, "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"}, "credo": {:hex, :credo, "1.5.5", "e8f422026f553bc3bebb81c8e8bf1932f498ca03339856c7fec63d3faac8424b", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "dd8623ab7091956a855dc9f3062486add9c52d310dfd62748779c4315d8247de"}, - "dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"}, + "dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"}, "earmark_parser": {:hex, :earmark_parser, "1.4.10", "6603d7a603b9c18d3d20db69921527f82ef09990885ed7525003c7fe7dc86c56", [:mix], [], "hexpm", "8e2d5370b732385db2c9b22215c3f59c84ac7dda7ed7e544d7c459496ae519c0"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"}, @@ -40,6 +40,7 @@ "slime": {:hex, :slime, "1.2.1", "71e036056051f0a6fae136af34eaa1322e8e11cdd2da3a56196fd31bca34dd49", [:mix], [{:neotoma, "~> 1.7", [hex: :neotoma, repo: "hexpm", optional: false]}], "hexpm", "298568e64291fed4eb690be094f6c46400daa03b594bab34fcaa0167e139c263"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"}, + "temple": {:hex, :temple, "0.6.0-rc.0", "2febf687a87901629e9ad994200a55e0d23b700413e7cb0afc26716ad485b52f", [:mix], [{:phoenix, ">= 0.0.0", [hex: :phoenix, repo: "hexpm", optional: true]}], "hexpm", "408988b116a5f7f53ae822aa844e7f6dae28a21e4038e21b5fcddc51493e5c0d"}, "timex": {:hex, :timex, "3.6.2", "845cdeb6119e2fef10751c0b247b6c59d86d78554c83f78db612e3290f819bc2", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "26030b46199d02a590be61c2394b37ea25a3664c02fafbeca0b24c972025d47a"}, "tzdata": {:hex, :tzdata, "1.0.5", "69f1ee029a49afa04ad77801febaf69385f3d3e3d1e4b56b9469025677b89a28", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "55519aa2a99e5d2095c1e61cc74c9be69688f8ab75c27da724eb8279ff402a5a"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, diff --git a/priv/site/_includes/main.exs b/priv/site/_includes/main.exs new file mode 100644 index 00000000..e6dec4b7 --- /dev/null +++ b/priv/site/_includes/main.exs @@ -0,0 +1,31 @@ +use Temple + +temple do + div class: "main" do + div class: "content" do + h1 class: "fancy-title" do + div class: "visually-hidden" do + "Still" + end + end + + div class: "logo-container" do + responsive_image("_includes/logo.png", class: "logo", aria_hidden: true) + end + + p class: "lead" do + "A composable elixir static site builder" + end + + div class: "info" do + link(@env, "Docs", to: "https://hexdocs.pm/still", class: "cta") + |> to_string() + link(@env, "GitHub", to: "https://github.com/still-ex/still", class: "cta") + |> to_string() + end + end + include(@env, "_includes/marquee.exs") + div class: "overlay" + responsive_image("_includes/main.jpg", class: "cover", aria_hidden: true) + end +end diff --git a/priv/site/_includes/main.slime b/priv/site/_includes/main.slime deleted file mode 100644 index e3dcf138..00000000 --- a/priv/site/_includes/main.slime +++ /dev/null @@ -1,22 +0,0 @@ -.main - .content - h1.fancy-title - .visually-hidden Still - - .logo-container - = responsive_image("_includes/logo.png", class: "logo", aria_hidden: true) - - p.lead A composable elixir static site builder - - .info - = link @env, "Docs", to: "https://hexdocs.pm/still", class: "cta" - = link @env, "GitHub", to: "https://github.com/still-ex/still", class: "cta" - .marquee - .content - span Tonight on display: still (featuring elixir) - span Tonight on display: still (featuring elixir) - span Tonight on display: still (featuring elixir) - span Tonight on display: still (featuring elixir) - span Tonight on display: still (featuring elixir) - .overlay - = responsive_image("_includes/main.jpg", class: "cover", aria_hidden: true) diff --git a/priv/site/_includes/marquee.exs b/priv/site/_includes/marquee.exs new file mode 100644 index 00000000..6575dcd4 --- /dev/null +++ b/priv/site/_includes/marquee.exs @@ -0,0 +1,17 @@ +--- +text: "Tonight on display: still (featuring elixir)" +--- + +use Temple + +temple do + div class: "marquee" do + div class: "content" do + for _ <- 1..5 do + span do + @text + end + end + end + end +end diff --git a/priv/site/index.slime b/priv/site/index.slime index ac0f44bb..42f29550 100644 --- a/priv/site/index.slime +++ b/priv/site/index.slime @@ -2,7 +2,7 @@ layout: _layout.slime --- -= include(@env, "_includes/main.slime") += include(@env, "_includes/main.exs") = include(@env, "_includes/manifesto.slime") = include(@env, "_includes/getting_started.slime") = include(@env, "_includes/community.slime")