add helpers, fix watch
This commit is contained in:
parent
8bc094afb8
commit
f743cca9fe
8 changed files with 48 additions and 67 deletions
|
@ -6,6 +6,7 @@ defmodule SloaneSH.Assets.Markdown do
|
||||||
type = Keyword.fetch!(opts, :type)
|
type = Keyword.fetch!(opts, :type)
|
||||||
|
|
||||||
quote do
|
quote do
|
||||||
|
import SloaneSH.Layouts.Helpers, warn: false
|
||||||
alias SloaneSH.Asset
|
alias SloaneSH.Asset
|
||||||
alias SloaneSH.FrontMatter
|
alias SloaneSH.FrontMatter
|
||||||
alias SloaneSH.Layouts
|
alias SloaneSH.Layouts
|
||||||
|
@ -49,7 +50,7 @@ defmodule SloaneSH.Assets.Markdown do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_render(ctx, {path, ".eex"}, data, attrs) do
|
defp do_render(ctx, {path, ".eex"}, data, attrs) do
|
||||||
eexed = EEx.eval_string(data, ctx: ctx, attrs: attrs)
|
eexed = eval_eex(data, "#{path}.eex", ctx, attrs)
|
||||||
do_render(ctx, base_and_ext(path), eexed, attrs)
|
do_render(ctx, base_and_ext(path), eexed, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,6 +69,15 @@ defmodule SloaneSH.Assets.Markdown do
|
||||||
{base, ext}
|
{base, ext}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp eval_eex(template, file, ctx, attrs) do
|
||||||
|
{result, _binding} =
|
||||||
|
template
|
||||||
|
|> EEx.compile_string(file: file)
|
||||||
|
|> Code.eval_quoted([ctx: ctx, attrs: attrs], __ENV__)
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
def handle_attrs(cfg, path, data, attrs) do
|
def handle_attrs(cfg, path, data, attrs) do
|
||||||
attrs
|
attrs
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,8 @@ defmodule SloaneSH.Layouts do
|
||||||
`EEx` based layouts
|
`EEx` based layouts
|
||||||
"""
|
"""
|
||||||
require EEx
|
require EEx
|
||||||
import SloaneSH.Layouts.Partials
|
import SloaneSH.Layouts.Partials, warn: false
|
||||||
|
import SloaneSH.Layouts.Helpers, warn: false
|
||||||
|
|
||||||
@layouts_dir Path.join(:code.priv_dir(:sloane_sh), "site/layouts")
|
@layouts_dir Path.join(:code.priv_dir(:sloane_sh), "site/layouts")
|
||||||
|
|
||||||
|
|
19
lib/sloane_sh/layouts/helpers.ex
Normal file
19
lib/sloane_sh/layouts/helpers.ex
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
defmodule SloaneSH.Layouts.Helpers do
|
||||||
|
alias SloaneSH.Context
|
||||||
|
|
||||||
|
def cx(classes) do
|
||||||
|
classes
|
||||||
|
|> Enum.map(fn
|
||||||
|
{_, _} = t -> t
|
||||||
|
c -> {c, true}
|
||||||
|
end)
|
||||||
|
|> Enum.filter(fn {_, v} -> !!v end)
|
||||||
|
|> Enum.map_join(" ", fn {class, _} -> class end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def sorted_post_attrs(%Context{} = ctx) do
|
||||||
|
ctx.posts
|
||||||
|
|> Enum.map(& &1.attrs)
|
||||||
|
|> Enum.sort_by(& &1[:date], &Date.compare/2)
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,6 +3,7 @@ defmodule SloaneSH.Layouts.Partials do
|
||||||
HTML partials for use in HTML layouts
|
HTML partials for use in HTML layouts
|
||||||
"""
|
"""
|
||||||
require EEx
|
require EEx
|
||||||
|
import SloaneSH.Layouts.Helpers, warn: false
|
||||||
|
|
||||||
EEx.function_from_string(
|
EEx.function_from_string(
|
||||||
:def,
|
:def,
|
||||||
|
@ -21,14 +22,4 @@ defmodule SloaneSH.Layouts.Partials do
|
||||||
""",
|
""",
|
||||||
[:ctx, :attrs]
|
[:ctx, :attrs]
|
||||||
)
|
)
|
||||||
|
|
||||||
defp cx(classes) do
|
|
||||||
classes
|
|
||||||
|> Enum.map(fn
|
|
||||||
{_, _} = t -> t
|
|
||||||
c -> {c, true}
|
|
||||||
end)
|
|
||||||
|> Enum.filter(fn {_, v} -> !!v end)
|
|
||||||
|> Enum.map_join(" ", fn {class, _} -> class end)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
defmodule SloaneSH.Markdown do
|
|
||||||
@moduledoc """
|
|
||||||
Markdown parsing using `Earmark` and `Earmark.Parser`
|
|
||||||
"""
|
|
||||||
require Logger
|
|
||||||
use TypedStruct
|
|
||||||
|
|
||||||
alias SloaneSH.Context
|
|
||||||
alias SloaneSH.FrontMatter
|
|
||||||
alias __MODULE__
|
|
||||||
|
|
||||||
typedstruct do
|
|
||||||
field :attrs, map(), default: %{}
|
|
||||||
field :html, String.t(), default: ""
|
|
||||||
field :text, String.t(), default: ""
|
|
||||||
end
|
|
||||||
|
|
||||||
def transform(%Context{} = ctx, data) when is_binary(data) do
|
|
||||||
with {:ok, attrs, body} <- FrontMatter.parse(data, ctx),
|
|
||||||
{:ok, html, msgs} <- Earmark.as_html(body),
|
|
||||||
{:ok, html_tree} <- Floki.parse_fragment(html) do
|
|
||||||
for msg <- msgs, do: Logger.warning(msg)
|
|
||||||
|
|
||||||
{:ok,
|
|
||||||
%Markdown{
|
|
||||||
attrs: attrs,
|
|
||||||
html: html,
|
|
||||||
text: Floki.text(html_tree, sep: " ")
|
|
||||||
}}
|
|
||||||
else
|
|
||||||
{:error, _, msgs} ->
|
|
||||||
for msg <- msgs, do: Logger.error(msg)
|
|
||||||
:error
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
:error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -52,24 +52,14 @@ defmodule SloaneSH.Watch do
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl GenServer
|
@impl GenServer
|
||||||
def handle_info({:file_event, pid, {path, events}}, %{ctx: ctx, watcher_pid: pid} = state) do
|
def handle_info({:file_event, pid, {path, _events}}, %{ctx: ctx, watcher_pid: pid} = state) do
|
||||||
if String.match?(path, ~r/layouts/) do
|
if String.match?(path, ~r/layouts/) do
|
||||||
recompile_layouts()
|
recompile_layouts()
|
||||||
Build.run(ctx)
|
|
||||||
{:noreply, state}
|
|
||||||
else
|
|
||||||
ctx = Context.maybe_add(ctx, path)
|
|
||||||
|
|
||||||
if Context.in_context?(ctx, path) do
|
|
||||||
path = Path.relative_to(path, ctx.config.pages_dir)
|
|
||||||
Logger.info("File changed: #{path}")
|
|
||||||
Build.build_page(ctx, path)
|
|
||||||
end
|
|
||||||
|
|
||||||
%{state | ctx: ctx}
|
|
||||||
|
|
||||||
{:noreply, state}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Build.run(ctx)
|
||||||
|
|
||||||
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl GenServer
|
@impl GenServer
|
||||||
|
@ -79,9 +69,10 @@ defmodule SloaneSH.Watch do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp recompile_layouts do
|
defp recompile_layouts do
|
||||||
|
helpers_source = Layouts.Helpers.module_info(:compile)[:source] |> List.to_string()
|
||||||
partials_source = Layouts.Partials.module_info(:compile)[:source] |> List.to_string()
|
partials_source = Layouts.Partials.module_info(:compile)[:source] |> List.to_string()
|
||||||
layouts_source = Layouts.module_info(:compile)[:source] |> List.to_string()
|
layouts_source = Layouts.module_info(:compile)[:source] |> List.to_string()
|
||||||
{:ok, _, _} = Kernel.ParallelCompiler.compile([partials_source, layouts_source])
|
{:ok, _, _} = Kernel.ParallelCompiler.compile([helpers_source, partials_source, layouts_source])
|
||||||
|
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
+++
|
+++
|
||||||
page_title = "home"
|
page_title = "home"
|
||||||
|
permalink = "/"
|
||||||
+++
|
+++
|
||||||
|
|
||||||
hey, i'm sloane! i'm a professional software engineer and an amateur musician, photographer, wife, chef, and pet mom.
|
hey, i'm sloane! i'm a professional software engineer and an amateur musician, photographer, wife, chef, and pet mom.
|
7
priv/site/pages/posts.html.eex
Normal file
7
priv/site/pages/posts.html.eex
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<ul>
|
||||||
|
<%= for post <- sorted_post_attrs(ctx) do %>
|
||||||
|
<li>
|
||||||
|
<a href="<%= post[:permalink] %>"><%= post[:title] %></a>
|
||||||
|
</li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
Loading…
Reference in a new issue