Sha256: b2c45c2d83a4a998021213e8edc7bab1c00e643926cda7480218bb2cbb6d09ce

Contents?: true

Size: 1.11 KB

Versions: 43

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

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

43 entries across 43 versions & 1 rubygems

Version Path
nanoc-4.8.17 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.16 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.15 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.14 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.13 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.12 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.11 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.10 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.9 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.8 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.7 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.6 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.5 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.4 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.3 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.2 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.1 lib/nanoc/helpers/html_escape.rb
nanoc-4.8.0 lib/nanoc/helpers/html_escape.rb
nanoc-4.7.14 lib/nanoc/helpers/html_escape.rb
nanoc-4.7.13 lib/nanoc/helpers/html_escape.rb