diff --git a/lib/xrb.rb b/lib/xrb.rb index 3a95f39..f80b7f8 100644 --- a/lib/xrb.rb +++ b/lib/xrb.rb @@ -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' diff --git a/lib/xrb/builder.rb b/lib/xrb/builder.rb index ab8fc88..30dffa9 100644 --- a/lib/xrb/builder.rb +++ b/lib/xrb/builder.rb @@ -179,7 +179,6 @@ def <<(content) end else Markup.append(@output, content) - # @output << content end end diff --git a/lib/xrb/markup_template.rb b/lib/xrb/markup_template.rb deleted file mode 100644 index 28ab731..0000000 --- a/lib/xrb/markup_template.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -require_relative 'template' - -module XRB - class MarkupTemplate < Template - class Assembler < Template::Assembler - # Output a string interpolation. - def expression(code) - @code << "#{OUT}<<(#{code});" - end - - # Output raw text to the template. - def text(text) - text = text.gsub("'", "\\\\'") - @code << "#{OUT}.raw('#{text}');" - end - end - - def to_string(scope = Object.new, output = nil) - super.output - end - - protected - - # We need an assembler which builds specific `Markup.append` sequences. - def make_assembler - Assembler.new - end - - # The output of the markup template is encoded markup (e.g. with entities, tags, etc). - def output_buffer - Builder.new(encoding: code.encoding) - end - end -end diff --git a/lib/xrb/template.rb b/lib/xrb/template.rb index b92156b..c7e94e9 100644 --- a/lib/xrb/template.rb +++ b/lib/xrb/template.rb @@ -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};" @@ -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}<

Hello <World>

+ \ No newline at end of file diff --git a/test/xrb/.template/capture.xrb b/test/xrb/.template/capture.xrb index e139b57..06f4180 100644 --- a/test/xrb/.template/capture.xrb +++ b/test/xrb/.template/capture.xrb @@ -1,4 +1,14 @@ - -test test test - -#{result.upcase.inspect} \ No newline at end of file +> block +end + +my_capture do ?> +

Hello #{""}

+ \ No newline at end of file diff --git a/test/xrb/.template/interpolation.txt b/test/xrb/.template/interpolation.txt new file mode 100644 index 0000000..5a33b17 --- /dev/null +++ b/test/xrb/.template/interpolation.txt @@ -0,0 +1 @@ +<escaped> \ No newline at end of file diff --git a/test/xrb/.template/interpolation.xrb b/test/xrb/.template/interpolation.xrb new file mode 100644 index 0000000..2796dee --- /dev/null +++ b/test/xrb/.template/interpolation.xrb @@ -0,0 +1 @@ +#{""} \ No newline at end of file diff --git a/test/xrb/.template/my-capture.txt b/test/xrb/.template/my-capture.txt new file mode 100644 index 0000000..6cc4d75 --- /dev/null +++ b/test/xrb/.template/my-capture.txt @@ -0,0 +1,2 @@ +

Hello <World>

+
\ No newline at end of file diff --git a/test/xrb/.template/my-capture.xrb b/test/xrb/.template/my-capture.xrb new file mode 100644 index 0000000..06f4180 --- /dev/null +++ b/test/xrb/.template/my-capture.xrb @@ -0,0 +1,14 @@ +> block +end + +my_capture do ?> +

Hello #{""}

+ \ No newline at end of file diff --git a/test/xrb/markup_parser.rb b/test/xrb/markup_parser.rb index d225111..83eb4fd 100755 --- a/test/xrb/markup_parser.rb +++ b/test/xrb/markup_parser.rb @@ -132,33 +132,33 @@ end end -describe "

"

" do - include_context ValidMarkup - - let(:template_text) {%q{

#{events[3][1]}

}} - 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 "

"

" do +# include_context ValidMarkup + +# let(:template_text) {%q{

#{events[3][1]}

}} +# 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 diff --git a/test/xrb/markup_string.rb b/test/xrb/markup_string.rb index b648794..d626311 100644 --- a/test/xrb/markup_string.rb +++ b/test/xrb/markup_string.rb @@ -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) {"

Hello World

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

Hello World

"} - 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 == "<h1>Hello World</h1>" - end +# expect(template.to_string(model)).to be == "<h1>Hello World</h1>" +# 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