micromicro 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6734c4d9cbd071d3288a5f8c72de07095fc6db2915224f97684a0577362bcdb0
4
- data.tar.gz: 9225043ef91161f2a92fa338bc874771dc55d9e81ffe969fe7b005c9dbc28403
3
+ metadata.gz: 75bb9eaa673828648bc3f0a78d9822f434ae99317ae9499998f984c661c0771b
4
+ data.tar.gz: 2ab7a2896a8498507cb57f7e1c5a10c1e5dff8091328370a10e645420374ec12
5
5
  SHA512:
6
- metadata.gz: 58824c1699a2aff5e22d906cbb449f95f55a665d7374ba62e26ddfa3fd6986ae952e22965446e4bbe973d2e17a1f05cba32e74f7d30e1e327fbc262ebce7b1f1
7
- data.tar.gz: aa9db8c15e10ecc08fa65db0cf6964914681ef72b552a300c4b95899b4a837835c1c69baff9b8e48586df34eefb7bb4156e55a5c81582deda4a9948554224f48
6
+ metadata.gz: a24f38005e975861d4e5ce1c3b728ea4535d7a094cae2515a8b6e7f0e7bb7abe7558eec44651a7e833d0120d397bcb45687022c374a039f5d755d46798264e8c
7
+ data.tar.gz: 58f27f493e1255ef7b4ea1904cb643c1392f62dacea645708142f153eccfba2ed0a9ed52e33f76f4ec4eae5012b9dce159ec963c216a5506969b4a47be4e2042
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Changelog
2
2
 
