Sha256: ce052a62c1e52d08ae817057676378a1803b17f9e79aaad319ccc5206791a6c0

Contents?: true

Size: 1.08 KB

Versions: 37

Compression:

Stored size: 1.08 KB

Contents

module Nanoc::Helpers
  # @see http://nanoc.ws/doc/reference/helpers/#filtering
  module HTMLEscape
    require 'nanoc/helpers/capturing'
    include Nanoc::Helpers::Capturing

    # @param [String] string
    #
    # @return [String]
    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;')
      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

    alias h html_escape
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
nanoc-4.4.7 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.6 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.5 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.4 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.3 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.2 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.1 lib/nanoc/helpers/html_escape.rb
nanoc-4.4.0 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.8 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.7 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.6 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.5 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.4 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.3 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.2 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.1 lib/nanoc/helpers/html_escape.rb
nanoc-4.3.0 lib/nanoc/helpers/html_escape.rb