Class: Jekyll::SrcsetTag::SrcsetSourceTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/srcset_tag/srcset_source_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ SrcsetSourceTag

Returns a new instance of SrcsetSourceTag.



7
8
9
10
# File 'lib/jekyll/srcset_tag/srcset_source_tag.rb', line 7

def initialize(tag_name, markup, tokens)
  @markup = markup
  super
end

Instance Method Details

#markup_regexObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll/srcset_tag/srcset_source_tag.rb', line 29

def markup_regex
  %r{
    \w+
    \s*
    \:
    \s*
    (?:
      "(?:[^"\\]|\\.)*"
      |
      '(?:[^'\\]|\\.)*'
      |
      [^\s]+
    )
  }x
end

#markup_to_hash(markup) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/jekyll/srcset_tag/srcset_source_tag.rb', line 21

def markup_to_hash(markup)
  matches = markup.scan(markup_regex)
  matches.each_with_object({}) do |match, memo|
    key, value = match.split(':', 2)
    memo[key.strip] = value.gsub(/\A("|')|("|')\Z/, '')
  end
end

#render(context) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/jekyll/srcset_tag/srcset_source_tag.rb', line 12

def render(context)
  render_markup = Liquid::Template.parse(@markup)
                                  .render(context)
                                  .gsub(/\\\{\\\{|\\\{\\%/, '\{\{' => '{{', '\{\%' => '{%')
  hash = markup_to_hash(render_markup)
  markup = hash.map { |(key, value)| "#{key}=\"#{CGI.escape_html(value)}\""}
  '<source ' + markup.join(' ') +  ' />'
end