3
- ## v3.0.0 / 2022-08-28
3
+ ## 3.1.0 / 2022-09-24
4
+
5
+ - **New feature:** parse `img[srcset]` (microformats/microformats2-parsing#7) (cdda328)
6
+ - Improve usage of activesupport extensions (5ed120c)
7
+
8
+ ## 3.0.0 / 2022-08-28
4
9
 
5
10
  - Improved YARD documentation
6
11
  - New `Item` instance methods (8105d6f):
data/README.md CHANGED
@@ -48,7 +48,7 @@ doc.to_h
48
48
  #=> { :items => [{ :type => ["h-card"], :properties => { :name => ["Jason Garber"] } }], :rels => {}, :"rel-urls" => {} }
49
49
  ```
50
50
 
51
- See [USAGE.md](https://github.com/jgarber623/micromicro/blob/main/USAGE.md) for detailed examples of MicroMicro's features.
51
+ See [USAGE.md](https://github.com/jgarber623/micromicro/blob/main/USAGE.md) for detailed examples of MicroMicro's features. Additional structured documentation is available on [RubyDoc.info](https://rubydoc.info/gems/micromicro).
52
52
 
53
53
  ## Contributing
54
54
 
@@ -64,7 +64,7 @@ module MicroMicro
64
64
  #
65
65
  # @return [Hash{Symbol => Array<String, Hash>}]
66
66
  def to_h
67
- group_by(&:name).symbolize_keys.deep_transform_values(&:value)
67
+ group_by(&:name).transform_keys(&:to_sym).deep_transform_values(&:value)
68
68
  end
69
69
 
70
70
  # A collection of url {MicroMicro::Property}s parsed from the node.
@@ -38,9 +38,9 @@ module MicroMicro
38
38
  #
39
39
  # @return [Hash{Symbol => Array<String>}]
40
40
  def group_by_rel
41
- each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |member, hash|
42
- member.rels.each { |rel| hash[rel] << member.href }
43
- end.symbolize_keys.transform_values(&:uniq)
41
+ each_with_object(Hash.new { |hash, key| hash[key] = Set.new }) do |member, hash|
42
+ member.rels.each { |rel| hash[rel.to_sym] << member.href }
43
+ end.transform_values(&:to_a)
44
44
  end
45
45
 
46
46
  # Return a Hash of this collection's {MicroMicro::Relationship}s grouped
@@ -51,7 +51,7 @@ module MicroMicro
51
51
  #
52
52
  # @return [Hash{Symbol => Hash{Symbol => Array, String}}]
53
53
  def group_by_url
54
- group_by(&:href).symbolize_keys.transform_values { |relationships| relationships.first.to_h.slice!(:href) }
54
+ group_by(&:href).to_h { |k, v| [k.to_sym, v.first.to_h.except(:href)] }
55
55
  end
56
56
 
57
57
  # Retrieve an Array of this collection's unique {MicroMicro::Relationship}
@@ -90,17 +90,14 @@ module MicroMicro
90
90
 
91
91
  # @return [String, nil]
92
92
  def value
93
- @value ||=
94
- if normalized_date || normalized_time || normalized_timezone
95
- "#{normalized_date} #{normalized_time}#{normalized_timezone}".strip
96
- end
93
+ @value ||= "#{normalized_date} #{normalized_time}#{normalized_timezone}".strip.presence
97
94
  end
98
95
 
99
96
  # @return [Hash{Symbol => String, nil}]
100
97
  def values
101
98
  @values ||=
102
99
  if string.match?(DATE_TIME_TIMEZONE_REGEXP)
103
- string.match(DATE_TIME_TIMEZONE_REGEXP).named_captures.symbolize_keys
100
+ string.match(DATE_TIME_TIMEZONE_REGEXP).named_captures.transform_keys(&:to_sym)
104
101
  else
105
102
  {}
106
103
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MicroMicro
4
+ module Parsers
5
+ class ImageElementParser
6
+ # @return [String]
7
+ attr_reader :value
8
+
9
+ # @param node [Nokogiri::XML::Element]
10
+ # @param value [String]
11
+ def initialize(node, value)
12
+ @node = node
13
+ @value = value
14
+ end
15
+
16
+ # @return [String, nil]
17
+ def alt
18
+ @alt ||= node['alt']&.strip
19
+ end
20
+
21
+ # @return [Boolean]
22
+ def alt?
23
+ !alt.nil?
24
+ end
25
+
26
+ # @return [Hash{Symbol => String}, nil]
27
+ def srcset
28
+ @srcset ||= image_candidates if node['srcset']
29
+ end
30
+
31
+ # @return [Boolean]
32
+ def srcset?
33
+ srcset.present?
34
+ end
35
+
36
+ # @return [Hash{Symbol => String, Hash{Symbol => String}}]
37
+ def to_h
38
+ hash = { value: value }
39
+
40
+ hash[:srcset] = srcset if srcset?
41
+ hash[:alt] = alt if alt?
42
+
43
+ hash
44
+ end
45
+
46
+ private
47
+
48
+ # @return [Nokogiri::XML::Element]
49
+ attr_reader :node
50
+
51
+ # @return [Hash{Symbol => String}]
52
+ #
53
+ # rubocop:disable Style/PerlBackrefs
54
+ def image_candidates
55
+ node['srcset']
56
+ .split(',')
57
+ .each_with_object({}) do |candidate, hash|
58
+ candidate.strip.match(/^(.+?)(\s+.+)?$/) do
59
+ key = ($2 || '1x').strip.to_sym
60
+
61
+ hash[key] = $1 unless hash[key]
62
+ end
63
+ end
64
+ end
65
+ # rubocop:enable Style/PerlBackrefs
66
+ end
67
+ end
68
+ end
@@ -19,12 +19,11 @@ module MicroMicro
19
19
  def value
20
20
  @value ||=
21
21
  if attribute_value
22
- return attribute_value unless candidate_node.matches?('img[alt]')
23
-
24
- {
25
- value: attribute_value,
26
- alt: candidate_node['alt'].strip
27
- }
22
+ if candidate_node.matches?('img[alt], img[srcset]')
23
+ ImageElementParser.new(candidate_node, attribute_value).to_h
24
+ else
25
+ attribute_value
26
+ end
28
27
  end
29
28
  end
30
29
 
@@ -23,11 +23,8 @@ module MicroMicro
23
23
  # @return [String, Hash{Symbol => String}]
24
24
  def value
25
25
  @value ||=
26
- if node.matches?('img[alt]')
27
- {
28
- value: resolved_value,
29
- alt: node['alt'].strip
30
- }
26
+ if node.matches?('img[alt], img[srcset]')
27
+ ImageElementParser.new(node, resolved_value).to_h
31
28
  else
32
29
  resolved_value
33
30
  end
@@ -57,7 +57,7 @@ module MicroMicro
57
57
  self.class
58
58
  .node_set_from(node)
59
59
  .map { |value_node| self.class.value_from(value_node) }
60
- .select(&:present?)
60
+ .compact_blank!
61
61
  end
62
62
 
63
63
  private
@@ -152,8 +152,8 @@ module MicroMicro
152
152
 
153
153
  return hash.merge(parser.value) if embedded_markup_property?
154
154
 
155
- p_property = item.properties.find { |property| property.name == 'name' } if plain_text_property?
156
- u_property = item.properties.find { |property| property.name == 'url' } if url_property?
155
+ p_property = item.properties.find_by(name: 'name') if plain_text_property?
156
+ u_property = item.properties.find_by(name: 'url') if url_property?
157
157
 
158
158
  hash.merge(value: (p_property || u_property || parser).value)
159
159
  else
@@ -65,7 +65,7 @@ module MicroMicro
65
65
  title: title,
66
66
  type: type,
67
67
  text: text
68
- }.select { |_, value| value.present? }
68
+ }.compact_blank!
69
69
  end
70
70
 
71
71
  # An Array of unique values from node's +rel+ attribute.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MicroMicro
4
- VERSION = '3.0.0'
4
+ VERSION = '3.1.0'
5
5
  end
data/lib/micromicro.rb CHANGED
@@ -3,9 +3,9 @@
3
3
  require 'forwardable'
4
4
 
5
5
  require 'active_support/core_ext/array/grouping'
6
+ require 'active_support/core_ext/enumerable'
6
7
  require 'active_support/core_ext/hash/deep_transform_values'
7
- require 'active_support/core_ext/hash/keys'
8
- require 'active_support/core_ext/hash/slice'
8
+ require 'active_support/core_ext/hash/except'
9
9
  require 'active_support/core_ext/object/blank'
10
10
  require 'nokogiri'
11
11
  require 'nokogiri/html-ext'
@@ -15,6 +15,7 @@ require_relative 'micro_micro/collectible'
15
15
  require_relative 'micro_micro/helpers'
16
16
 
17
17
  require_relative 'micro_micro/parsers/date_time_parser'
18
+ require_relative 'micro_micro/parsers/image_element_parser'
18
19
  require_relative 'micro_micro/parsers/value_class_pattern_parser'
19
20
 
20
21
  require_relative 'micro_micro/parsers/base_property_parser'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micromicro
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Garber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-29 00:00:00.000000000 Z
11
+ date: 2022-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -77,6 +77,7 @@ files:
77
77
  - lib/micro_micro/parsers/date_time_parser.rb
78
78
  - lib/micro_micro/parsers/date_time_property_parser.rb
79
79
  - lib/micro_micro/parsers/embedded_markup_property_parser.rb
80
+ - lib/micro_micro/parsers/image_element_parser.rb
80
81
  - lib/micro_micro/parsers/implied_name_property_parser.rb
81
82
  - lib/micro_micro/parsers/implied_photo_property_parser.rb
82
83
  - lib/micro_micro/parsers/implied_url_property_parser.rb
@@ -93,7 +94,7 @@ licenses:
93
94
  - MIT
94
95
  metadata:
95
96
  bug_tracker_uri: https://github.com/jgarber623/micromicro/issues
96
- changelog_uri: https://github.com/jgarber623/micromicro/blob/v3.0.0/CHANGELOG.md
97
+ changelog_uri: https://github.com/jgarber623/micromicro/blob/v3.1.0/CHANGELOG.md
97
98
  rubygems_mfa_required: 'true'
98
99
  post_install_message:
99
100
  rdoc_options: []