Skip to content

Commit 8b6ef6c

Browse files
committedNov 10, 2024··
test(pages, posts): run converter from frontmatter in test
1 parent e39efa4 commit 8b6ef6c

File tree

4 files changed

+102
-51
lines changed

4 files changed

+102
-51
lines changed
 

‎lib/tableau/extensions/page_extension.ex

+11-11
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,18 @@ defmodule Tableau.PageExtension do
9292
config.dir
9393
|> Path.join("**/*.{#{exts}}")
9494
|> Common.paths()
95-
|> Common.entries(fn pathmap ->
95+
|> Common.entries(fn %{path: path, front_matter: front_matter, pre_convert_body: body, ext: ext} ->
9696
{
97-
build(pathmap.path, pathmap.front_matter, pathmap.pre_convert_body, config),
98-
fn assigns -> pick_module_and_convert(converters, pathmap, assigns) end
97+
build(path, front_matter, body, config),
98+
fn assigns ->
99+
converter =
100+
case front_matter[:converter] do
101+
nil -> converters[ext]
102+
converter -> Module.concat([converter])
103+
end
104+
105+
converter.convert(path, front_matter, body, assigns)
106+
end
99107
}
100108
end)
101109

@@ -113,14 +121,6 @@ defmodule Tableau.PageExtension do
113121
|> Map.put(:graph, graph)}
114122
end
115123

116-
defp to_module(nil), do: nil
117-
defp to_module(string) when is_binary(string), do: Module.concat(string, nil)
118-
119-
defp pick_module_and_convert(converters, pathmap, assigns) do
120-
cnv_module = to_module(pathmap.front_matter[:converter]) || converters[pathmap.ext]
121-
cnv_module.convert(pathmap.path, pathmap.front_matter, pathmap.pre_convert_body, assigns)
122-
end
123-
124124
defp build(filename, front_matter, body, pages_config) do
125125
front_matter
126126
|> Map.put(:__tableau_page_extension__, true)

‎lib/tableau/extensions/post_extension.ex

+11-11
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,18 @@ defmodule Tableau.PostExtension do
100100
config.dir
101101
|> Path.join("**/*.{#{exts}}")
102102
|> Common.paths()
103-
|> Common.entries(fn pathmap ->
103+
|> Common.entries(fn %{path: path, front_matter: front_matter, pre_convert_body: body, ext: ext} ->
104104
{
105-
build(pathmap.path, pathmap.front_matter, pathmap.pre_convert_body, config),
106-
fn assigns -> pick_module_and_convert(converters, pathmap, assigns) end
105+
build(path, front_matter, body, config),
106+
fn assigns ->
107+
converter =
108+
case front_matter[:converter] do
109+
nil -> converters[ext]
110+
converter -> Module.concat([converter])
111+
end
112+
113+
converter.convert(path, front_matter, body, assigns)
114+
end
107115
}
108116
end)
109117
|> Enum.sort_by(fn {post, _} -> post.date end, {:desc, DateTime})
@@ -129,14 +137,6 @@ defmodule Tableau.PostExtension do
129137
|> Map.put(:graph, graph)}
130138
end
131139

132-
defp to_module(nil), do: nil
133-
defp to_module(string) when is_binary(string), do: Module.concat(string, nil)
134-
135-
defp pick_module_and_convert(converters, pathmap, assigns) do
136-
cnv_module = to_module(pathmap.front_matter[:converter]) || converters[pathmap.ext]
137-
cnv_module.convert(pathmap.path, pathmap.front_matter, pathmap.pre_convert_body, assigns)
138-
end
139-
140140
defp build(filename, attrs, body, posts_config) do
141141
Application.put_env(:date_time_parser, :include_zones_from, ~N[2010-01-01T00:00:00])
142142

‎test/tableau/extensions/page_extension_test.exs

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
defmodule Tableau.PageExtensionTest.Layout do
2+
@moduledoc false
3+
use Tableau.Layout
4+
5+
require EEx
6+
7+
EEx.function_from_string(
8+
:def,
9+
:template,
10+
~s'''
11+
<div>
12+
<%= render(@inner_content) %>
13+
</div>
14+
''',
15+
[:assigns]
16+
)
17+
end
18+
119
defmodule Tableau.PageExtensionTest do
220
use ExUnit.Case, async: true
321

