Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 26, 2024
1 parent 8467530 commit 372403e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
18 changes: 18 additions & 0 deletions examples/xrb/gems.locked
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
PATH
remote: ../..
specs:
xrb (0.2.0)

GEM
remote: https://rubygems.org/
specs:

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
xrb!

BUNDLED WITH
2.5.5
3 changes: 3 additions & 0 deletions examples/xrb/gems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem "xrb", path: "../../"
9 changes: 4 additions & 5 deletions examples/xrb/test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby

require_relative 'lib/xrb'
require 'xrb'

class Formatter
def title_for(options)
Expand All @@ -14,19 +14,18 @@ def value_for(options)
def row(options = {}, &block)
XRB::Builder.fragment do |builder|
builder.inline(:dt) { builder.text title_for(options) }

if block_given?
binding.irb
builder.inline(:dd) { builder.capture(self, &block) }
else
builder.inline(:dd) { builder.text value_for(options) }
end
end
end >> block
end
end

template = XRB::Template.load_file('test.xrb')

binding.irb
puts template.send(:code)

puts template.to_string(binding)
6 changes: 4 additions & 2 deletions examples/xrb/test.xrb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
f = Formatter.new
?>

<?r $stderr.puts "(1) #{_out.class}" ?>
<dl>
# Test Interpolation
#{f.row :title => 'Test title', :value => "Test value"}

# Test Tag
<?r f.row title: 'Title Test' do ?>
<?r $stderr.puts "(2) #{_out.class}" ?>
<a href="test-link">Test Link</a>
<?r end ?>

# Done
</dl>
18 changes: 17 additions & 1 deletion lib/xrb/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def == other
# This is a bit of a hack... but is required for existing specs to pass:
self.to_s == other.to_s
end

def >> block
if block
output = Template.buffer(block.binding)
if output.is_a?(Builder)
@block.call(output)
else
@block.call(Builder.new(output))
end

return nil
else
return self
end
end
end

# A helper to generate fragments of markup.
Expand Down Expand Up @@ -161,7 +176,8 @@ def <<(content)
content.call(self)
end
else
Markup.append(@output, content)
# Markup.append(@output, content)
@output << content
end
end

Expand Down

0 comments on commit 372403e

Please sign in to comment.