Skip to content

Commit

Permalink
I'm maybe making a huge mistake
Browse files Browse the repository at this point in the history
Magic comments? Who needs them! I'm going to lean hard into html and
write some server-side custom elements. Here goes nothing.

Write some tests. Make them pass.
  • Loading branch information
danott committed Jan 15, 2024
1 parent 2f58c52 commit e5018ac
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
33 changes: 25 additions & 8 deletions lib/magic_comment_hydrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def hydrate

def next_magic_comment
magic_comment_regex = /<!--(Include|IncludeInHeader)::(.*?)-->/m
content.match(magic_comment_regex)
eval_ruby_regex = %r{<(eval-ruby)>(.*?)</eval-ruby>}m
include_in_header_regex =
%r{<(include-in-header)>(.*?)</include-in-header>}m
content.match(magic_comment_regex) || content.match(eval_ruby_regex) ||
content.match(include_in_header_regex)
end

def next
Expand All @@ -25,20 +29,33 @@ def next

insert_at =
case current_magic_comment[1]
when "IncludeInHeader"
when "IncludeInHeader", "include-in-header"
content.index("</head>")
when "Include"
when "Include", "eval-ruby"
content.index(current_magic_comment[0])
else
fail "Don't know how to handle #{current_magic_comment[1]}"
end

includable, maybe_string = current_magic_comment[2].split(/\s/m, 2)
replacement =
includables
.fetch(includable)
.new(string: maybe_string.to_s.strip, data: data)
.render
case current_magic_comment[1]
when "eval-ruby"
begin
eval(current_magic_comment[2]).to_s.strip
end
when "include-in-header"
current_magic_comment[2].to_s.strip
when "IncludeInHeader", "Include"
includable, maybe_string = current_magic_comment[2].split(/\s/m, 2)

includables
.fetch(includable)
.new(string: maybe_string.to_s.strip, data: data)
.render
.to_s
else
fail "Don't know how to handle #{current_magic_comment[1]}"
end

next_content =
content.sub(current_magic_comment[0], "").insert(insert_at, replacement)
Expand Down
3 changes: 3 additions & 0 deletions site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ You can subscribe to [Dan Ott's Website RSS Feed](/feed.xml).
Here's a list of posts I've written in reverse chronological order.

<!--Include::Index-->
<eval-ruby>
Includable::Index.new(string: nil, data: Build.data)
</eval-ruby>
13 changes: 13 additions & 0 deletions site/javascripts/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/* @format */

console.log("Hello World!");

class IncludeHere extends HTMLElement {
connectedCallback() {
console.error("Unexpected Custom element added to page!");
console.error(this);
this.parentElement.removeChild(this);
}
}

class IncludeInHeader extends IncludeHere {}

customElements.define("include-in-header", IncludeInHeader);
customElements.define("eval-ruby", IncludeHere);
18 changes: 18 additions & 0 deletions test/magic_comment_hydrator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class MagicCommentHydratorTest < Minitest::Test
<h1>Test</h1>
<!--Include::DummyIntendedForInline-->
<!--Include::HydraIncludable-->
<h2><eval-ruby>2 + 2</eval-ruby></h2>
<include-in-header>
<meta name="include-in-header">
<eval-ruby>'<meta name="eval-ruby"' + ">"</eval-ruby>
</include-in-header>
</body>
</html>
HTML
Expand Down Expand Up @@ -51,6 +59,16 @@ def test_everything
.content
.scan("<h1>DummyIntendedForInline Return that!</h1>")
.size

refute_includes result.content, "<eval-ruby>2 + 2</eval-ruby>"
assert_includes result.content, "<h2>4</h2>"

refute_includes result.content, "<include-in-header>"
assert_includes result.content, '<meta name="include-in-header">'
assert_includes result.content, '<meta name="eval-ruby">'
assert result.content.index('name="eval-ruby"') <
result.content.index("</head>"),
"Should actually be injected into the head!"
end

class DummyIntendedForInline
Expand Down

0 comments on commit e5018ac

Please sign in to comment.