Class: Liquid::Tag::Parser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/liquid/tag/parser.rb,
lib/liquid/tag/parser/version.rb

Constant Summary collapse

FALSE =

'!'
FLOAT =
%r!\A\d+\.\d+\Z!.freeze
QUOTE =
%r!(["'])([^\1]*)(\1)!.freeze
SPECIAL =
%r{(?<!\\)([@!:=])}.freeze
BOOL =
%r{\A(?<!\\)([!@])([\w:]+)\z}.freeze
UNQUOTED_SPECIAL =
%r{(?<!\\)(://)}.freeze
SPECIAL_ESCAPED =
%r{\\([@!:=])}.freeze
KEY =
%r{\b(?<!\\):}.freeze
INT =
%r!^\d+$!.freeze
TRUE =
'@'
VERSION =
'2.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, defaults: {}, sep: '=') ⇒ Parser

Returns a new instance of Parser.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/liquid/tag/parser.rb', line 53

def initialize(raw, defaults: {}, sep: '=')
  @sep = sep
  @unescaped_sep = sep
  @rsep = Regexp.escape(sep)
  @escaped_sep_regexp = %r!\\(#{@rsep})!
  @sep_regexp = %r{\b(?<!\\)(#{@rsep})}
  @escaped_sep = "\\#{@sep}"
  @args = defaults
  @raw = raw

  parse
end

Instance Attribute Details

#argsObject (readonly) Also known as: raw_args

Returns the value of attribute args.



12
13
14
# File 'lib/liquid/tag/parser.rb', line 12

def args
  @args
end

Instance Method Details

#skippable_loop(skip: [], hash: false) ⇒ Hash<Symbol,Object>, Array<String>

Consumes a block and wraps around reusably on arguments.

Returns:

  • (Hash<Symbol,Object>, Array<String>)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/liquid/tag/parser.rb', line 70

def skippable_loop(skip: [], hash: false)
  @args.each_with_object(hash ? {} : []) do |(key, val), obj|
    next obj if skip_in_html?(key: key, val: val, skips: skip)
    yield(
      [
        key,
        val
      ],
      obj
    )
  end
end

#to_h(skip: [], html: false) ⇒ Hash<Object>, Array<String>

Converts arguments into an HTML hash (or to arguments).

Parameters:

  • skip (Array<Symbol>) (defaults to: [])

    keys to skip.

  • html (true, false) (defaults to: false)

    skip non-html values.

Returns:

  • (Hash<Object>, Array<String>)


100
101
102
103
104
105
106
# File 'lib/liquid/tag/parser.rb', line 100

def to_h(skip: [], html: false)
  return @args unless html

  skippable_loop(skip: skip, hash: true) do |(k, v), o|
    o[k] = v
  end
end

#to_html(skip: []) ⇒ String

Converts the arguments into an HTML attribute string.

Parameters:

  • skip (Array<Symbol>) (defaults to: [])

    keys to skip.

Returns:

  • (String)


88
89
90
91
92
# File 'lib/liquid/tag/parser.rb', line 88

def to_html(skip: [])
  skippable_loop(skip: skip, hash: false) do |(k, v), o|
    o << (v == true ? k.to_s : "#{k}=\"#{v}\"")
  end.join(' ')
end