Skip to content

Commit

Permalink
Support pipe in Serbea 2.0, add new pure Ruby template syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Oct 10, 2023
1 parent 6e11b08 commit e14b27b
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ PATH
rake (>= 13.0)
roda (~> 3.46)
rouge (~> 3.0)
serbea (~> 1.0)
serbea (~> 2.0)
thor (~> 1.1)
tilt (~> 2.0)
zeitwerk (~> 2.5)
Expand Down Expand Up @@ -199,7 +199,7 @@ GEM
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
serbea (1.0.1)
serbea (2.0.0)
activesupport (>= 6.0)
erubi (>= 1.10)
tilt (~> 2.0)
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-builder/lib/bridgetown-builder/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def initialize(name = nil, current_site = nil)

def doc(*)
raise Bridgetown::Errors::FatalException,
"The `doc' method has been removed. Please use the `new_resource' builder DSL instead"
"The `doc' method has been removed. Please use the `add_resource' builder DSL instead"
end
end
end
Expand Down
21 changes: 0 additions & 21 deletions bridgetown-builder/test/test_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ def build
hook :site, :pre_read do
site.config[:pre_read_hook_ran] = true
end

hook :site, :post_read do
if site.data[:languages]
doc "#{site.data[:languages][1]}.md" do
title "Ruby"
date "2020-05-17"
end
end
end
end
end

Expand Down Expand Up @@ -48,18 +39,6 @@ class TestHooks < BridgetownUnitTest
assert @site.config[:after_reset_hook_ran]
assert @site.config[:pre_read_hook_ran]
end

# TODO: get working with Resource
# should "work alongside Document Builder" do
# @site.reset
# @site.setup
# @site.read
# @generator = Builders::DocumentsGenerator.new(@site.config)
# @generator.generate(@site)

# post = @site.posts.docs.find { |doc| doc.data[:title] == "Ruby" }
# assert_includes post.destination(""), "/dest/2020/05/17/ruby.html"
# end
end

context "SiteBuilder" do
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/bridgetown-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("rake", ">= 13.0")
s.add_runtime_dependency("roda", "~> 3.46")
s.add_runtime_dependency("rouge", "~> 3.0")
s.add_runtime_dependency("serbea", "~> 1.0")
s.add_runtime_dependency("serbea", "~> 2.0")
s.add_runtime_dependency("thor", "~> 1.1")
s.add_runtime_dependency("tilt", "~> 2.0")
s.add_runtime_dependency("zeitwerk", "~> 2.5")
Expand Down
10 changes: 10 additions & 0 deletions bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def require_all(path)
# Ensure we can set up fallbacks so the default locale gets used
I18n::Backend::Simple.include I18n::Backend::Fallbacks

# Monkey patches:

module HashWithDotAccess
class Hash # :nodoc:
def to_liquid
Expand All @@ -68,6 +70,14 @@ def to_liquid
end
end

class Enumerator
def html_map(&block)
results = map.each(&block)

results.join.html_safe
end
end

# Create our little String subclass for Ruby Front Matter
class Rb < String; end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "tilt/erubi"
require "serbea/pipeline"

module Bridgetown
class OutputBuffer < ActiveSupport::SafeBuffer
Expand Down Expand Up @@ -74,6 +75,7 @@ def capture(*args)

class ERBView < RubyTemplateView
include ERBCapture
include Serbea::Pipeline::Helper

def h(input)
Erubi.h(input)
Expand Down
66 changes: 63 additions & 3 deletions bridgetown-core/lib/bridgetown-core/converters/ruby_templates.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,76 @@
# frozen_string_literal: true

module Bridgetown
class PureRubyView < ERBView
# @yield a block which should be HTML escaped
def text(input, &blk)
if blk
h(pipe(input, &blk))
else
h(input)
end
end

def render(item = nil, **options, &block) # rubocop:disable Metrics
return @_erbout if !block && item.nil? && !options.key?(:html)

if options.key?(:html) || (block && item.nil?)
result = options.key?(:html) ? options[:html].presence : yield
return result if result.is_a?(OutputBuffer)

@_erbout ||= OutputBuffer.new
@_erbout << result.to_s.html_safe

