-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
10e6d03
commit cdda328
Showing
4 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters