Module: Nanoc::Helpers::Capturing

Included in:
SetContent, Filtering, HTMLEscape, Rendering
Defined in:
lib/nanoc/helpers/capturing.rb

Overview

Defined Under Namespace

Classes: GetContent, SetContent

Instance Method Summary collapse

Instance Method Details

#capture(&block) ⇒ String

Returns:

  • (String)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/nanoc/helpers/capturing.rb', line 149

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..]

  # 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, &block) ⇒ void #content_for(name, params, &block) ⇒ void #content_for(name, content) ⇒ void #content_for(name, params, content) ⇒ void #content_for(item, name) ⇒ String

Overloads:

  • #content_for(name, &block) ⇒ void

    This method returns an undefined value.

    Parameters:

    • name (Symbol, String)
  • #content_for(name, params, &block) ⇒ void

    This method returns an undefined value.

    Parameters:

    • name (Symbol, String)

    Options Hash (params):

    • existing (Symbol)
  • #content_for(name, content) ⇒ void

    This method returns an undefined value.

    Parameters:

    • name (Symbol, String)
    • content (String)
  • #content_for(name, params, content) ⇒ void

    This method returns an undefined value.

    Parameters:

    • name (Symbol, String)
    • content (String)

    Options Hash (params):

    • existing (Symbol)
  • #content_for(item, name) ⇒ String

    Parameters:

    • name (Symbol, String)

    Returns:

    • (String)


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
# File 'lib/nanoc/helpers/capturing.rb', line 105

def content_for(*args, &)
  if block_given? # Set content
    name = args[0]
    params =
      case args.size
      when 1
        {}
      when 2
        args[1]
      else
        raise ArgumentError, 'expected 1 or 2 argument (the name ' \
          "of the capture, and optionally params) but got #{args.size} instead"
      end

    SetContent.new(name, params, @item).run(&)
  elsif args.size > 1 && (args.first.is_a?(Symbol) || args.first.is_a?(String)) # Set content
    name = args[0]
    content = args.last
    params =
      case args.size
      when 2
        {}
      when 3
        args[1]
      else
        raise ArgumentError, 'expected 2 or 3 arguments (the name ' \
          "of the capture, optionally params, and the content) but got #{args.size} instead"
      end

    _erbout = +'' # rubocop:disable Lint/UnderscorePrefixedVariableName
    SetContent.new(name, params, @item).run { _erbout << content }
  else # Get content
    if args.size != 2
      raise ArgumentError, 'expected 2 arguments (the item ' \
        "and the name of the capture) but got #{args.size} instead"
    end
    requested_item = args[0]
    name = args[1]

    GetContent.new(requested_item, name, @item, @config).run
  end
end