Skip to content

Commit

Permalink
Compiler: add support for defer (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Jan 9, 2025
1 parent 19e630d commit e70aaa4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PATH
remote: .
specs:
papercraft (1.2)
papercraft (1.3)
escape_utils (~> 1.3.0)
kramdown (~> 2.5.1)
kramdown-parser-gfm (~> 1.1.0)
rouge (~> 4.5.1)
sirop (~> 0.4)
sirop (~> 0.5)

GEM
remote: https://rubygems.org/
Expand All @@ -22,7 +22,7 @@ GEM
prism (0.27.0)
rexml (3.4.0)
rouge (4.5.1)
sirop (0.4)
sirop (0.5)
prism (~> 0.27.0)
tilt (2.2.0)

Expand Down
12 changes: 6 additions & 6 deletions lib/papercraft.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
# frozen_string_literal: true

require 'kramdown'
require 'rouge'
require 'kramdown-parser-gfm'

require_relative 'papercraft/template'
require_relative 'papercraft/renderer'
require_relative 'papercraft/compiler'

# require_relative 'papercraft/compiler'

# Papercraft is a composable templating library
module Papercraft
Expand Down Expand Up @@ -82,6 +77,11 @@ def json(o = nil, mime_type: nil, &template)
# @param opts [Hash] Kramdown option overrides
# @return [String] HTML
def markdown(markdown, **opts)
# require relevant deps on use
require 'kramdown'
require 'rouge'
require 'kramdown-parser-gfm'

opts = default_kramdown_options.merge(opts)
Kramdown::Document.new(markdown, **opts).to_html
end
Expand Down
2 changes: 1 addition & 1 deletion papercraft.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.required_ruby_version = '>= 3.2'

s.add_runtime_dependency 'sirop', '~>0.4'
s.add_runtime_dependency 'sirop', '~>0.5'
s.add_runtime_dependency 'escape_utils', '~>1.3.0'
s.add_runtime_dependency 'kramdown', '~>2.5.1'
s.add_runtime_dependency 'rouge', '~>4.5.1'
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/compiler/defer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><h1>bar</h1></div>
7 changes: 7 additions & 0 deletions test/fixtures/compiler/defer_compiled.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
->(__buffer__) {; __parts__ = [__buffer__.dup]; __buffer__.clear
__parts__ << "<div>";__parts__ << ->(__b__) {
__b__ << "<h1>#{CGI.escapeHTML((@foo).to_s)}</h1>"
}
@foo = 'bar'
__parts__ << "</div>";__parts__.each { |p| p.is_a?(Proc) ? p.(__buffer__) : (__buffer__ << p) }
}
8 changes: 8 additions & 0 deletions test/fixtures/compiler/defer_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
->() {
div {
defer {
h1 @foo
}
@foo = 'bar'
}
}
1 change: 1 addition & 0 deletions test/test_compiler.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative './helper'
require_relative '../lib/papercraft/compiler'

class CompilerTest < Minitest::Test
Dir["#{FIXTURES_PATH}/compiler/*_source.rb"].each do |fn|
Expand Down

0 comments on commit e70aaa4

Please sign in to comment.