Sha256: bf37f2e081a3ca77c95f1f7038f3cc264760c0ad6eb7a311111c7d814a8f38f7

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module TopHat
  module HtmlHelper

    def html_tag(options={})
      opts = options.dup
      opts.merge!(xmlns_options(opts.delete(:xmlns))) if opts[:xmlns]
      opts.merge!(prefix_options(opts.delete(:prefix))) if opts[:prefix]

      tag(:html, opts, true)
    end

    private

    def xmlns_options(xmlns)
      options = {}

      if xmlns.kind_of?(String)
        options['xmlns'] = xmlns
      elsif xmlns.kind_of?(Array)
        options.merge!(xmlns_from_array(xmlns))
      elsif xmlns.kind_of?(Hash)
        options.merge!(xmlns_from_hash(xmlns))
      end

      options
    end

    def xmlns_from_array(xmlns)
      options = {}

      xmlns.each do |x|
        if x.kind_of?(Hash)
          options.merge!(xmlns_from_hash(x))
        else
          options['xmlns'] = x
        end
      end

      options
    end

    def xmlns_from_hash(h)
      { "xmlns:#{h[:prefix]}" => h[:url] }
    end

    def prefix_options(prefix)
      options = {}

      options['prefix'] = case prefix
      when String then prefix
      when Hash then prefix_from_hash(prefix)
      when Array then prefix_from_array(prefix)
      end

      options
    end

    def prefix_from_array(prefixes)
      prefixes.map do |prefix|
        if prefix.kind_of?(Hash)
          prefix_from_hash(prefix)
        else
          prefix
        end
      end.join(' ')
    end

    def prefix_from_hash(h)
      "#{h[:prefix]}: #{h[:url]}"
    end

  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/tophat-2.3.1/lib/tophat/html.rb
tophat-2.3.1 lib/tophat/html.rb
tophat-2.3.0 lib/tophat/html.rb
tophat-2.2.1 lib/tophat/html.rb
tophat-2.2.0 lib/tophat/html.rb
tophat-2.1.0 lib/tophat/html.rb