lib/nanoc/helpers/capturing.rb in nanoc-4.1.6 vs lib/nanoc/helpers/capturing.rb in nanoc-4.2.0b1

- old
+ new

@@ -23,44 +23,10 @@ # <div id="sidebar"> # <h3>Summary</h3> # <%= content_for(@item, :summary) || '(no summary)' %> # </div> module Capturing - # @api private - class CapturesStore - def initialize - @store = {} - end - - def []=(item, name, content) - @store[item.identifier] ||= {} - @store[item.identifier][name] = content - end - - def [](item, name) - @store[item.identifier] ||= {} - @store[item.identifier][name] - end - - def reset_for(item) - @store[item.identifier] = {} - end - end - - class ::Nanoc::Int::Site - # @api private - def captures_store - @captures_store ||= CapturesStore.new - end - - # @api private - def captures_store_compiled_items - require 'set' - @captures_store_compiled_items ||= Set.new - end - end - # @overload content_for(name, params = {}, &block) # # Captures the content inside the block and stores it so that it can be # referenced later on. The same method, {#content_for}, is used for # getting the captured content as well as setting it. When capturing, @@ -112,59 +78,51 @@ # Capture content = capture(&block) # Prepare for store - store = @site.unwrap.captures_store + snapshot_contents = @item.reps[:default].unwrap.snapshot_contents + capture_name = "__capture_#{name}".to_sym case existing_behavior when :overwrite - store[@item, name.to_sym] = '' + snapshot_contents[capture_name] = '' when :append - store[@item, name.to_sym] ||= '' + snapshot_contents[capture_name] ||= '' when :error - if store[@item, name.to_sym] && store[@item, name.to_sym] != content + if snapshot_contents[capture_name] && snapshot_contents[capture_name] != content + # FIXME: get proper exception raise "a capture named #{name.inspect} for #{@item.identifier} already exists" else - store[@item, name.to_sym] = '' + snapshot_contents[capture_name] = '' end else raise ArgumentError, 'expected :existing_behavior param to #content_for to be one of ' \ ":overwrite, :append, or :error, but #{existing_behavior.inspect} was given" end # Store - @site.unwrap.captures_store_compiled_items << @item.unwrap - store[@item, name.to_sym] << content + snapshot_contents[capture_name] << content else # Get content - # Get args if args.size != 2 raise ArgumentError, 'expected 2 arguments (the item ' \ "and the name of the capture) but got #{args.size} instead" end - item = args[0].is_a?(Nanoc::ItemWithRepsView) ? args[0].unwrap : args[0] + item = args[0] name = args[1] + rep = item.reps[:default].unwrap + # Create dependency if @item.nil? || item != @item.unwrap - Nanoc::Int::NotificationCenter.post(:visit_started, item) - Nanoc::Int::NotificationCenter.post(:visit_ended, item) + dependency_tracker = @config._context.dependency_tracker + dependency_tracker.bounce(item.unwrap) - # This is an extremely ugly hack to get the compiler to recompile the - # item from which we use content. For this, we need to manually edit - # the content attribute to reset it. :( - # FIXME: clean this up - unless @site.unwrap.captures_store_compiled_items.include?(item) - @site.unwrap.captures_store.reset_for(item) - item.forced_outdated = true - @site.unwrap.compiler.reps[item].each do |r| - r.snapshot_contents = { last: item.content } - raise Nanoc::Int::Errors::UnmetDependency.new(r) - end + unless rep.compiled? + raise Nanoc::Int::Errors::UnmetDependency.new(rep) end end - # Get content - @site.unwrap.captures_store[item, name.to_sym] + rep.snapshot_contents["__capture_#{name}".to_sym] end end # Evaluates the given block and returns its contents. The contents of the # block is not outputted.