Skip to content

Commit

Permalink
Remove MarkupTemplate specialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 27, 2024
1 parent 8879317 commit b8dafd1
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 110 deletions.
1 change: 0 additions & 1 deletion lib/xrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
require_relative 'xrb/native'
require_relative 'xrb/builder'
require_relative 'xrb/template'
require_relative 'xrb/markup_template'

require_relative 'xrb/reference'
1 change: 0 additions & 1 deletion lib/xrb/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def <<(content)
end
else
Markup.append(@output, content)
# @output << content
end
end

Expand Down
36 changes: 0 additions & 36 deletions lib/xrb/markup_template.rb

This file was deleted.

21 changes: 8 additions & 13 deletions lib/xrb/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class Assembler
def initialize(encoding: Encoding::UTF_8)
@code = String.new.force_encoding(encoding)
end

attr :code

# Output raw text to the template.
def text(text)
text = text.gsub("'", "\\\\'")
@code << "#{OUT}<<'#{text}';"
@code << "#{OUT}.raw('#{text}');"

# This is an interesting approach, but it doens't preserve newlines or tabs as raw characters, so template line numbers don't match up.
# @parts << "#{OUT}<<#{text.dump};"
Expand All @@ -64,9 +64,8 @@ def instruction(text, postfix = nil)
end

# Output a string interpolation.
def expression(text)
# Double brackets are required here to handle expressions like #{foo rescue "bar"}.
@code << "#{OUT}<<String(#{text});"
def expression(code)
@code << "#{OUT}<<(#{code});"
end
end

Expand All @@ -93,11 +92,11 @@ def freeze
end

def to_string(scope = Object.new, output = nil)
output ||= output_buffer
builder = Builder.new(output, encoding: code.encoding)

scope.instance_exec(output, &to_proc)
scope.instance_exec(builder, &to_proc)

return output
return builder.output
end

def to_buffer(scope)
Expand All @@ -110,10 +109,6 @@ def to_proc(scope = @binding.dup)

protected

def output_buffer
String.new.force_encoding(code.encoding)
end

def code
@code ||= compile!
end
Expand Down
3 changes: 2 additions & 1 deletion test/xrb/.template/capture.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
"TEST TEST TEST\n"
<div><p>Hello &lt;World&gt;</p>
</div>
18 changes: 14 additions & 4 deletions test/xrb/.template/capture.xrb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?r result = XRB::Template.capture do ?>
test test test
<?r end ?>
#{result.upcase.inspect}
<?r
def my_capture(value = nil, &block)
XRB::Builder.fragment do |builder|
if block_given?
builder.inline(:div) { builder.capture(self, &block) }
else
builder.inline(:div) { builder.text value }
end
end >> block
end

my_capture do ?>
<p>Hello #{"<World>"}</p>
<?r end ?>
1 change: 1 addition & 0 deletions test/xrb/.template/interpolation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
&lt;escaped&gt;
1 change: 1 addition & 0 deletions test/xrb/.template/interpolation.xrb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#{"<escaped>"}
2 changes: 2 additions & 0 deletions test/xrb/.template/my-capture.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div><p>Hello &lt;World&gt;</p>
</div>
14 changes: 14 additions & 0 deletions test/xrb/.template/my-capture.xrb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?r
def my_capture(value = nil, &block)
XRB::Builder.fragment do |builder|
if block_given?
builder.inline(:div) { builder.capture(self, &block) }
else
builder.inline(:div) { builder.text value }
end
end >> block
end

my_capture do ?>
<p>Hello #{"<World>"}</p>
<?r end ?>
54 changes: 27 additions & 27 deletions test/xrb/markup_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,33 @@
end
end

describe "<p attr=\"foo&amp;bar\">&quot;</p>" do
include_context ValidMarkup

let(:template_text) {%q{<p attr="#{events[1][2]}">#{events[3][1]}</p>}}
let(:template_buffer) {XRB::Buffer(template_text)}
let(:template) {XRB::MarkupTemplate.new(template_buffer)}

it "should parse empty attributes" do
expect(events).to be == [
[:open_tag_begin, "p", 1],
[:attribute, "attr", "foo&bar"],
[:open_tag_end, false],
[:text, "\""],
[:close_tag, "p", 30]
]
end

it "generates same output as input" do
result = template.to_string(self)
expect(result).to be == subject
end

it "should track entities" do
expect(events[1][2]).not.to be_a XRB::Markup
expect(events[3][1]).not.to be_a XRB::Markup
end
end
# describe "<p attr=\"foo&amp;bar\">&quot;</p>" do
# include_context ValidMarkup

# let(:template_text) {%q{<p attr="#{events[1][2]}">#{events[3][1]}</p>}}
# let(:template_buffer) {XRB::Buffer(template_text)}
# let(:template) {XRB::MarkupTemplate.new(template_buffer)}

# it "should parse empty attributes" do
# expect(events).to be == [
# [:open_tag_begin, "p", 1],
# [:attribute, "attr", "foo&bar"],
# [:open_tag_end, false],
# [:text, "\""],
# [:close_tag, "p", 30]
# ]
# end

# it "generates same output as input" do
# result = template.to_string(self)
# expect(result).to be == subject
# end

# it "should track entities" do
# expect(events[1][2]).not.to be_a XRB::Markup
# expect(events[3][1]).not.to be_a XRB::Markup
# end
# end

ValidMarkupFile = Sus::Shared("valid markup file") do |base|
include_context ValidMarkup
Expand Down
54 changes: 27 additions & 27 deletions test/xrb/markup_string.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
#!/usr/bin/env rspec
# frozen_string_literal: true
# #!/usr/bin/env rspec
# # frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2016-2024, by Samuel Williams.
# # Released under the MIT License.
# # Copyright, 2016-2024, by Samuel Williams.

require 'xrb'
require 'xrb/markup'
# require 'xrb'
# require 'xrb/markup'

Model = Struct.new(:text)
# Model = Struct.new(:text)

describe XRB::MarkupString do
with 'basic template' do
let(:template) {XRB::MarkupTemplate.load_file File.expand_path('.corpus/basic.xrb', __dir__)}
let(:html_text) {"<h1>Hello World</h1>"}
# describe XRB::MarkupString do
# with 'basic template' do
# let(:template) {XRB::MarkupTemplate.load_file File.expand_path('.corpus/basic.xrb', __dir__)}
# let(:html_text) {"<h1>Hello World</h1>"}

it "should escape unsafe text" do
model = Model.new(html_text)
# it "should escape unsafe text" do
# model = Model.new(html_text)

expect(template.to_string(model)).to be == "&lt;h1&gt;Hello World&lt;/h1&gt;"
end
# expect(template.to_string(model)).to be == "&lt;h1&gt;Hello World&lt;/h1&gt;"
# end

let(:safe_html_text) {XRB::Builder.tag('h1', 'Hello World')}
# let(:safe_html_text) {XRB::Builder.tag('h1', 'Hello World')}

it "should not escape safe text" do
model = Model.new(safe_html_text)
# it "should not escape safe text" do
# model = Model.new(safe_html_text)

expect(template.to_string(model)).to be == html_text
end
end
# expect(template.to_string(model)).to be == html_text
# end
# end

it "should convert nil to empty string" do
markup_string = XRB::MarkupString.new
# it "should convert nil to empty string" do
# markup_string = XRB::MarkupString.new

XRB::Markup.append(markup_string, nil)
# XRB::Markup.append(markup_string, nil)

expect(markup_string).to be(:empty?)
end
end
# expect(markup_string).to be(:empty?)
# end
# end

0 comments on commit b8dafd1

Please sign in to comment.