Skip to content

Commit 6117bc8

Browse files
authored
Make erbify block actually work (#54)
1 parent 0b44228 commit 6117bc8

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

lib/perron/resource.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def raw_content = File.read(@file_path)
4848
def content
4949
page_content = Perron::Resource::Separator.new(raw_content).content
5050

51-
return page_content unless erb_processing?
51+
return Perron::Resource::Renderer.erb(page_content, resource: self) if erb_processing?
5252

53-
Perron::Resource::Renderer.erb(page_content, {resource: self})
53+
render_inline_erb using: page_content
5454
end
5555

5656
def to_partial_path
@@ -79,6 +79,12 @@ def generate_id
7979
).first(ID_LENGTH)
8080
end
8181

82+
def render_inline_erb(using:)
83+
using.gsub(/<%=\s*erbify\s+do\s*%>(.*?)<%\s*end\s*%>/m) do
84+
Perron::Resource::Renderer.erb(Regexp.last_match(1).strip_heredoc, resource: self)
85+
end
86+
end
87+
8288
def erb_processing?
8389
@file_path.ends_with?(".erb") || metadata.erb == true
8490
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Inline ERB post
3+
author: Rails Designer
4+
---
5+
6+
This is a regular paragraph in the post. It should not be processed by ERB.
7+
8+
Here is a special block that should be erbified:
9+
<%= erbify do %>
10+
The slug for this resource is: <%= @resource.slug %> and is it authored by <%= @resource.metadata.author %>
11+
<% end %>
12+
13+
And one more paragraph for good measure.

test/perron/resource_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ class Perron::Site::ResourceTest < ActiveSupport::TestCase
44
setup do
55
@page_path = "test/dummy/app/content/pages/about.md"
66
@post_path = "test/dummy/app/content/posts/2023-05-15-sample-post.md"
7+
@inline_erb_post_path = "test/dummy/app/content/posts/2025-10-01-inline-erb-post.md"
78
@page = Content::Page.new(@page_path)
89
@post = Content::Post.new(@post_path)
10+
@inline_erb_post = Content::Post.new(@inline_erb_post_path) # <-- Addition
911
end
1012

1113
test "initialization sets file_path" do
@@ -39,6 +41,16 @@ class Perron::Site::ResourceTest < ActiveSupport::TestCase
3941
assert @post.content
4042
end
4143

44+
test "#content renders inline ERB blocks using erbify helper" do
45+
content = @inline_erb_post.content
46+
47+
assert_match "The slug for this resource is: inline-erb-post", content
48+
assert_no_match(/<%= erbify do %>/, content)
49+
50+
assert_match "This is a regular paragraph", content
51+
assert_match "And one more paragraph for good measure", content
52+
end
53+
4254
test "#metadata returns metadata hash" do
4355
assert_kind_of Hash, @page.metadata
4456
end

test/perron/site/builder/feeds/json_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class Perron::Site::Builder::Feeds::JsonTest < ActiveSupport::TestCase
1717
assert_equal "Dummy App", json["title"]
1818
assert_nil json["description"]
1919
assert_equal "http://localhost:3000/", json["home_page_url"]
20-
assert_equal 2, json["items"].count, "Should include 2 posts (one is excluded by frontmatter)"
20+
assert_equal 3, json["items"].count, "Should include 3 posts (one is excluded by frontmatter)"
2121

2222
titles = json["items"].map { it["title"] }
23-
assert_equal ["Another Sample Post", "Sample Post"], titles, "Posts should be sorted by date descending"
23+
assert_equal ["Inline ERB post", "Another Sample Post", "Sample Post"], titles, "Posts should be sorted by date descending"
2424
end
2525
end
2626

@@ -29,7 +29,7 @@ class Perron::Site::Builder::Feeds::JsonTest < ActiveSupport::TestCase
2929
json = JSON.parse(@builder.generate)
3030

3131
assert_equal 1, json["items"].count
32-
assert_equal "Another Sample Post", json["items"].first["title"]
32+
assert_equal "Inline ERB post", json["items"].first["title"]
3333
end
3434
end
3535

test/perron/site/builder/feeds/rss_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class Perron::Site::Builder::Feeds::RssTest < ActiveSupport::TestCase
1616
assert_equal "Dummy App", rss.at_xpath("//channel/title").text
1717
assert_equal "", rss.at_xpath("//channel/description").text
1818
assert_equal "http://localhost:3000/", rss.at_xpath("//channel/link").text
19-
assert_equal 2, rss.xpath("//item").count, "Should include 2 posts (one is excluded by frontmatter)"
19+
assert_equal 3, rss.xpath("//item").count, "Should include 2 posts (one is excluded by frontmatter)"
2020

2121
titles = rss.xpath("//item/title").map(&:text)
22-
assert_equal ["Another Sample Post", "Sample Post"], titles, "Posts should be sorted by date descending"
22+
assert_equal ["Inline ERB post", "Another Sample Post", "Sample Post"], titles, "Posts should be sorted by date descending"
2323
end
2424
end
2525

@@ -28,7 +28,7 @@ class Perron::Site::Builder::Feeds::RssTest < ActiveSupport::TestCase
2828
rss = Nokogiri::XML(@builder.generate).remove_namespaces!
2929

3030
assert_equal 1, rss.xpath("//item").count
31-
assert_equal "Another Sample Post", rss.at_xpath("//item/title").text
31+
assert_equal "Inline ERB post", rss.at_xpath("//item/title").text
3232
end
3333
end
3434

0 commit comments

Comments
 (0)