Module: Nanoc::Helpers::HTMLEscape

Includes:
Capturing
Included in:
LinkTo, Tagging
Defined in:
lib/nanoc/helpers/html_escape.rb

Overview

Instance Method Summary collapse

Methods included from Capturing

#capture, #content_for

Instance Method Details

#html_escape(string = nil, &block) ⇒ String Also known as: h

Parameters:

  • string (String) (defaults to: nil)

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nanoc/helpers/html_escape.rb', line 12

def html_escape(string = nil, &block)
  if block_given?
    # Capture and escape block
    data = capture(&block)
    escaped_data = html_escape(data)

    # Append filtered data to buffer
    buffer = eval('_erbout', block.binding)
    buffer << escaped_data
  elsif string
    unless string.is_a? String
      raise ArgumentError, 'The #html_escape or #h function needs either a ' \
        "string or a block to HTML-escape, but #{string.class} was given"
    end

    string
      .gsub('&', '&amp;')
      .gsub('<', '&lt;')
      .gsub('>', '&gt;')
      .gsub('"', '&quot;')
      .gsub("'", '&#39;')
  else
    raise 'The #html_escape or #h function needs either a ' \
      'string or a block to HTML-escape, but neither a string nor a block was given'
  end
end