Top Level Namespace

Defined Under Namespace

Modules: Jekyll

Instance Method Summary collapse

Instance Method Details

#check_in_context(context, path) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/jekyll-img-srcset.rb', line 168

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



70
71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/jekyll-img-srcset.rb', line 70

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jekyll-img-srcset.rb', line 110

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll-img-srcset.rb', line 46

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

#process_page(document, payload) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/jekyll-img-srcset.rb', line 123

def process_page(document, payload)
    # see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841
    doc_ext = document.extname.tr('.', '')
    cache = Jekyll::Cache.new("jekyll-img-srcset")
    if payload['site']['markdown_ext'].include? doc_ext
        content = document.content
        base_image_path = document.site.config["base_image_path"] || "assets/images"
        image_regex = /!\[(?<caption>.*?)?\]\((?<url>\S+?)(?<title> .+?)?\)(?<identifier>\{.+?\})?/ 
        content = content.gsub(image_regex) do | img |
            img = image_regex.match img
            if (/\.(jpe?g|png)$/i =~ img["url"]).nil?
                "#{base_image_path}/#{img[0]}"
            elsif img["url"].start_with? "http"
                img[0]
            elsif img["url"].end_with? ".svg"
                ident = parse_identifier img[:identifier]
                format_svg_image(img["url"], ident, img["title"],
                    document.site.baseurl)
            else
                if File.exists?(File.join base_image_path, img['url'])
                    widths, original_width = resize_image(img['url'], 
                        document.site.config["widths"],
                        document.site.config["destination"], 
                        base_image_path, cache)
                    document.site.keep_files.concat(widths.map do |w|
                        File.join(base_image_path, "#{w}", img['url'])
                    end)
                    ident = parse_identifier img[:identifier]
                    format_image(ident, widths, original_width,
                        img["url"], img["caption"], img["title"],
                        document.site.baseurl, base_image_path)
                else
                    img[0]
                end
            end
        end
        # puts content
        document.content = content
    end
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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-img-srcset.rb', line 15

def resize_image(url, widths, dest, base_image_path, cache)
    src_path = File.join(base_image_path, url)
    new_widths, w = cache.getset(src_path) do
        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}
        new_widths.map do |width| 
            [width, File.join(dest, base_image_path, "#{width}", url)]
        end.select do |width, target| 
            not File.exists? target
        end.each 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]
    end
    return new_widths, w
end