Skip to content

Commit

Permalink
Fix layout names being stored with file extensions (#9)
Browse files Browse the repository at this point in the history
* Fix layout names in templates

This commit fixes an issue with layout names for regular and partial
layouts that that forced users to refer to partials using quoted
atoms (e.g. `@partials."footer.html"`) due to the way we were parsing
layout names. For files that use multiple extensions like `.html.eex`,
it is now possible to refer to them using their name without any
extension.

In content, pages can now also refer to the layout just as `layout_name`
instead of including the file extensions of the layout.

* update plug_live_reload dependency
  • Loading branch information
goncalotomas authored Dec 14, 2023
1 parent ddf3deb commit 9208345
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion guides/working_with_templates/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To denote that a piece of content should be wrapped in a template, use the layou
layout: cool_layout
title: My Cool Griffin Blog Post
---
# <%= title %>
# <%= @title %>
```

This will look for a `cool_layout.eex` EEx file in your *layouts* folder at `lib/layouts/cool_layout.eex`.
Expand Down
35 changes: 29 additions & 6 deletions lib/griffin_ssg/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ defmodule GriffinSSG.Layouts do
layout_partials_dir = layouts_dir <> "/partials"

layout_files = GriffinSSG.Filesystem.search_directory(layouts_dir, @layout_extnames)
layout_names = Enum.map(layout_files, &Path.basename(&1, Path.extname(&1)))

layout_names =
Enum.map(layout_files, fn filename ->
filename
|> Path.basename()
|> String.split(".")
|> hd()
end)

num_layouts = length(layout_files)

result =
Expand All @@ -48,9 +56,19 @@ defmodule GriffinSSG.Layouts do
# compile partials
partials =
Enum.reduce(partial_layouts, %{}, fn filepath, acc ->
# Using Path.extname/1 doesn't work here because layout files
# can use multiple extensions (e.g. root.html.eex).
# Additionally, for partials, we specifically convert the name
# into an atom so it can be referred in a layout as @partials.name.
layout_name =
Path.basename(filepath)
|> String.split(".")
|> hd()
|> String.to_atom()

Map.put(
acc,
String.to_atom(Path.basename(filepath, Path.extname(filepath))),
layout_name,
EEx.compile_file(filepath)
)
end)
Expand Down Expand Up @@ -134,7 +152,12 @@ defmodule GriffinSSG.Layouts do
defp compile_layouts_rec([], not_compiled, _layout_names), do: not_compiled

defp compile_layouts_rec([file | remaining], acc, layout_names) do
layout_name = Path.basename(file, Path.extname(file))
# Using Path.extname/1 doesn't work here because layout files
# can use multiple extensions (e.g. root.html.eex)
layout_name =
Path.basename(file)
|> String.split(".")
|> hd()

layout =
file
Expand Down Expand Up @@ -171,9 +194,9 @@ defmodule GriffinSSG.Layouts do
if parent_layout == nil do
{:error, :parent_layout_not_found}
else
# there is currently no better way of doing this that I know of,
# since compiled or eval'ed EEx strings replace all variables
# and we only want to replace @content.
# refactor: there is currently no better way of doing this
# that I know of, since compiled or eval'ed EEx strings
# replace all variables and we only want to replace @content.
# This isn't ideal because users might use different spacing
# which wouldn't work with the way we're merging the layouts.
content_patterns = [
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Griffin.MixProject do
{:earmark, "~> 1.4"},
{:file_system, "~> 1.0"},
{:plug_cowboy, "~> 2.6"},
{:plug_live_reload, github: "goncalotomas/plug_live_reload"},
{:plug_live_reload, "~> 0.2"},
{:slugify, "~> 1.3"},
{:yaml_elixir, "~> 2.9"},

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"plug": {:hex, :plug, "1.15.2", "94cf1fa375526f30ff8770837cb804798e0045fd97185f0bb9e5fcd858c792a3", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "02731fa0c2dcb03d8d21a1d941bdbbe99c2946c0db098eee31008e04c6283615"},
"plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"},
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
"plug_live_reload": {:git, "https://github.com/goncalotomas/plug_live_reload.git", "19c66405f5f4ef371807fbb02f208080e2a95fa4", []},
"plug_live_reload": {:hex, :plug_live_reload, "0.2.0", "61154a657ad48e856fe9c3056080c2ececcfa236793f2de98185fbe291c1333e", [:mix], [{:cowboy, "~> 2.10", [hex: :cowboy, repo: "hexpm", optional: false]}, {:file_system, "~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ff953f55b7eeacf832f77800824b60a14818dc2749b61b5e6e07adef6de5d93a"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"slugify": {:hex, :slugify, "1.3.1", "0d3b8b7e5c1eeaa960e44dce94382bee34a39b3ea239293e457a9c5b47cc6fd3", [:mix], [], "hexpm", "cb090bbeb056b312da3125e681d98933a360a70d327820e4b7f91645c4d8be76"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
Expand Down

0 comments on commit 9208345

Please sign in to comment.