Skip to content

Commit

Permalink
Add DynamicMerger (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun authored Oct 4, 2023
1 parent eef1fb9 commit 82b7903
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/temple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Filters
autoload :StaticMerger, 'temple/filters/static_merger'
autoload :StringSplitter, 'temple/filters/string_splitter'
autoload :DynamicInliner, 'temple/filters/dynamic_inliner'
autoload :DynamicMerger, 'temple/filters/dynamic_merger'
autoload :Escapable, 'temple/filters/escapable'
autoload :Eraser, 'temple/filters/eraser'
autoload :Validator, 'temple/filters/validator'
Expand Down
69 changes: 69 additions & 0 deletions lib/temple/filters/dynamic_merger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true
module Temple
module Filters
# Compile [:multi, [:static, 'foo'], [:dynamic, 'bar']] to [:dynamic, '"foo#{bar}"']
class DynamicMerger < Filter
def on_multi(*exps)
exps = exps.dup
result = [:multi]
buffer = []

until exps.empty?
type, arg = exps.first
if type == :dynamic && arg.count("\n") == 0
buffer << exps.shift
elsif type == :static && exps.size > (count = arg.count("\n")) &&
exps[1, count].all? { |e| e == [:newline] }
(1 + count).times { buffer << exps.shift }
elsif type == :newline && exps.size > (count = count_newline(exps)) &&
exps[count].first == :static && count == exps[count].last.count("\n")
(count + 1).times { buffer << exps.shift }
else
result.concat(merge_dynamic(buffer))
buffer = []
result << compile(exps.shift)
end
end
result.concat(merge_dynamic(buffer))

result.size == 2 ? result[1] : result
end

private

def merge_dynamic(exps)
# Merge exps only when they have both :static and :dynamic
unless exps.any? { |type,| type == :static } && exps.any? { |type,| type == :dynamic }
return exps
end

strlit_body = String.new
exps.each do |type, arg|
case type
when :static
strlit_body << arg.dump.sub!(/\A"/, '').sub!(/"\z/, '').gsub('\n', "\n")
when :dynamic
strlit_body << "\#{#{arg}}"
when :newline
# newline is added by `gsub('\n', "\n")`
else
raise "unexpected type #{type.inspect} is given to #merge_dynamic"
end
end
[[:dynamic, "%Q\0#{strlit_body}\0"]]
end

def count_newline(exps)
count = 0
exps.each do |exp|
if exp == [:newline]
count += 1
else
return count
end
end
return count
end
end
end
end
57 changes: 57 additions & 0 deletions spec/filters/dynamic_merger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
describe Temple::Filters::DynamicMerger do
describe '#call' do
def assert_compile(expected, input)
actual = Temple::Filters::DynamicMerger.new.compile(input)
expect(expected).to eq(actual)
end

def assert_noop(input)
actual = Temple::Filters::DynamicMerger.new.compile(input)
expect(input).to eq(actual)
end

def strlit(body)
"%Q\0#{body}\0"
end

specify { assert_compile([:static, 'foo'], [:multi, [:static, 'foo']]) }
specify { assert_compile([:dynamic, 'foo'], [:multi, [:dynamic, 'foo']]) }
specify { assert_noop([:multi, [:static, 'foo'], [:newline]]) }
specify { assert_noop([:multi, [:dynamic, 'foo'], [:newline]]) }
specify { assert_noop([:multi, [:static, "foo\n"], [:newline]]) }
specify { assert_noop([:multi, [:static, 'foo'], [:dynamic, "foo\n"], [:newline]]) }
specify { assert_noop([:multi, [:static, "foo\n"], [:dynamic, 'foo'], [:newline]]) }
specify do
assert_compile([:dynamic, strlit("\#{foo}foo\n")],
[:multi, [:dynamic, 'foo'], [:static, "foo\n"], [:newline]])
end
specify do
assert_compile([:multi,
[:dynamic, strlit("\#{foo}foo\n\n")],
[:newline], [:code, 'foo'],
],
[:multi,
[:dynamic, 'foo'], [:static, "foo\n\n"], [:newline], [:newline],
[:newline], [:code, 'foo'],
])
end
specify do
assert_compile([:multi,
[:dynamic, strlit("\#{foo}foo\n")],
[:code, 'bar'],
[:dynamic, strlit("\#{foo}foo\n")],
],
[:multi,
[:dynamic, 'foo'], [:static, "foo\n"], [:newline],
[:code, 'bar'],
[:dynamic, 'foo'], [:static, "foo\n"], [:newline],
])
end
specify do
assert_compile([:multi, [:newline], [:dynamic, strlit("foo\n\#{foo}")]],
[:multi, [:newline], [:newline], [:static, "foo\n"], [:dynamic, 'foo']])
end
specify { assert_compile([:static, "\n"], [:multi, [:static, "\n"]]) }
specify { assert_compile([:newline], [:multi, [:newline]]) }
end
end

0 comments on commit 82b7903

Please sign in to comment.