Top Level Namespace

Defined Under Namespace

Modules: Jekyll

Instance Method Summary collapse

Instance Method Details

#check_in_context(context, path) ⇒ Object

Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|

process_page(document, payload)

end



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/jekyll-img-srcset.rb', line 181

def check_in_context(context, path)
    parts = path.split(".")
    c = context.dup
    parts.each do | part |
        if not c.is_a?(Array) and c.key? part
            c = c[part]
        else
            return nil
        end
    end
    return c
end

#format_image(ident, sizes, original_width, url, caption, title, baseurl, base_image_path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jekyll-img-srcset.rb', line 83

def format_image(ident, sizes, original_width, url, caption, title, baseurl, base_image_path)
    srcset = sizes.map {|s| "#{baseurl}/#{base_image_path}/#{s}/#{url} #{s}w"}
    srcset << "#{baseurl}/#{base_image_path}/#{url} #{original_width}w"
    srcset = srcset.join ", "
    classes = "image #{ident[:dimension]} #{ident[:classes].join " "}"
    content = "<div class=\"#{classes}\""
    if not ident[:style].nil?
        content += " style=\"#{ident[:style]}\""
    end
    content += ">"
    content += "<div>"
    srcset_sizes = if not ident[:sizes].nil? and ident[:sizes].length > 0
        ident[:sizes]
    else
        "(min-width: 1180px) 1024px, (min-width: 980px) 844px, (min-width: 740px) 640, 100vw"
    end
    if not caption.nil? and caption != ""
        content += "<figure>
            <img
                    src    = \"#{baseurl}/#{base_image_path}/#{sizes[0]}/#{url}\"
                    alt    = \"#{title}\"
                    sizes  = \"#{srcset_sizes}\"
                    srcset = \"#{srcset}\"
            />
            <figcaption>#{caption}</figcaption>
        </figure>"
    else
        content += "<img
            src    = \"#{baseurl}/#{base_image_path}/#{sizes[0]}/#{url}\"
            alt    = \"#{title}\"
            sizes  = \"#{srcset_sizes}\"
            srcset = \"#{srcset}\"
        />"
    end
    content += "</div>"
    content += "</div>"
    return content
end

#format_svg_image(url, ident, title, baseurl, base_image_path) ⇒ Object

Formats svg image data by providing the associated image tag



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jekyll-img-srcset.rb', line 123

def format_svg_image(url, ident, title, baseurl, base_image_path)
    content = "<img "
    if not ident[:style].nil?
        content += "style=\"#{ident[:style]}\" "
    end
    if not ident[:classes].empty?
        content += "class=\"#{ident[:classes].join " "}\" "
    end
    content += "src    = \"#{baseurl}/#{base_image_path}/#{url}\" "
    content += "alt    = \"#{title}\""
    content += "/>"
end

#parse_identifier(ident) ⇒ Hash

Parses the argument list

Parameters:

  • ident (String)

    the list to parse, e.g., ‘.someclass key=“value”’

Returns:

  • (Hash)

    a hash containing the sorted values



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll-img-srcset.rb', line 59

def parse_identifier(ident)
    i = {
        classes: [],
        sizes: "(min-width: 1180px) 1024px, (min-width: 980px) 844px, (min-width: 740px) 640, 100vw"
    }
    if ident.nil?
        return i
    end

    ident.scan(/#[^\s\}]+|\.[^\s\}]+|[^\s\}]+="[^"]+"/) do | match |
        if match.start_with? "#"
            match[0] = ''
            i[:id] = match
        elsif match.start_with? "."
            match[0] = ''
            i[:classes] << match
        else
            parts = match.split '='
            i[parts[0].to_sym] = parts[1].gsub(/^"/, '').gsub(/"$/, '')
        end
    end
    return i
end

#resize_image(url, widths, dest, base_image_path, cache) ⇒ Array<Numeric>, Numeric

Resizes an image to provide different versions for multiple display resolutions

Parameters:

  • url (String)

    the image’s url

  • widths (Array<Numeric>)

    target sizes to resize to; only widths smaller than the original width are generated

  • base_image_path (String)

    the base image path. Usually assets/images

  • cache (Jekyll::Cache)

    cache object to not resize if the computation was already done

Returns:

  • (Array<Numeric>, Numeric)

    the generated widths and the original iamge width



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll-img-srcset.rb', line 16

def resize_image(url, widths, dest, base_image_path, cache)
    src_path = File.join(base_image_path, url)
    src_mtime = File.new(src_path).mtime
    if cache.key? src_path
        _, _, modified_time = cache[src_path]
        if src_mtime > modified_time
            cache.delete src_path
        end
    end
    # resize images if they aren't already in the cache
    new_widths, w, modified_time = cache.getset(src_path) do
        modified_time = File.new(src_path).mtime
        image = MiniMagick::Image.open(src_path)
        w = image.dimensions[0]
        aspect = image.dimensions[1].to_f / w
        [w, aspect] 
        new_widths = widths.select {|x| x <= w}
        width_to_create = new_widths.map do |width| 
            [width, File.join(dest, base_image_path, "#{width}", url)]
        end
        .select do |width, target| 
            (not File.exists? target) or (File.new(target).mtime < src_mtime)
        end
        Parallel.each(width_to_create) do |width, target|
            image = MiniMagick::Image.open(src_path)
            image.resize "#{width}x#{width*aspect}"
            if not Dir.exists? File.dirname(target)
                FileUtils.mkdir_p File.dirname(target)
            end
            image.write target
        end
        [new_widths, w, modified_time]
    end

    return new_widths, w
end