Class: Jekyll::SrcsetTag::SrcsetTag

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ SrcsetTag

Returns a new instance of SrcsetTag.



9
10
11
12
# File 'lib/jekyll/srcset_tag/srcset_tag.rb', line 9

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

Instance Method Details

#image(source_path, output_path, web_output_path, markup, sources) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll/srcset_tag/srcset_tag.rb', line 45

def image(source_path, output_path, web_output_path, markup, sources)
  markup = markup_regex.match(markup)
  unless markup
    raise "srcset tag doesn't look right - it should be {% srcset image_src [ppi:1,2] [html_attributes] %}"
  end
  Image.new(source_path: source_path,
            output_path: output_path,
            web_output_path: web_output_path,
            image_path: markup[:image_src],
            ppi: markup[:ppi] || 1,
            sources: sources,
            html_attributes: markup[:html_attr])
end

#markup_regexObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jekyll/srcset_tag/srcset_tag.rb', line 59

def markup_regex
  %r{
    ^
    (?<image_src>[^\s]+\.[a-zA-Z0-9]{3,4})
    \s*
    (ppi:(?<ppi>(\d(\.\d\d?)?,)*\d(\.\d\d?)?))?
    \s*
    (?<html_attr>[\s\S]+)?
    $
  }x
end

#render(context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll/srcset_tag/srcset_tag.rb', line 14

def render(context)
  render_markup = Liquid::Template.parse(@markup)
                                  .render(context)
                                  .gsub(/\\\{\\\{|\\\{\\%/, '\{\{' => '{{', '\{\%' => '{%')

  # set keep files
  settings = srcset_settings(context)
  site = context.registers[:site]
  site.config['keep_files'] << settings['output'] unless site.config['keep_files'].include?(settings['output'])

  image = image File.join(site.source, settings['source']),
                File.join(site.dest, settings['output']),
                '/' + settings['output'],
                render_markup,
                sources(super)
  image.generate_images!
  image.to_html
end

#sources(content) ⇒ Object



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

def sources(content)
  # return sources objects
  html = Nokogiri::HTML(content)
  html.css('source').map do |source|
    Image::Source.new(width: source.attr('width'),
                      height: source.attr('height'),
                      media: source.attr('media'),
                      size: source.attr('size'))
  end

end

#srcset_settings(context) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/jekyll/srcset_tag/srcset_tag.rb', line 71

def srcset_settings(context)
  settings = context.registers[:site].config['srcset']
  settings ||= {}
  settings['source'] ||= '_assets/images/fullsize'
  settings['output'] ||= 'images/generated'
  settings
end