diff --git a/Gemfile b/Gemfile index 3fcb138..f25fd5d 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in serbea.gemspec gemspec + +gem "erubi", github: "jaredcwhite/erubi", branch: "config-literal-prefix" diff --git a/Rakefile b/Rakefile index 30baabd..4d59046 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,12 @@ require "bundler/gem_tasks" require "rake/testtask" +require "bundler" + +Bundler.setup Rake::TestTask.new(:test) do |t| - t.libs << "test" - t.libs << "lib" - t.test_files = FileList["test/**/test_*.rb"] + t.test_files = FileList["test/test.rb"] + t.warning = false end task :default => :test diff --git a/lib/serbea.rb b/lib/serbea.rb index 0a364fd..75521ec 100644 --- a/lib/serbea.rb +++ b/lib/serbea.rb @@ -9,7 +9,13 @@ module Tilt class SerbeaTemplate < ErubiTemplate def prepare - @options.merge!(outvar: "@_erbout", bufval: "Serbea::Buffer.new", engine_class: Serbea::TemplateEngine) + @options.merge!( + outvar: "@_erbout", + bufval: "Serbea::Buffer.new", + literal_prefix: "{%", + literal_postfix: "%}", + engine_class: Serbea::TemplateEngine + ) super end diff --git a/lib/serbea/helpers.rb b/lib/serbea/helpers.rb index afe59c0..4299caa 100644 --- a/lib/serbea/helpers.rb +++ b/lib/serbea/helpers.rb @@ -29,9 +29,27 @@ def pipeline(context, value) Pipeline.new(context, value) end - def helper(name, &block) - self.class.send(:define_method, name, &block) + def helper(name, &helper_block) + self.class.define_method(name) do |*args, &block| + previous_buffer_state = @_erbout + @_erbout = Serbea::Buffer.new + + # For compatibility with ActionView, not used by Bridgetown normally + previous_ob_state = @output_buffer + @output_buffer = Serbea::Buffer.new + + result = helper_block.call(*args, &block) + if @output_buffer != "" + # use Rails' ActionView buffer if present + result = @output_buffer + end + @_erbout = previous_buffer_state + @output_buffer = previous_ob_state + + result.is_a?(String) ? result.html_safe : result + end end + alias_method :macro, :helper def h(input) ERB::Util.h(input.to_s) diff --git a/lib/serbea/template_engine.rb b/lib/serbea/template_engine.rb index 430dcb2..2eb6b0e 100644 --- a/lib/serbea/template_engine.rb +++ b/lib/serbea/template_engine.rb @@ -127,8 +127,8 @@ def process_serbea_input(template, properties) buff << text if code.length > 0 - code.sub! /^\{%@/, "" - code.sub! /%}$/, "" + code.sub!(/^\{%@/, "") + code.sub!(/%}$/, "") unless ["end", ""].include? code.strip original_line_length = code.lines.size diff --git a/serbea.gemspec b/serbea.gemspec index 3f9cb5d..f9cee5f 100644 --- a/serbea.gemspec +++ b/serbea.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |spec| spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features)/!) } spec.require_paths = ["lib"] + spec.add_runtime_dependency("rake", "~> 13.0") spec.add_runtime_dependency("erubi", "~> 1.9") spec.add_runtime_dependency("activesupport", "~> 6.0") spec.add_runtime_dependency("tilt", "~> 2.0") diff --git a/test/partials/_helpers.serb b/test/partials/_helpers.serb index e64c84a..20e958e 100644 --- a/test/partials/_helpers.serb +++ b/test/partials/_helpers.serb @@ -5,9 +5,7 @@ end end %} -{% - helper :goober do |content="", &block| - content = capture(&block) if block - "Totally grand! #{content.split(',').join(';').split(' ').join(',')} Yay!" - end -%} \ No newline at end of file +{% macro :test_macro do |content="", yo: "ya", &block| + content = capture(&block) if block %} +Totally grand, {{ yo }}! {{ content | split: ',' | join: ';' | split: ' ' | join: ',' }} +{% end %} \ No newline at end of file diff --git a/test/template.serb b/test/template.serb index 80c7be2..424a4a4 100644 --- a/test/template.serb +++ b/test/template.serb @@ -36,6 +36,8 @@ Here we go! {{ so_far | upcase | foo "WILD", bar: baz | finalize }} Yay! {{ 123 ``` {% endraw %} +{%%= "another raw example" %} + ---- {%= form( @@ -46,11 +48,11 @@ Here we go! {{ so_far | upcase | foo "WILD", bar: baz | finalize }} Yay! {{ 123 ---- -{{ defined?(groovy) ? "HAHA" : "groovy not defined :(" }} +{{ defined?(groovy) ? "yipee!" : "groovy not defined :(" }} {% groovy = " groovy! " %} -{{ defined?(groovy) ? "HAHA" : "groovy not defined :(" }} +{{ defined?(groovy) ? "yipee!" : "groovy not defined :(" }} ---- @@ -81,7 +83,7 @@ Prepend permanent? ---- -{%@ "weee/wooo", cool: "beans do yeah" do |testing| %} +{%@ "weee/wooo", cool: "beans yeah" do |testing| %} This is **dope!** {%@ "weee/wooo", cool: "cats" do %} So great. @@ -98,15 +100,15 @@ Prepend permanent? {{ partial "errors", errors: error_messages |> upcase: }} ---- -{%= goober do %} +{%= test_macro do %} Is this a thing?? - {%= goober do %} + {%= test_macro do %} I hope so! {% end %} Wee! {% end %} - -{{ goober "Foo" }} +{%= test_macro "Best helper ever!", yo: "dude" -%} +{%= test_macro -%} {{ "Let's not escape this!" | safe }} {{ "Now let's escape by default!" }} diff --git a/test/test.rb b/test/test.rb index 7725627..ee2d958 100644 --- a/test/test.rb +++ b/test/test.rb @@ -90,7 +90,6 @@ def render(tmpl_name, variables = {}, &block) end fake_tmpl = "aha! {{ content }} yes! cool {%= cool %}" - fake_tmpl += "{% if defined? blah %}wee!{% end %}" tmpl = Tilt::SerbeaTemplate.new { fake_tmpl } @@ -118,7 +117,7 @@ def partial(partial_name, options = {}) Serbea::TemplateEngine.front_matter_preamble = "self.pagedata = YAML.load" #Serbea::Pipeline.raise_on_missing_filters = true -tmpl = Tilt.new("template.serb") +tmpl = Tilt.new(File.join(__dir__, "template.serb")) #puts "=====" #puts tmpl.instance_variable_get(:@engine).src @@ -140,4 +139,11 @@ def scope(name, func) output = tmpl.render(SerbView.new(baz)) -puts output \ No newline at end of file +previous_output = File.read(File.join(__dir__, "test_output.txt")) + +if output.strip != previous_output.strip + File.write(File.join(__dir__, "bad_output.txt"), output) + raise "Output does not match! Saved to bad_output.txt" +end + +puts "\nYay! Test passed." diff --git a/test/test_output.txt b/test/test_output.txt new file mode 100644 index 0000000..b5720f8 --- /dev/null +++ b/test/test_output.txt @@ -0,0 +1,95 @@ +The title is: I'm a title! :) +Categories: ["category1", "category2"] + +This is totally great +Um… + +1357 +Whoa + +Here we go! ((SO GOOD(WILD)lala)) Yay! 123.0 Done. + +---- + + +```ruby +{%= form classes: "raw" do %} + {{ blah blah | blee }} +{% end %} +``` + + +{%= "another raw example" %} + +---- + +
+---- + +groovy not defined :( + + +yipee! + +---- + +Groovy? Groovy! | groovy! :) + +---- + + +Selected? true + +---- + + +Whoa OMG! T h i s i s A M A Z I N G ! ;-D + +Prepend permanent? +T h i s i s A M A Z I N G ! + +---- + +aha! + This is **dope!** + aha! + So great. + yes! cool cats yes! cool beans yeah +aha! yes! cool boxes + ++ Multiply! [10, 30, 60, 90] +
+ +ARGH! FIRST_NAME: BAD!! + +---- +Totally grand, ya! Is,this,a,thing??,Totally,grand;,ya!,I;hope;so!,Wee! +Totally grand, dude! Best,helper,ever! +Totally grand, ya! + +Let's not escape this! +Now let's <em>escape</em> by default! +Now let's NOT escape by default! + +Component! < +component 1!
+ > +Component! < +component 2!
+ > +Component! < > + + +Array length: 2 + +1,2,3 +ha1heha2heha3he + +5.10 + +1-3-6-2-9 Foo | xx! + + Name: lambda test, output: 200