return @_erbout
end

if item.respond_to?(:render_in)
result = item.render_in(self, &block)
result&.html_safe
else
partial(item, **options, &block)&.html_safe
end
end

def _render_partial(partial_name, options)
partial_path = _partial_path(partial_name, "rb")
return super unless File.exist?(partial_path)

(@_locals_stack ||= []).push(options)
(@_buffer_stack ||= []).push(@_erbout)
@_erbout = OutputBuffer.new

tmpl = site.tmp_cache["partial-tmpl:#{partial_path}"] ||=
options.keys.map do |k|
"#{k}=locals[:#{k}];"
end.push(File.read(partial_path)).join

instance_eval(tmpl).to_s.tap do
@_locals_stack.pop
@_erbout = @_buffer_stack.pop
end
end

def _output_buffer
@_erbout # might be nil
end

def locals
@_locals_stack&.last || {}
end
end

module Converters
class RubyTemplates < Converter
priority :highest
input :rb

def convert(content, convertible)
erb_view = Bridgetown::ERBView.new(convertible)
erb_view.instance_eval(
rb_view = Bridgetown::PureRubyView.new(convertible)
results = rb_view.instance_eval(
content, convertible.path.to_s, line_start(convertible)
).to_s
)
rb_view._output_buffer || results.to_s
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
end
%>---

Fa <%= 8.times.map { "la" }.join(" ") %>!
Fa <%= pipe(8) { times | map(->(_) { "la" }) | join(" ") | concat("!") } %>
26 changes: 23 additions & 3 deletions bridgetown-core/test/resources/src/_pages/i-am-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
front_matter do
layout :default
title "I am Ruby. Here me roar!"
include_markdown true
end
###

markdownify <<~MARKDOWN
render html: <<-HTML
<p>Hello #{text "<p>world</p>"}</p>
#{ render "a_partial", abc: 123 }
#{ render "an_erb_partial", abc: 456 }
HTML

> Well, _this_ is quite interesting! =)
if data.include_markdown
render do
markdownify <<~MARKDOWN
MARKDOWN
> Well, _this_ is quite interesting! =)
MARKDOWN
end
end

render html: <<-HTML
<ul>
#{3.times.html_map do |i| <<-HTML
<li>#{text i}</li>
HTML
end}
</ul>
HTML
3 changes: 3 additions & 0 deletions bridgetown-core/test/resources/src/_partials/_a_partial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
render html: <<~HTML
<output>#{text("Does this work?") { upcase | concat(" ") | concat(abc.to_s) }}</output>
HTML
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<output><%= "Does this work? #{abc}" %></output>
15 changes: 14 additions & 1 deletion bridgetown-core/test/test_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,27 @@ class TestResource < BridgetownUnitTest
@site = resources_site
@site.process
@dest_file = File.read(dest_dir("i-am-ruby/index.html"))
Serbea::Pipeline.raise_on_missing_filters = true

# rubocop:disable Layout/TrailingWhitespace
assert_includes @dest_file, <<~HTML
<body>
<p>Hello &lt;p&gt;world&lt;/p&gt;</p>
<output>DOES THIS WORK? 123</output>
<output>Does this work? 456</output>
<blockquote>
<p>Well, <em>this</em> is quite interesting! =)</p>
</blockquote>
</blockquote> <ul>
<li>0</li>
<li>1</li>
<li>2</li>
</ul>
</body>
HTML
# rubocop:enable Layout/TrailingWhitespace
end
end

Expand Down
4 changes: 2 additions & 2 deletions bridgetown-website/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ PATH
rake (>= 13.0)
roda (~> 3.46)
rouge (~> 3.0)
serbea (~> 1.0)
serbea (~> 2.0)
thor (~> 1.1)
tilt (~> 2.0)
zeitwerk (~> 2.5)
Expand Down Expand Up @@ -119,7 +119,7 @@ GEM
ruby2js (5.1.0)
parser
regexp_parser (~> 2.1.1)
serbea (1.0.1)
serbea (2.0.0)
activesupport (>= 6.0)
erubi (>= 1.10)
tilt (~> 2.0)
Expand Down

0 comments on commit e14b27b

Please sign in to comment.