@@ -232,10 +250,11 @@ defmodule Tableau.PageExtensionTest do
232250
test "renders with a custom converter in frontmatter", %{tmp_dir: dir, token: token} do
233251
File.write(Path.join(dir, "a-page.md"), """
234252
---
235-
title: missing layout key
253+
title: custom converter
236254
type: articles
255+
layout: Tableau.PageExtensionTest.Layout
237256
permalink: /:type/:title
238-
converter: "Tableau.MDExConverter"
257+
converter: Tableau.PageExtensionTest
239258
---
240259
241260
A great page
@@ -244,18 +263,27 @@ defmodule Tableau.PageExtensionTest do
244263
assert {:ok, token} = PageExtension.run(token)
245264

246265
assert %{
247-
pages: [
248-
%{
249-
__tableau_page_extension__: true,
250-
body: "\nA great page\n",
251-
file: ^dir <> "/a-page.md",
252-
layout: Blog.DefaultPageLayout,
253-
permalink: "/articles/missing-layout-key",
254-
title: "missing layout key",
255-
type: "articles"
256-
}
257-
]
266+
pages: [%{body: "\nA great page\n", converter: "Tableau.PageExtensionTest"}],
267+
graph: graph
258268
} = token
269+
270+
page =
271+
graph
272+
|> Graph.vertices()
273+
|> Enum.find(fn p ->
274+
case p do
275+
%Tableau.Page{permalink: "/articles/custom-converter"} -> true
276+
_ -> false
277+
end
278+
end)
279+
280+
graph = Tableau.Graph.insert(graph, [Tableau.PageExtensionTest.Layout])
281+
282+
content = Tableau.Document.render(graph, page, %{}, %{})
283+
284+
assert content =~ "A GREAT PAGE"
259285
end
260286
end
287+
288+
def convert(_, _, body, _), do: String.upcase(body)
261289
end

‎test/tableau/extensions/post_extension_test.exs

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
defmodule Tableau.PostExtensionTest.Layout do
2+
@moduledoc false
3+
use Tableau.Layout
4+
5+
require EEx
6+
7+
EEx.function_from_string(
8+
:def,
9+
:template,
10+
~s'''
11+
<div>
12+
<%= render(@inner_content) %>
13+
</div>
14+
''',
15+
[:assigns]
16+
)
17+
end
18+
119
defmodule Tableau.PostExtensionTest do
220
use ExUnit.Case, async: true
321

@@ -297,31 +315,36 @@ defmodule Tableau.PostExtensionTest do
297315
---
298316
title: foo man chu_foo.js
299317
type: articles
300-
layout: Some.Layout
318+
layout: Tableau.PostExtensionTest.Layout
301319
date: 2023-02-01 00:01:00
302320
permalink: /:type/:year/:month/:day/:title
303-
converter: "Tableau.MDExConverter"
321+
converter: Tableau.PostExtensionTest
304322
---
305323
306324
A great post
307325
""")
308326

309327
assert {:ok, token} = PostExtension.run(token)
310328

311-
assert %{
312-
posts: [
313-
%{
314-
__tableau_post_extension__: true,
315-
body: "\nA great post\n",
316-
date: ~U[2023-02-01 00:01:00Z],
317-
file: ^dir <> "/a-post.md",
318-
layout: Some.Layout,
319-
permalink: "/articles/2023/02/01/foo-man-chu-foo.js",
320-
title: "foo man chu_foo.js",
321-
type: "articles"
322-
}
323-
]
324-
} = token
329+
assert %{posts: [%{converter: "Tableau.PostExtensionTest"}], graph: graph} = token
330+
331+
page =
332+
graph
333+
|> Graph.vertices()
334+
|> Enum.find(fn p ->
335+
case p do
336+
%Tableau.Page{permalink: "/articles/2023/02/01/foo-man-chu-foo.js"} -> true
337+
_ -> false
338+
end
339+
end)
340+
341+
graph = Tableau.Graph.insert(graph, [Tableau.PostExtensionTest.Layout])
342+
343+
content = Tableau.Document.render(graph, page, %{}, %{})
344+
345+
assert content =~ "A GREAT POST"
325346
end
326347
end
348+
349+
def convert(_, _, body, _), do: String.upcase(body)
327350
end

0 commit comments

Comments
 (0)
Please sign in to comment.