Skip to content

Commit

Permalink
Make Text magic comment just like all the others
Browse files Browse the repository at this point in the history
This may be over-engineered. But I would like to treat this magic
comment just like all the others. On the bright side, this clears a
YAGNI path for passing arguments to Includable classes if I ever need
it.

The build was getting slow re-building everything for every page. Add
some stupid-simple caching in instance variables to speed it back up.
  • Loading branch information
danott committed Jan 14, 2024
1 parent bf5e172 commit 5074a6d
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 47 deletions.
11 changes: 6 additions & 5 deletions lib/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ def target_dir
end

def dehydrated_sources
Source.gather(source_dir)
@dehydrated_sources ||= Source.gather(source_dir)
end

def dehydrated_targets
dehydrated_sources.map(&:hydrate)
@dehydrated_targets ||= dehydrated_sources.map(&:hydrate)
end

def hydrated_targets
dehydrated_targets.map(&:hydrate) + [RssTarget.new]
@hydrated_targets ||= dehydrated_targets.map(&:hydrate) + [RssTarget.new]
end

def data
BuildData.new(dehydrated_targets)
@data ||= BuildData.new(dehydrated_targets)
end

def includables
{
@includable ||= {
"Text" => Includable::Text,
"DataDump" => Includable::DataDump,
"Index" => Includable::Index,
"PageDefaults" => Includable::PageDefaults,
Expand Down
6 changes: 5 additions & 1 deletion lib/html_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def initialize(content:, path:, data:)

def hydrate
hydrator =
MagicCommentHydrator.new(content: content, includables: Build.includables)
MagicCommentHydrator.new(
content: content,
includables: Build.includables,
data: Build.data
)
self.class.new(content: hydrator.hydrate.content, path: path, data: data)
end

Expand Down
11 changes: 7 additions & 4 deletions lib/includable/data_dump.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module Includable
class DataDump
def everything
Build.data.all.merge(
posts_tagged_climbing: Build.data.posts_tagged("climbing")
)
attr_reader :everything

def initialize(string:, data:)
@everything =
data.all.merge(
posts_tagged_climbing: Build.data.posts_tagged("climbing")
)
end

def render
Expand Down
11 changes: 7 additions & 4 deletions lib/includable/index.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Includable
class Index
attr_reader :posts

Post = Struct.new(:title, :url, :date)

def posts
Build.data.posts.map do |p|
Post.new(p.fetch("title"), p.fetch("url"), p.fetch("date"))
end
def initialize(string:, data:)
@posts =
data.posts.map do |p|
Post.new(p.fetch("title"), p.fetch("url"), p.fetch("date"))
end
end

def render
Expand Down
5 changes: 4 additions & 1 deletion lib/includable/page_defaults.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module Includable
class PageDefaults
def initialize(string:, data:)
end

def render
<<~HTML
<!--IncludeInHeader::Text::
<!--IncludeInHeader::Text
<link rel="stylesheet" href="/style.css">
<script src="/script.js"></script>
-->
Expand Down
18 changes: 10 additions & 8 deletions lib/includable/six_words.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module Includable
class SixWords
attr_reader :entries

Entry = Struct.new(:date, :words)

def entries
Build
.data
.sets
.fetch("six_words")
.map { |e| Entry.new(e.fetch("date"), e.fetch("words")) }
.sort_by(&:date)
.reverse
def initialize(string:, data:)
@entries =
data
.sets
.fetch("six_words")
.map { |e| Entry.new(e.fetch("date"), e.fetch("words")) }
.sort_by(&:date)
.reverse
end

def render
Expand Down
13 changes: 13 additions & 0 deletions lib/includable/text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Includable
class Text
attr_reader :string

def initialize(string:, data:)
@string = string
end

def render
string
end
end
end
36 changes: 18 additions & 18 deletions lib/magic_comment_hydrator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class MagicCommentHydrator
attr_reader :content
attr_reader :includables
attr_reader :data

def initialize(content:, includables:)
def initialize(content:, includables:, data:)
@content = content
@includables = includables
@data = data
end

def hydrate
Expand All @@ -21,27 +23,25 @@ def next

current_magic_comment = next_magic_comment

delete_at = content.index(current_magic_comment[0])
insert_at =
(
if current_magic_comment[1] == "IncludeInHeader"
content.index("</head>")
else
delete_at
end
)

case current_magic_comment[1]
when "IncludeInHeader"
content.index("</head>")
when "Include"
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 =
(
if current_magic_comment[2].start_with?("Text::")
current_magic_comment[2].delete_prefix("Text::")
else
includables.fetch(current_magic_comment[2]).new.render
end
)
includables
.fetch(includable)
.new(string: maybe_string.to_s.strip, data: data)
.render

next_content =
content.sub(current_magic_comment[0], "").insert(insert_at, replacement)
self.class.new(content: next_content, includables: includables)
self.class.new(content: next_content, includables: includables, data: data)
end
end
33 changes: 27 additions & 6 deletions test/magic_comment_hydrator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class MagicCommentHydratorTest < Minitest::Test
<title>Test</title>
</head>
<body>
<!--IncludeInHeader::Text:: <meta name="single line text"> -->
<!--IncludeInHeader::Text::
<!--IncludeInHeader::Text <meta name="single line text"> -->
<!--IncludeInHeader::Text
<meta name="multi line text">
-->
<!--IncludeInHeader::DummyIntendedForHeader-->
Expand All @@ -21,15 +21,19 @@ def test_everything
hydrator =
MagicCommentHydrator.new(
content: CONTENT,
data: {
"Fetch this!" => "Return that!"
},
includables: {
"Text" => Includable::Text,
"HydraIncludable" => HydraIncludable,
"DummyIntendedForInline" => DummyIntendedForInline,
"DummyIntendedForHeader" => DummyIntendedForHeader
}
)
result = hydrator.hydrate

refute_includes result.content, "<!--IncludeInHeader::Text::"
refute_includes result.content, "<!--IncludeInHeader::Text "
assert_includes result.content, '<meta name="single line text">'
assert_includes result.content, '<meta name="multi line text">'

Expand All @@ -38,25 +42,42 @@ def test_everything
assert_includes result.content, '<meta name="DummyIntendedForHeader">'

refute_includes result.content, "<!--Include::DummyIntendedForInline-->"
assert_includes result.content, "<h1>DummyIntendedForInline</h1>"
assert_includes result.content,
"<h1>DummyIntendedForInline Return that!</h1>"

refute_includes result.content, "<!--Include::HydraIncludable-->"
assert_equal 2, result.content.scan("<h1>DummyIntendedForInline</h1>").size
assert_equal 2,
result
.content
.scan("<h1>DummyIntendedForInline Return that!</h1>")
.size
end

class DummyIntendedForInline
attr_reader :data

def initialize(string:, data:)
@data = data
end

def render
"<h1>DummyIntendedForInline</h1>"
"<h1>DummyIntendedForInline #{data.fetch("Fetch this!")}</h1>"
end
end

class DummyIntendedForHeader
def initialize(string:, data:)
end

def render
%Q[<meta name="DummyIntendedForHeader">]
end
end

class HydraIncludable
def initialize(string:, data:)
end

def render
"<!--Include::DummyIntendedForInline-->"
end
Expand Down

0 comments on commit 5074a6d

Please sign in to comment.