Skip to content

Commit

Permalink
Improve the behavior of helper/macro, get test script working
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Oct 31, 2020
1 parent 1a1df9a commit e83379e
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion lib/serbea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions lib/serbea/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/serbea/template_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions serbea.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 4 additions & 6 deletions test/partials/_helpers.serb
Original file line number Diff line number Diff line change
Expand Up @@ -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
%}
{% macro :test_macro do |content="", yo: "ya", &block|
content = capture(&block) if block %}
Totally grand, {{ yo }}! {{ content | split: ',' | join: ';' | split: ' ' | join: ',' }}
{% end %}
16 changes: 9 additions & 7 deletions test/template.serb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Here we go! {{ so_far | upcase | foo "WILD", bar: baz | finalize }} Yay! {{ 123
```
{% endraw %}

{%%= "another raw example" %}

----

{%= form(
Expand All @@ -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 :(" }}

----

Expand Down Expand Up @@ -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.
Expand All @@ -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 <em>escape</em> this!" | safe }}
{{ "Now let's <em>escape</em> by default!" }}
Expand Down
12 changes: 9 additions & 3 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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
Expand All @@ -140,4 +139,11 @@ def scope(name, func)

output = tmpl.render(SerbView.new(baz))

puts output
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."
95 changes: 95 additions & 0 deletions test/test_output.txt
Original file line number Diff line number Diff line change
@@ -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 %}
<input /> {{ blah blah | blee }}
{% end %}
```


{%= "another raw example" %}

----

<form class="checkoout">
<div class="error"><input type="text" name="first_name" required /></div>
</form>
----

groovy not defined :(


yipee!

----

Groovy? Groovy! | groovy! :)

----

<input value="&quot;Totally &#39;wild" />
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

<p>
Multiply! [10, 30, 60, 90]
</p>

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 <em>escape</em> this!
Now let&#39;s &lt;em&gt;escape&lt;/em&gt; by default!
Now let's NOT <em>escape</em> by default!

Component! <
<p>component 1!</p>
>
Component! <
<p>component 2!</p>
>
Component! < >


Array length: 2

1,2,3
ha1heha2heha3he

5.10

1-3-6-2-9 Foo | xx!

Name: lambda test, output: 200

0 comments on commit e83379e

Please sign in to comment.