Sha256: 325cbeb1d0e612c5c5d7eb1e16a13c4c273011933219aa3ae5b4edc69080cb1b

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module PictureTag
  module Instructions
    # Handles HTML attributes, sourced from configuration and the liquid tag,
    # sent to various elements.
    # Stored as a hash, with string keys.
    class HTMLAttributeSet
      # Initialize with leftovers passed into the liquid tag
      def initialize(params)
        @content = load_preset

        parse_params(params) if params
        handle_source_url
      end

      def [](key)
        @content[key]
      end

      private

      def load_preset
        PictureTag.preset['attributes'].dup || {}
      end

      # Syntax this function processes:
      # class="old way" --picture class="new way" --alt Here's my alt text
      def parse_params(words)
        key = 'implicit'

        words.each do |word|
          if word.match(/^--/)
            key = word.delete_prefix('--')
          elsif @content[key]
            @content[key] << ' ' + word
          else
            @content[key] = word
          end
        end
      end

      def handle_source_url
        return unless PictureTag.preset['link_source'] && self['link'].nil?

        target = PictureTag.source_images.first.shortname

        @content['link'] = ImgURI.new(target, source_image: true).to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll_picture_tag-1.7.0 lib/jekyll_picture_tag/instructions/html_attributes.rb