Module: Nanoc::Helpers::Capturing
- Included in:
- Filtering, HTMLEscape, Rendering
- Defined in:
- lib/nanoc/helpers/capturing.rb
Overview
Provides functionality for “capturing” content in one place and reusing this content elsewhere.
For example, suppose you want the sidebar of your site to contain a short summary of the item. You could put the summary in the meta file, but that’s not possible when the summary contains eRuby. You could also put the sidebar inside the actual item, but that’s not very pretty. Instead, you write the summary on the item itself, but capture it, and print it in the sidebar layout.
This helper has been tested with ERB and Haml. Other filters may not work correctly.
Instance Method Summary collapse
-
#capture(&block) ⇒ String
Evaluates the given block and returns its contents.
-
#content_for(*args, &block) ⇒ Object
Instance Method Details
#capture(&block) ⇒ String
Evaluates the given block and returns its contents. The contents of the block is not outputted.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/nanoc/helpers/capturing.rb', line 174 def capture(&block) # Get erbout so far erbout = eval('_erbout', block.binding) erbout_length = erbout.length # Execute block yield # Get new piece of erbout erbout_addition = erbout[erbout_length..-1] # Remove addition erbout[erbout_length..-1] = '' # Depending on how the filter outputs, the result might be a # single string or an array of strings (slim outputs the latter). erbout_addition = erbout_addition.join if erbout_addition.is_a? Array # Done. erbout_addition end |
#content_for(name, params = {}, &block) ⇒ void #content_for(item, name) ⇒ String
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/nanoc/helpers/capturing.rb', line 96 def content_for(*args, &block) if block_given? # Set content # Get args case args.size when 1 name = args[0] params = {} when 2 name = args[0] params = args[1] else raise ArgumentError, 'expected 1 or 2 argument (the name ' \ "of the capture, and optionally params) but got #{args.size} instead" end name = args[0] existing_behavior = params.fetch(:existing, :error) # Capture content = capture(&block) # Prepare for store store = @site.unwrap.captures_store case existing_behavior when :overwrite store[@item, name.to_sym] = '' when :append store[@item, name.to_sym] ||= '' when :error if store[@item, name.to_sym] && store[@item, name.to_sym] != content raise "a capture named #{name.inspect} for #{@item.identifier} already exists" else store[@item, name.to_sym] = '' 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 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] name = args[1] # Create dependency if @item.nil? || item != @item.unwrap Nanoc::Int::NotificationCenter.post(:visit_started, item) Nanoc::Int::NotificationCenter.post(:visit_ended, item) # 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 end end # Get content @site.unwrap.captures_store[item, name.to_sym] end end |