Skip to content

Commit

Permalink
Add srcset parsing
Browse files Browse the repository at this point in the history
This commit adds a new ImageElementParser class that encompasses
existing custom `img[alt]` handling with the addition of `srcset`
parsing based on the rules I proposed in
microformats/microformats2-parsing#7.

See my comment here:

microformats/microformats2-parsing#7 (comment)
  • Loading branch information
jgarber623 committed Sep 23, 2022
1 parent 10e6d03 commit cdda328
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
63 changes: 63 additions & 0 deletions lib/micro_micro/parsers/image_element_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

module MicroMicro
module Parsers
class ImageElementParser
# @return [String]
attr_reader :value

# @param node [Nokogiri::XML::Element]
# @param value [String]
def initialize(node, value)
@node = node
@value = value
end

# @return [String, nil]
def alt
@alt ||= node['alt']&.strip
end

# @return [Boolean]
def alt?
!alt.nil?
end

# @return [Hash{Symbol => String}, nil]
def srcset
@srcset ||= parsed_srcset if node['srcset']
end

# @return [Boolean]
def srcset?
srcset.present?
end

# @return [Hash{Symbol => String, Hash{Symbol => String}}]
def to_h
hash = { value: value }

hash[:srcset] = srcset if srcset?
hash[:alt] = alt if alt?

hash
end

private

# @return [Nokogiri::XML::Element]
attr_reader :node

# @return [Hash{Symbol => String}]
def parsed_srcset
node['srcset']
.split(',')
.to_h do |candidate|
candidate.strip.match(/^(.+?)(\s+.+)?$/) do
[(Regexp.last_match(2) || '1x').strip.to_sym, Regexp.last_match(1)]
end
end
end
end
end
end
11 changes: 5 additions & 6 deletions lib/micro_micro/parsers/implied_photo_property_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ class ImpliedPhotoPropertyParser < BaseImpliedPropertyParser
def value
@value ||=
if attribute_value
return attribute_value unless candidate_node.matches?('img[alt]')

{
value: attribute_value,
alt: candidate_node['alt'].strip
}
if candidate_node.matches?('img[alt], img[srcset]')
ImageElementParser.new(candidate_node, attribute_value).to_h
else
attribute_value
end
end
end

Expand Down
7 changes: 2 additions & 5 deletions lib/micro_micro/parsers/url_property_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ class UrlPropertyParser < BasePropertyParser
# @return [String, Hash{Symbol => String}]
def value
@value ||=
if node.matches?('img[alt]')
{
value: resolved_value,
alt: node['alt'].strip
}
if node.matches?('img[alt], img[srcset]')
ImageElementParser.new(node, resolved_value).to_h
else
resolved_value
end
Expand Down
1 change: 1 addition & 0 deletions lib/micromicro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require_relative 'micro_micro/helpers'

require_relative 'micro_micro/parsers/date_time_parser'
require_relative 'micro_micro/parsers/image_element_parser'
require_relative 'micro_micro/parsers/value_class_pattern_parser'

require_relative 'micro_micro/parsers/base_property_parser'
Expand Down

0 comments on commit cdda328

Please # to comment.