visualci-gallery 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,7 +14,7 @@ module VisualCI
14
14
  out = []
15
15
  size = @m['layout'] == 'strip' ? strip_size : @m['groups'][0]['sizes'][0]
16
16
  out << %(<section class="vg" data-vg data-vg-layout="#{@m['layout']}" aria-label="#{esc(@m['label'])}" ) +
17
- %(data-vg-size="#{size}"#{@m['themeSwitch'] ? '' : ' data-vg-theme-source="page"'}>)
17
+ %(data-vg-size="#{size}"#{@m['themeSwitch'] ? '' : ' data-vg-theme-source="page"'}#{preload_attr}>)
18
18
  out << "<#{h(0)} class=\"vg-title\">#{esc(@m['title'])}</#{h(0)}>" unless @m['title'].nil?
19
19
  @m['layout'] == 'strip' ? strip(out) : grid(out)
20
20
  if (see = @m['seeAll'])
@@ -58,10 +58,32 @@ module VisualCI
58
58
  '</div>'
59
59
  end
60
60
 
61
+ # Preloading with the defaults needs no attribute; off, or with caps of the site's own, says so.
62
+ def preload_attr
63
+ p = @m['preload']
64
+ return '' if p.nil?
65
+ return ' data-vg-preload="off"' if p == 'off'
66
+
67
+ %( data-vg-preload="fold=#{p['fold']} count=#{p['count']} bytes=#{p['bytes']} parallel=#{p['parallel']}")
68
+ end
69
+
70
+ # The sizes the preloader caps by, where the manifest gives them: of the card's image (src), of the full-size
71
+ # one when the card shows a thumbnail, and of the 2x copy.
72
+ def bytes_attrs(v)
73
+ thumb = v['thumb']
74
+ shown = thumb ? thumb['bytes'] : v['bytes']
75
+ (shown.nil? ? '' : %( data-vg-bytes="#{shown}")) +
76
+ (thumb && !v['bytes'].nil? ? %( data-vg-full-bytes="#{v['bytes']}") : '') +
77
+ (v['url2x'] && !v['bytes2x'].nil? ? %( data-vg-2x-bytes="#{v['bytes2x']}") : '')
78
+ end
79
+
61
80
  def image(title, v, themes)
62
81
  alt = "#{title}, #{SIZE_LABEL[v['size']].downcase}, #{themes.join(' and ')}"
63
- %(<img class="vg-img" data-vg-size="#{v['size']}" data-vg-theme="#{themes.join(' ')}" src="#{esc(v['url'])}") +
82
+ thumb = v['thumb']
83
+ %(<img class="vg-img" data-vg-size="#{v['size']}" data-vg-theme="#{themes.join(' ')}" src="#{esc(thumb ? thumb['url'] : v['url'])}") +
84
+ (thumb ? %( data-src-full="#{esc(v['url'])}") : '') +
64
85
  (v['url2x'] ? %( data-src-2x="#{esc(v['url2x'])}") : '') +
86
+ bytes_attrs(v) +
65
87
  %( width="#{v['width']}" height="#{v['height']}" alt="#{esc(alt)}" loading="lazy" decoding="async">)
66
88
  end
67
89
 
@@ -76,7 +98,8 @@ module VisualCI
76
98
  shown << size
77
99
  first ||= at[0]
78
100
  # Every size has both themes (the model refuses a screen without); the same image for both is shown once.
79
- if at[0]['url'] == at[1]['url'] && at[0]['url2x'] == at[1]['url2x']
101
+ if at[0]['url'] == at[1]['url'] && at[0]['url2x'] == at[1]['url2x'] &&
102
+ at[0]['thumb']&.dig('url') == at[1]['thumb']&.dig('url')
80
103
  imgs << image(sc['title'], at[0], %w[light dark])
81
104
  else
82
105
  at.each { |v| imgs << image(sc['title'], v, [v['theme']]) }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module VisualCI
4
4
  module Gallery
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -30,6 +30,10 @@ module VisualCI
30
30
  # The largest width or height (SPEC.md, "Validation"): the density maths stays exact in every language.
31
31
  MAX_SIZE = 1_000_000
32
32
  SIZE_LABEL = { 'desktop' => 'Desktop', 'phone' => 'Phone' }.freeze
33
+ # The largest size in bytes or preload cap: a whole number every language holds exactly.
34
+ MAX_CAP = 1_000_000_000_000
35
+ # The preloader's caps when the site sets none (SPEC.md, "Options").
36
+ PRELOAD_DEFAULTS = { 'fold' => 250, 'count' => 500, 'bytes' => 50_000_000, 'parallel' => 4 }.freeze
33
37
 
34
38
  # Whole-string matches, ASCII digits, and `.` matching line breaks too (/m), as SPEC.md says.
35
39
  EXTENSION = /\.(?:[pP][nN][gG]|[jJ][pP][eE]?[gG]|[wW][eE][bB][pP])\z/
@@ -82,7 +86,7 @@ module VisualCI
82
86
 
83
87
  # The model (SPEC.md, "The model") as a Hash with the same keys as the fixtures' model.json.
84
88
  def model(manifest, title: UNSET, heading_level: nil, id: nil, screens: nil, order: nil, groups: nil,
85
- group_order: nil, credit: nil, layout: nil, theme_switch: nil, see_all: nil)
89
+ group_order: nil, credit: nil, layout: nil, theme_switch: nil, see_all: nil, preload: nil)
86
90
  check_manifest(manifest)
87
91
  # Options in the contract's order (SPEC.md, "Validation"): the first problem is the error.
88
92
  level = opt(heading_level, 'headingLevel') { |v| integral?(v) }
@@ -96,6 +100,7 @@ module VisualCI
96
100
  switch = opt(theme_switch, 'themeSwitch') { |v| boolean?(v) }
97
101
  layout = opt(layout, 'layout') { |v| %w[grid strip].include?(v.to_s) }&.to_s || 'grid'
98
102
  see_all = opt(see_all, 'seeAll') { |v| see_all?(v) }
103
+ preload = opt(preload, 'preload') { |v| preload?(v) }
99
104
  model = {
100
105
  'layout' => layout,
101
106
  'themeSwitch' => layout == 'grid' && switch != false,
@@ -105,6 +110,7 @@ module VisualCI
105
110
  'headingLevel' => (level ? level.to_i : 2).clamp(1, 4),
106
111
  'id' => id,
107
112
  'credit' => credit.nil? ? true : credit,
113
+ 'preload' => preload_model(preload),
108
114
  'groups' => build_groups(manifest, screens_opt, groups_opt, order, group_order)
109
115
  }
110
116
  raise Error, 'the gallery manifest has no screenshots to show' if model['groups'].empty?
@@ -196,15 +202,24 @@ module VisualCI
196
202
  variants = (screens[shot[:key]] ||= {})
197
203
  (shot[:theme] ? [shot[:theme]] : THEMES).each do |theme|
198
204
  v = (variants["#{shot[:size]}-#{theme}"] ||= { 'size' => shot[:size], 'theme' => theme, 'url' => '',
199
- 'url2x' => nil, 'width' => 0, 'height' => 0 })
205
+ 'url2x' => nil, 'width' => 0, 'height' => 0,
206
+ 'bytes' => nil, 'bytes2x' => nil, 'thumb' => nil })
200
207
  if shot[:x2]
