-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |