Sha256: c086f8f9b28b0c845c913a5a2f9c011faddcc071aaa38027936d6b398d7e45c7

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

module PictureTag
  module Parsers
    # This class takes in the arguments passed to the liquid tag, and splits it
    # up into 'words' (correctly handling quotes and backslash escapes.)
    #
    # To handle quotes and backslash escaping, we have to parse the string by
    # characters to break it up correctly. I'm sure there's a library to do
    # this, but it's not that much code honestly. If this starts getting big,
    # we'll pull in a new dependency.
    #
    class ArgSplitter
      attr_reader :words

      def initialize(raw_params)
        @words = []
        @word = ''
        @in_quotes = false
        @escaped = false

        raw_params.each_char { |c| handle_char(c) }

        add_word # We have to explicitly add the last one.
      end

      private

      def handle_char(char)
        # last character was a backslash:
        if @escaped
          close_escape char

        # char is a backslash or a quote:
        elsif char.match?(/["\\]/)
          handle_special char

        # Character isn't whitespace, or it's inside double quotes:
        elsif @in_quotes || char.match(/\S/)
          @word << char

        # Character is whitespace outside of double quotes:
        else
          add_word
        end
      end

      def add_word
        return if @word.empty?

        @words << @word
        @word = ''
      end

      def handle_special(char)
        case char
        when '\\'
          @escaped = true
        when '"'
          @in_quotes = !@in_quotes
          @word << char
        end
      end

      def close_escape(char)
        @word << char
        @escaped = false
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
jekyll_picture_tag-2.1.2 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.1.1 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.1.0 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.0.4 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.0.3 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.0.2 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.0.1 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.0.0 lib/jekyll_picture_tag/parsers/arg_splitter.rb
jekyll_picture_tag-2.0.0pre1 lib/jekyll_picture_tag/parsers/arg_splitter.rb