201
- v['url2x'] ||= item['url']
208
+ if v['url2x'].nil?
209
+ v['url2x'] = item['url']
210
+ v['bytes2x'] = item['bytes']&.to_i
211
+ end
202
212
  elsif v['url'] == ''
203
213
  w = shot[:css_width]
204
214
  scaled = !w.nil? && width > w
205
215
  v['url'] = item['url']
206
216
  v['width'] = scaled ? w : width
207
217
  v['height'] = scaled ? (2 * height * w + width) / (2 * width) : height
218
+ v['bytes'] = item['bytes']&.to_i
219
+ if (t = item['thumb'])
220
+ v['thumb'] = { 'url' => t['url'], 'width' => t['width'].to_i, 'height' => t['height'].to_i,
221
+ 'bytes' => t['bytes']&.to_i }
222
+ end
208
223
  end
209
224
  end
210
225
  end
@@ -247,6 +262,10 @@ module VisualCI
247
262
  unless positive_integral?(item['width']) && positive_integral?(item['height'])
248
263
  raise Error, "#{where} (#{item['name']}): its width and height aren't whole numbers from 1 to #{MAX_SIZE}"
249
264
  end
265
+ raise Error, "#{where} (#{item['name']}): its bytes isn't a whole number" unless item['bytes'].nil? || bytes?(item['bytes'])
266
+ unless item['thumb'].nil? || thumb?(item['thumb'])
267
+ raise Error, "#{where} (#{item['name']}): its thumb needs an http(s) url, a width and a height"
268
+ end
250
269
  end
251
270
  end
252
271
  end
@@ -261,6 +280,36 @@ module VisualCI
261
280
  integral?(value) && value.positive? && value <= MAX_SIZE
262
281
  end
263
282
 
283
+ # A size in bytes: a whole number from 0 to 10^12.
284
+ def bytes?(value)
285
+ integral?(value) && value >= 0 && value <= MAX_CAP
286
+ end
287
+
288
+ def thumb?(value)
289
+ value.is_a?(Hash) && value['url'].is_a?(String) && ABSOLUTE.match?(value['url']) &&
290
+ positive_integral?(value['width']) && positive_integral?(value['height']) &&
291
+ (value['bytes'].nil? || bytes?(value['bytes']))
292
+ end
293
+
294
+ # preload: true, false, or {fold:, count:, bytes:, parallel:} (string or symbol keys), each nil or a whole number.
295
+ def preload?(value)
296
+ return true if boolean?(value)
297
+
298
+ value.is_a?(Hash) && value.all? do |k, x|
299
+ k = k.to_s
300
+ PRELOAD_DEFAULTS.key?(k) &&
301
+ (x.nil? || (integral?(x) && x >= (k == 'parallel' ? 1 : 0) && x <= (k == 'parallel' ? 8 : MAX_CAP)))
302
+ end
303
+ end
304
+
305
+ # nil: preloading with the defaults. "off": none. Else every cap, filled in from the defaults.
306
+ def preload_model(value)
307
+ return nil if value.nil? || value == true
308
+ return 'off' if value == false
309
+
310
+ PRELOAD_DEFAULTS.to_h { |k, d| [k, fetch_key(value, k)&.to_i || d] }
311
+ end
312
+
264
313
  def boolean?(value)
265
314
  [true, false].include?(value)
266
315
  end
@@ -317,7 +366,7 @@ module VisualCI
317
366
  end
318
367
 
319
368
  private_class_method :build_groups, :get, :pair, :common_folder, :rank, :check_manifest, :integral?,
320
- :positive_integral?, :boolean?, :opt, :strings, :texts, :fetch_key, :see_all?, :safe_href?
369
+ :positive_integral?, :bytes?, :thumb?, :preload?, :preload_model, :boolean?, :opt, :strings, :texts, :fetch_key, :see_all?, :safe_href?
321
370
  end
322
371
  end
323
372
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visualci-gallery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DocSpring