diff --git a/CHANGELOG.md b/CHANGELOG.md index b9de5dc..b0d873d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.3 2024-12-16 + +- Update dependencies + ## 1.2 2023-08-21 - Update dependencies diff --git a/Gemfile.lock b/Gemfile.lock index 00295cf..b7001d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,9 +3,9 @@ PATH specs: papercraft (1.2) escape_utils (~> 1.3.0) - kramdown (~> 2.4.0) + kramdown (~> 2.5.1) kramdown-parser-gfm (~> 1.1.0) - rouge (~> 4.2.0) + rouge (~> 4.5.1) sirop (~> 0.4) GEM @@ -14,14 +14,14 @@ GEM benchmark-ips (2.7.2) erubis (2.7.0) escape_utils (1.3.0) - kramdown (2.4.0) - rexml + kramdown (2.5.1) + rexml (>= 3.3.9) kramdown-parser-gfm (1.1.0) kramdown (~> 2.0) - minitest (5.22.3) + minitest (5.25.4) prism (0.27.0) - rexml (3.2.6) - rouge (4.2.1) + rexml (3.4.0) + rouge (4.5.1) sirop (0.4) prism (~> 0.27.0) tilt (2.2.0) @@ -34,7 +34,7 @@ PLATFORMS DEPENDENCIES benchmark-ips (~> 2.7.2) erubis (~> 2.7.0) - minitest (~> 5.22.0) + minitest (~> 5.25.4) papercraft! tilt (~> 2.2.0) diff --git a/lib/papercraft/compiler.rb b/lib/papercraft/compiler.rb index ccc0050..b6d78de 100644 --- a/lib/papercraft/compiler.rb +++ b/lib/papercraft/compiler.rb @@ -39,6 +39,7 @@ def initialize end def compile(node) + @root_node = node inject_buffer_parameter(node) @buffer.clear @@ -96,7 +97,13 @@ def flush_html_buffer if @last_loc_start adjust_whitespace(@html_location_start) if @html_location_start end - @buffer << "__buffer__ << \"#{@html_buffer}\"" + if @defer_proc_mode + @buffer << "__b__ << \"#{@html_buffer}\"" + elsif @defer_mode + @buffer << "__parts__ << \"#{@html_buffer}\"" + else + @buffer << "__buffer__ << \"#{@html_buffer}\"" + end @html_buffer.clear @last_loc_end = loc_end(@html_location_end) if @html_location_end @@ -114,6 +121,10 @@ def visit_call_node(node) emit_html_text(node) when :emit emit_html_emit(node) + when :emit_yield + raise NotImplementedError, "emit_yield is not yet supported in compiled templates" + when :defer + emit_html_deferred(node) else emit_html_tag(node) end @@ -216,4 +227,37 @@ def emit_html_emit(node) embed_visit(node.arguments, '#{Papercraft.render_emit_call(', ')}') end + + def emit_html_deferred(node) + raise NotImplementedError, "#defer in embed mode is not supported in compiled templates" if @embed_mode + + block = node.block + return if not block + + setup_defer_mode if !@defer_mode + + flush_html_buffer + @buffer << ';__parts__ << ->(__b__) ' + @defer_proc_mode = true + visit(node.block) + @defer_proc_mode = nil + end + + DEFER_PREFIX_EMPTY = "; __parts__ = []" + DEFER_PREFIX_NOT_EMPTY = "; __parts__ = [__buffer__.dup]; __buffer__.clear" + DEFER_POSTFIX = ";__parts__.each { |p| p.is_a?(Proc) ? p.(__buffer__) : (__buffer__ << p) }" + + def setup_defer_mode + @defer_mode = true + if @html_buffer && !@html_buffer.empty? + @buffer << DEFER_PREFIX_NOT_EMPTY + else + @buffer << DEFER_PREFIX_EMPTY + end + + @root_node.after_body do + flush_html_buffer + @buffer << DEFER_POSTFIX + end + end end diff --git a/lib/papercraft/version.rb b/lib/papercraft/version.rb index 4df5754..d3c318f 100644 --- a/lib/papercraft/version.rb +++ b/lib/papercraft/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Papercraft - VERSION = '1.2' + VERSION = '1.3' end diff --git a/papercraft.gemspec b/papercraft.gemspec index 7c4b210..1eddc7a 100644 --- a/papercraft.gemspec +++ b/papercraft.gemspec @@ -23,11 +23,11 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'sirop', '~>0.4' s.add_runtime_dependency 'escape_utils', '~>1.3.0' - s.add_runtime_dependency 'kramdown', '~>2.4.0' - s.add_runtime_dependency 'rouge', '~>4.2.0' + s.add_runtime_dependency 'kramdown', '~>2.5.1' + s.add_runtime_dependency 'rouge', '~>4.5.1' s.add_runtime_dependency 'kramdown-parser-gfm', '~>1.1.0' - s.add_development_dependency 'minitest', '~>5.22.0' + s.add_development_dependency 'minitest', '~>5.25.4' s.add_development_dependency 'benchmark-ips', '~>2.7.2' s.add_development_dependency 'erubis', '~>2.7.0' s.add_development_dependency 'tilt', '~>2.2.0'