module PictureTag module Instructions # Handles the specific tag image set to construct. class Preset attr_reader :name def initialize(name) @name = name @content = build_preset end def [](key) @content[key] end def formats @content['formats'] end def fallback_format @content['fallback_format'] end def fallback_width @content['fallback_width'] end # Allows a per-preset hard override of the global nomarkdown setting and # JPT's feeble attempts at auto-detection. def nomarkdown? if @content['nomarkdown'].nil? PictureTag.config.nomarkdown? else @content['nomarkdown'] end end # Image widths to generate for a given media query. def widths(media = nil) setting_lookup('widths', 'media', media) end # Image quality setting. Surprisingly complicated; can depend on both format and width. def quality(format = nil, width = nil) setting = setting_lookup('quality', 'format', format) return setting unless setting.is_a? Hash parse_quality_hash(setting, width) end def parse_quality_hash(points, width) # The points can be given in any order. low, high = *points.keys.map(&:to_i).sort case width when 0..low then points[low] when low..high then Utils.interpolate(points.keys, points.values, width) when high..999_999 then points[high] end end # Gravity setting (for imagemagick cropping) def gravity(media = nil) setting_lookup('gravity', 'media', media) end # Crop value def crop(media = nil) setting_lookup('crop', 'media', media) end private # Return arbitrary setting values, taking their defaults into account. # Ex: quality can be set for all image formats, or individually per # format. Per-format settings should override the general setting. def setting_lookup(setting, prefix, lookup) media_values = @content[prefix + '_' + setting] || {} media_values.default = @content[setting] media_values[lookup] end def build_preset # The _data/picture.yml file is optional. picture_data_file = grab_data_file default_preset.merge picture_data_file end def default_preset YAML.safe_load File.read( File.join(ROOT_PATH, 'jekyll_picture_tag/defaults/presets.yml') ) end def grab_data_file search_data('presets') || search_data('markup_presets') || no_preset end def search_data(key) PictureTag.site .data .dig('picture', key, @name) end def no_preset unless @name == 'default' Utils.warning( <<~HEREDOC Preset "#{@name}" not found in {PictureTag.config['data_dir']}/picture.yml under markup_presets key. Using default values." HEREDOC ) end {} end end end end