Skip to content

Commit

Permalink
Fix broken codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dalgona committed Feb 4, 2020
1 parent 19f53cb commit f8dd58c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 43 deletions.
11 changes: 4 additions & 7 deletions lib/serum/build/file_processor/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Serum.Build.FileProcessor.Template do

require Serum.Result, as: Result
import Serum.IOProxy, only: [put_msg: 2]
alias Serum.Error
alias Serum.Template
alias Serum.Template.Compiler, as: TC
alias Serum.Template.Storage, as: TS
Expand All @@ -22,13 +21,11 @@ defmodule Serum.Build.FileProcessor.Template do

@spec compile_and_load([Serum.File.t()], Template.type()) :: Result.t([Template.t()])
defp compile_and_load(files, type) do
case TC.compile_files(files, type: type) do
{:ok, result} ->
TS.load(result, type)
result |> Enum.map(&elem(&1, 1)) |> expand_includes()
Result.run do
result <- TC.compile_files(files, type: type)
TS.load(result, type)

{:error, %Error{}} = error ->
error
result |> Enum.map(&elem(&1, 1)) |> expand_includes()
end
end

Expand Down
13 changes: 8 additions & 5 deletions lib/serum/build/page_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Serum.Build.PageGenerator do

_moduledocp = "A module responsible for rendering complete HTML pages."

require Serum.Result, as: Result
import Serum.IOProxy, only: [put_msg: 2]
alias Serum.Error
alias Serum.Fragment
Expand All @@ -16,12 +17,14 @@ defmodule Serum.Build.PageGenerator do
def run(fragments) do
put_msg(:info, "Generating complete HTML pages...")

template = TS.get("base", :template)
Result.run do
template <- TS.get("base", :template)

fragments
|> Task.async_stream(&render(&1, template))
|> Enum.map(&elem(&1, 1))
|> Result.aggregate("failed to render HTML pages:")
fragments
|> Task.async_stream(&render(&1, template))
|> Enum.map(&elem(&1, 1))
|> Result.aggregate("failed to render HTML pages:")
end
end

@spec render(Fragment.t(), Template.t()) :: Result.t(Serum.File.t())
Expand Down
11 changes: 4 additions & 7 deletions lib/serum/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ defmodule Serum.Page do
}

require Serum.Result, as: Result
alias Serum.Error
alias Serum.Fragment
alias Serum.Renderer
alias Serum.Template
alias Serum.Template.Storage, as: TS

defstruct [
Expand Down Expand Up @@ -100,12 +98,11 @@ defmodule Serum.Page do
template_name = page.template || "page"
bindings = [page: metadata, contents: page.data]

with %Template{} = template <- TS.get(template_name, :template),
{:ok, html} <- Renderer.render_fragment(template, bindings) do
Result.run do
template <- TS.get(template_name, :template)
html <- Renderer.render_fragment(template, bindings)

Fragment.new(page.file, page.output, metadata, html)
else
nil -> Result.fail(Simple, ["the template \"#{template_name}\" is not available"])
{:error, %Error{}} = error -> error
end
end

Expand Down
11 changes: 4 additions & 7 deletions lib/serum/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ defmodule Serum.Post do
"""

require Serum.Result, as: Result
alias Serum.Error
alias Serum.Fragment
alias Serum.Renderer
alias Serum.Tag
alias Serum.Template
alias Serum.Template.Storage, as: TS

@type t :: %__MODULE__{
Expand Down Expand Up @@ -102,12 +100,11 @@ defmodule Serum.Post do
template_name = post.template || "post"
bindings = [page: metadata, contents: post.data]

with %Template{} = template <- TS.get(template_name, :template),
{:ok, html} <- Renderer.render_fragment(template, bindings) do
Result.run do
template <- TS.get(template_name, :template)
html <- Renderer.render_fragment(template, bindings)

Fragment.new(post.file, post.output, metadata, html)
else
nil -> Result.fail(Simple, ["the template \"#{template_name}\" is not available"])
{:error, %Error{}} = error -> error
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/serum/post_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule Serum.PostList do
"""

require Serum.Result, as: Result
alias Serum.Error
alias Serum.Fragment
alias Serum.Plugin.Client, as: PluginClient
alias Serum.Renderer
Expand Down Expand Up @@ -139,12 +138,13 @@ defmodule Serum.PostList do
@spec to_fragment(t()) :: Result.t(Fragment.t())
def to_fragment(post_list) do
metadata = compact(post_list)
template = TS.get("list", :template)
bindings = [page: metadata]

case Renderer.render_fragment(template, bindings) do
{:ok, html} -> Fragment.new(%Serum.File{}, post_list.output, metadata, html)
{:error, %Error{}} = error -> error
Result.run do
template <- TS.get("list", :template)
html <- Renderer.render_fragment(template, bindings)

Fragment.new(%Serum.File{}, post_list.output, metadata, html)
end
end

Expand Down
7 changes: 3 additions & 4 deletions lib/serum/template/compiler/include.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ defmodule Serum.Template.Compiler.Include do

defp prewalk_fun({:include, _, [arg]} = ast, context) do
case TS.get(arg, :include) do
%Template{} = include ->
{:ok, include} ->
{new_include, new_context} = do_expand(include, context)

{quote(do: (fn -> unquote(new_include.ast) end).()), new_context}

nil ->
file = context.template.file
{:error, error} = Result.fail(Simple, ["include not found: \"#{arg}\""], file: file)
{:error, %Error{} = error} ->
error = %Error{error | file: context.template.file}

{ast, %{context | error: {:error, error}}}
end
Expand Down
26 changes: 18 additions & 8 deletions lib/serum/template/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ defmodule Serum.Template.Helpers do
exporting functions/macros with the same names.
"""

require Serum.Result, as: Result
alias Serum.Error
alias Serum.Renderer
alias Serum.Template
alias Serum.Template.Storage, as: TS

@doc "Returns the value of `@site.base_url`."
Expand Down Expand Up @@ -91,14 +92,23 @@ defmodule Serum.Template.Helpers do
"""

def render(name, args \\ []) do
with %Template{} = template <- TS.get(name, :include),
true <- Keyword.keyword?(args),
{:ok, html} <- Renderer.render_fragment(template, args: args) do
html
Result.run do
template <- TS.get(name, :include)
ensure_keyword(args)
Renderer.render_fragment(template, args: args)
end
|> case do
{:ok, html} -> html
{:error, %Error{}} = error -> raise Result.get_message(error, 0)
end
end

@spec ensure_keyword(term()) :: Result.t({})
defp ensure_keyword(args) do
if Keyword.keyword?(args) do
Result.return()
else
nil -> raise "include not found: \"#{name}\""
false -> raise "'args' must be a keyword list, got: #{inspect(args)}"
{:error, _} = error -> raise Serum.Result.get_message(error, 0)
Result.fail(Simple, ["'args' must be a keyword list, got: #{inspect(args)}"])
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/serum/template/compiler/include_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Serum.Template.Compiler.IncludeTest do

setup_all do
TS.load(includes(), :include)

:ok
end

describe "expand/1" do
Expand Down

0 comments on commit f8dd58c

Please sign in to comment.