html2rss 0.24.0 → 0.26.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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +76 -2
  3. data/html2rss.gemspec +4 -1
  4. data/lib/html2rss/auto_source/cleanup.rb +62 -36
  5. data/lib/html2rss/auto_source/scraper/html.rb +68 -145
  6. data/lib/html2rss/auto_source/scraper/json_state/article_normalizer.rb +116 -0
  7. data/lib/html2rss/auto_source/scraper/json_state/candidate_detector.rb +63 -0
  8. data/lib/html2rss/auto_source/scraper/json_state/document_scanner.rb +179 -0
  9. data/lib/html2rss/auto_source/scraper/json_state/value_finder.rb +55 -0
  10. data/lib/html2rss/auto_source/scraper/json_state.rb +31 -401
  11. data/lib/html2rss/auto_source/scraper/schema/item_list.rb +2 -14
  12. data/lib/html2rss/auto_source/scraper/semantic_html/entry_deduplicator.rb +35 -30
  13. data/lib/html2rss/auto_source/scraper/semantic_html.rb +40 -120
  14. data/lib/html2rss/auto_source/scraper/sitemap/parser.rb +161 -0
  15. data/lib/html2rss/auto_source/scraper/sitemap.rb +10 -7
  16. data/lib/html2rss/auto_source/scraper/wordpress_api.rb +6 -2
  17. data/lib/html2rss/auto_source/scraper/xhr_articles.rb +69 -0
  18. data/lib/html2rss/auto_source/scraper.rb +109 -53
  19. data/lib/html2rss/auto_source/segment.rb +29 -0
  20. data/lib/html2rss/auto_source/segmenter/cluster.rb +163 -0
  21. data/lib/html2rss/auto_source/segmenter/list.rb +103 -0
  22. data/lib/html2rss/auto_source/segmenter/primary_link.rb +80 -0
  23. data/lib/html2rss/auto_source/segmenter/semantic.rb +104 -0
  24. data/lib/html2rss/auto_source/segmenter.rb +95 -0
  25. data/lib/html2rss/auto_source.rb +79 -21
  26. data/lib/html2rss/capture.rb +315 -0
  27. data/lib/html2rss/cli.rb +72 -18
  28. data/lib/html2rss/config/auto_source_contract.rb +5 -1
  29. data/lib/html2rss/config/request_controls.rb +33 -0
  30. data/lib/html2rss/config/schema.rb +67 -26
  31. data/lib/html2rss/config/selectors_validator.rb +60 -21
  32. data/lib/html2rss/config/validator.rb +33 -35
  33. data/lib/html2rss/config.rb +6 -2
  34. data/lib/html2rss/error.rb +15 -3
  35. data/lib/html2rss/feed_builder/rss.rb +17 -5
  36. data/lib/html2rss/feed_pipeline/auto_fallback.rb +16 -6
  37. data/lib/html2rss/feed_pipeline/runtime_policy.rb +3 -43
  38. data/lib/html2rss/feed_pipeline.rb +11 -0
  39. data/lib/html2rss/feed_result.rb +1 -1
  40. data/lib/html2rss/hash_util.rb +17 -0
  41. data/lib/html2rss/html/article_extractor/category_extractor.rb +10 -36
  42. data/lib/html2rss/html/article_extractor/date_extractor.rb +2 -7
  43. data/lib/html2rss/html/article_extractor/enclosure_extractor.rb +5 -50
  44. data/lib/html2rss/html/article_extractor/image_extractor.rb +5 -16
  45. data/lib/html2rss/html/article_rules/category.rb +55 -0
  46. data/lib/html2rss/html/article_rules/date.rb +25 -0
  47. data/lib/html2rss/html/article_rules/enclosure.rb +72 -0
  48. data/lib/html2rss/html/article_rules/image.rb +109 -0
  49. data/lib/html2rss/html/article_rules.rb +10 -0
  50. data/lib/html2rss/html/rendering/audio_renderer.rb +2 -12
  51. data/lib/html2rss/html/rendering/escaped_attributes.rb +27 -0
  52. data/lib/html2rss/html/rendering/image_renderer.rb +2 -12
  53. data/lib/html2rss/html/rendering/pdf_renderer.rb +2 -8
  54. data/lib/html2rss/html/rendering/video_renderer.rb +2 -12
  55. data/lib/html2rss/html/sst_article_extractor.rb +308 -0
  56. data/lib/html2rss/link_destination/destination_facts.rb +40 -0
  57. data/lib/html2rss/link_destination/noise_policy.rb +68 -0
  58. data/lib/html2rss/link_destination/path_classifier.rb +208 -0
  59. data/lib/html2rss/link_destination/text_classifier.rb +64 -0
  60. data/lib/html2rss/link_destination.rb +8 -0
  61. data/lib/html2rss/mcp/server.rb +577 -0
  62. data/lib/html2rss/mcp.rb +21 -0
  63. data/lib/html2rss/request_service/blocked_surface.rb +24 -1
  64. data/lib/html2rss/request_service/botasaurus_contract.rb +89 -9
  65. data/lib/html2rss/request_service/botasaurus_strategy.rb +4 -2
  66. data/lib/html2rss/request_service/budget.rb +7 -35
  67. data/lib/html2rss/request_service/context.rb +0 -6
  68. data/lib/html2rss/request_service/faraday_strategy.rb +87 -3
  69. data/lib/html2rss/request_service/network_guard.rb +5 -3
  70. data/lib/html2rss/request_service/policy.rb +1 -1
  71. data/lib/html2rss/request_service/response.rb +15 -1
  72. data/lib/html2rss/request_service/strategy.rb +1 -2
  73. data/lib/html2rss/request_service.rb +4 -9
  74. data/lib/html2rss/scoring/cluster_scorer.rb +87 -0
  75. data/lib/html2rss/scoring/container_assessor.rb +77 -0
  76. data/lib/html2rss/scoring/engine.rb +145 -0
  77. data/lib/html2rss/scoring/link_resolver.rb +84 -0
  78. data/lib/html2rss/scoring/observation.rb +53 -0
  79. data/lib/html2rss/scoring/ranked_segment.rb +33 -0
  80. data/lib/html2rss/scoring/score.rb +11 -0
  81. data/lib/html2rss/scoring.rb +8 -0
  82. data/lib/html2rss/selectors/extractors/attribute.rb +15 -0
  83. data/lib/html2rss/selectors/extractors/href.rb +12 -0
  84. data/lib/html2rss/selectors/extractors/html.rb +12 -0
  85. data/lib/html2rss/selectors/extractors/static.rb +14 -0
  86. data/lib/html2rss/selectors/extractors/text.rb +11 -0
  87. data/lib/html2rss/selectors/post_processors/gsub.rb +18 -0
  88. data/lib/html2rss/selectors/post_processors/html_to_markdown.rb +11 -0
  89. data/lib/html2rss/selectors/post_processors/markdown_to_html.rb +12 -0
  90. data/lib/html2rss/selectors/post_processors/parse_time.rb +11 -0
  91. data/lib/html2rss/selectors/post_processors/parse_uri.rb +11 -0
  92. data/lib/html2rss/selectors/post_processors/sanitize_html.rb +23 -1
  93. data/lib/html2rss/selectors/post_processors/substring.rb +21 -0
  94. data/lib/html2rss/selectors/post_processors/template.rb +20 -0
  95. data/lib/html2rss/selectors/schema_doc.rb +99 -0
  96. data/lib/html2rss/selectors.rb +0 -20
  97. data/lib/html2rss/sst/attrs.rb +91 -0
  98. data/lib/html2rss/sst/document.rb +23 -0
  99. data/lib/html2rss/sst/index.rb +112 -0
  100. data/lib/html2rss/sst/node.rb +147 -0
  101. data/lib/html2rss/sst/normalizer.rb +171 -0
  102. data/lib/html2rss/sst/tags.rb +26 -0
  103. data/lib/html2rss/sst/text.rb +81 -0
  104. data/lib/html2rss/sst.rb +8 -0
  105. data/lib/html2rss/url.rb +11 -7
  106. data/lib/html2rss/version.rb +1 -1
  107. data/lib/html2rss.rb +57 -26
  108. data/schema/html2rss-config.schema.json +428 -85
  109. metadata +93 -27
  110. data/lib/html2rss/auto_source/discovery/dom_clustering/group_scorer.rb +0 -80
  111. data/lib/html2rss/auto_source/discovery/dom_clustering/overlap_resolver.rb +0 -86
  112. data/lib/html2rss/auto_source/discovery/dom_clustering.rb +0 -119
  113. data/lib/html2rss/auto_source/discovery/list_candidates.rb +0 -94
  114. data/lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb +0 -219
  115. data/lib/html2rss/auto_source/discovery/semantic_containers.rb +0 -71
  116. data/lib/html2rss/auto_source/discovery/sitemap.rb +0 -159
  117. data/lib/html2rss/auto_source/discovery.rb +0 -14
  118. data/lib/html2rss/auto_source/link_heuristics/anchor_signals.rb +0 -28
  119. data/lib/html2rss/auto_source/link_heuristics/container_assessor.rb +0 -106
  120. data/lib/html2rss/auto_source/link_heuristics/container_signals.rb +0 -80
  121. data/lib/html2rss/auto_source/link_heuristics/destination_facts.rb +0 -42
  122. data/lib/html2rss/auto_source/link_heuristics/href_extractor.rb +0 -38
  123. data/lib/html2rss/auto_source/link_heuristics/path_classifier.rb +0 -221
  124. data/lib/html2rss/auto_source/link_heuristics/text_classifier.rb +0 -66
  125. data/lib/html2rss/auto_source/link_heuristics.rb +0 -139
  126. data/lib/html2rss/request_service/browserless_strategy.rb +0 -132
  127. data/lib/html2rss/request_service/puppet_commander/navigation_guards.rb +0 -148
  128. data/lib/html2rss/request_service/puppet_commander/preload_runner.rb +0 -86
  129. data/lib/html2rss/request_service/puppet_commander.rb +0 -95
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+
5
+ module Html2rss
6
+ module SST
7
+ ##
8
+ # Sole Nokogiri consumer on the heuristic auto-source path. Builds an
9
+ # immutable SST::Document with parent/depth/chrome indices.
10
+ class Normalizer # rubocop:disable Metrics/ClassLength
11
+ # Hard ceiling for SST node allocations; beyond this we degrade.
12
+ MAX_NODES = 5_000
13
+
14
+ # Tags stripped entirely from the SST.
15
+ STRIPPED_TAGS = Set['script', 'style', 'noscript', 'iframe', 'svg', 'template'].freeze
16
+
17
+ # Kept when MAX_NODES forces semantic-tag-only degrade.
18
+ SEMANTIC_DEGRADE_TAGS = Set['html', 'body', 'article', 'section', 'li', 'tr', 'div', 'a',
19
+ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'time', 'img', 'p',
20
+ 'ul', 'ol', 'main'].freeze
21
+
22
+ # Attribute names preserved in Attrs#raw for extraction.
23
+ RAW_ATTR_KEEP = /
24
+ \A(
25
+ data- |
26
+ aria- |
27
+ class |
28
+ id |
29
+ href |
30
+ src |
31
+ srcset |
32
+ style |
33
+ datetime |
34
+ itemprop |
35
+ type |
36
+ category |
37
+ categories |
38
+ tag |
39
+ tags |
40
+ topic |
41
+ topics |
42
+ section |
43
+ sections |
44
+ label |
45
+ labels |
46
+ theme |
47
+ themes |
48
+ subject |
49
+ subjects
50
+ )
51
+ /xi
52
+
53
+ class << self
54
+ ##
55
+ # @param input [String, Nokogiri::HTML::Document, Nokogiri::XML::Node]
56
+ # HTML string or already-parsed node (String is parsed once here)
57
+ # @return [Document]
58
+ # @raise [ArgumentError] when input is nil or unsupported
59
+ def call(input)
60
+ raise ArgumentError, 'input is required' if input.nil?
61
+
62
+ new(coerce(input)).call
63
+ end
64
+
65
+ private
66
+
67
+ # @param input [String, Nokogiri::XML::Node]
68
+ # @return [Nokogiri::XML::Node]
69
+ def coerce(input)
70
+ case input
71
+ when String then Nokogiri::HTML(input)
72
+ when Nokogiri::XML::Node then input
73
+ else
74
+ raise ArgumentError, "expected String or Nokogiri::XML::Node, got #{input.class}"
75
+ end
76
+ end
77
+ end
78
+
79
+ # @param parsed_body [Nokogiri::HTML::Document, Nokogiri::XML::Node]
80
+ def initialize(parsed_body)
81
+ @parsed_body = parsed_body
82
+ @node_count = 0
83
+ @degraded = false
84
+ @parents = {}.compare_by_identity
85
+ @depths = {}.compare_by_identity
86
+ @ignored_chrome = {}.compare_by_identity
87
+ end
88
+
89
+ ##
90
+ # @return [Document]
91
+ def call
92
+ root_nk = @parsed_body.respond_to?(:root) ? (@parsed_body.at_css('html') || @parsed_body.root) : @parsed_body
93
+ root = normalize_element(root_nk, parent: nil, depth: 0, path: '', chrome: false)
94
+ raise ArgumentError, 'SST Normalizer produced an empty tree' unless root
95
+
96
+ index = Index.new(root:, parents: @parents, depths: @depths, ignored_chrome: @ignored_chrome)
97
+ Document.build(root:, index:, degraded: @degraded, node_count: @node_count)
98
+ end
99
+
100
+ private
101
+
102
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
103
+ def normalize_element(nk_node, parent:, depth:, path:, chrome:)
104
+ return unless nk_node.respond_to?(:name)
105
+ return if nk_node.text? || nk_node.comment? || nk_node.cdata?
106
+
107
+ tag = nk_node.name.to_s.downcase
108
+ return if STRIPPED_TAGS.include?(tag)
109
+ return if @degraded && !SEMANTIC_DEGRADE_TAGS.include?(tag)
110
+
111
+ if @node_count >= MAX_NODES && !@degraded
112
+ @degraded = true
113
+ Html2rss::Log.warn("sst.normalizer MAX_NODES=#{MAX_NODES} breached; degrading to semantic-tag-only")
114
+ end
115
+
116
+ return if @degraded && !SEMANTIC_DEGRADE_TAGS.include?(tag)
117
+
118
+ @node_count += 1
119
+ tag_path = path.empty? ? "/#{tag}" : "#{path}/#{tag}"
120
+ attrs = extract_attrs(nk_node)
121
+ own_text = direct_text(nk_node)
122
+ chrome_here = chrome || Tags::IGNORED_CONTAINER_NAMES.include?(tag.to_sym)
123
+
124
+ children = nk_node.children.filter_map do |child|
125
+ normalize_element(child, parent: :pending, depth: depth + 1, path: tag_path, chrome: chrome_here)
126
+ end.freeze
127
+
128
+ node = Node.build(name: tag, attrs:, own_text:, children:, tag_path:)
129
+ @parents[node] = parent == :pending ? nil : parent
130
+ @depths[node] = depth
131
+ @ignored_chrome[node] = chrome_here
132
+
133
+ # Fix parent pointers now that node exists.
134
+ children.each { |child| @parents[child] = node }
135
+
136
+ node
137
+ end
138
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
139
+
140
+ def extract_attrs(nk_node) # rubocop:disable Metrics/MethodLength
141
+ Attrs.build(
142
+ href: nk_node['href'],
143
+ src: nk_node['src'],
144
+ id: nk_node['id'],
145
+ class_names: nk_node['class'].to_s.split(/\s+/).reject(&:empty?),
146
+ datetime: nk_node['datetime'],
147
+ itemprop: nk_node['itemprop'],
148
+ style: nk_node['style'],
149
+ srcset: nk_node['srcset'],
150
+ type: nk_node['type'],
151
+ raw: raw_attrs(nk_node)
152
+ )
153
+ end
154
+
155
+ def raw_attrs(nk_node)
156
+ typed = %w[href src id class datetime itemprop style srcset type].to_set
157
+ nk_node.attribute_nodes.each_with_object({}) do |attr, raw|
158
+ name = attr.name.to_s
159
+ next if typed.include?(name)
160
+ next unless name.match?(RAW_ATTR_KEEP)
161
+
162
+ raw[name] = attr.value.to_s
163
+ end
164
+ end
165
+
166
+ def direct_text(nk_node)
167
+ nk_node.children.select(&:text?).map(&:text).join
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Html2rss
4
+ module SST
5
+ ##
6
+ # Shared tag-name and href constants for SST nodes.
7
+ module Tags
8
+ # Heading element names.
9
+ HEADING_NAMES = %i[h1 h2 h3 h4 h5 h6].to_set.freeze
10
+ # Block-level names that contribute structural newlines in text.
11
+ BLOCK_NAMES = %i[p div li ul ol h1 h2 h3 h4 h5 h6 tr br].to_set.freeze
12
+ # Names skipped when collecting visible text.
13
+ INVISIBLE_NAMES = %i[svg script noscript style template].to_set.freeze
14
+ # Landmark names treated as utility chrome.
15
+ UTILITY_LANDMARK_NAMES = %i[nav aside footer menu].to_set.freeze
16
+ # Container names ignored as article roots.
17
+ IGNORED_CONTAINER_NAMES = %i[nav footer header svg script style].to_set.freeze
18
+ # Names excluded from class/structure clustering.
19
+ CLUSTER_EXCLUDED_NAMES = %i[html body nav footer header svg script style].to_set.freeze
20
+ # Href prefixes that never count as content destinations.
21
+ SKIP_HREF_PREFIXES = ['#', 'javascript:', 'mailto:', 'tel:', 'file://', 'sms:', 'data:'].freeze
22
+ # Layout-ish names used when resolving nested wrapper groups.
23
+ LAYOUT_NAMES = %i[div section article li ul ol].to_set.freeze
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Html2rss
4
+ module SST
5
+ ##
6
+ # Visible-text extraction for SST nodes (port of Navigator::TextExtractor).
7
+ module Text
8
+ module_function
9
+
10
+ ##
11
+ # @param node [Node]
12
+ # @param separator [String]
13
+ # @param exclude [Array<Node>, nil]
14
+ # @return [String, nil]
15
+ def extract(node, separator: ' ', exclude: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
16
+ raise ArgumentError, 'node is required' unless node
17
+
18
+ excluded = exclude&.each_with_object({}.compare_by_identity) { |n, h| h[n] = true }
19
+ if node.children.empty?
20
+ text = node.own_text.to_s.gsub(/\s+/, ' ').strip
21
+ return text.empty? ? nil : text
22
+ end
23
+
24
+ parts = collect_parts(node, separator, excluded)
25
+ own = node.own_text.to_s.gsub(/\s+/, ' ').strip
26
+ parts = [own, *parts] unless own.empty?
27
+ return if parts.empty?
28
+
29
+ parts.join.squeeze(' ').strip
30
+ end
31
+
32
+ def collect_parts(node, separator, excluded)
33
+ last_block = false
34
+ node.children.each_with_object([]) do |child, parts|
35
+ next if excluded&.[](child)
36
+ next unless visible_child?(child)
37
+
38
+ text, block = child_text_and_block(child, separator, excluded)
39
+ next if text.empty?
40
+
41
+ append_separator!(parts, separator, block, last_block)
42
+ parts << text
43
+ last_block = block
44
+ end
45
+ end
46
+ module_function :collect_parts
47
+ private_class_method :collect_parts
48
+
49
+ def child_text_and_block(child, separator, excluded)
50
+ text = if child.children.empty?
51
+ child.own_text.gsub(/\s+/, ' ').strip
52
+ else
53
+ extract(child, separator:, exclude: excluded&.keys).to_s.strip
54
+ end
55
+ text = "- #{text}" if child.name == :li && !text.empty?
56
+ [text, Tags::BLOCK_NAMES.include?(child.name)]
57
+ end
58
+ module_function :child_text_and_block
59
+ private_class_method :child_text_and_block
60
+
61
+ def append_separator!(parts, separator, is_block, last_was_block)
62
+ return if parts.empty?
63
+
64
+ parts << if is_block || last_was_block
65
+ (separator == ' ' ? "\n" : separator)
66
+ else
67
+ ' '
68
+ end
69
+ end
70
+ module_function :append_separator!
71
+ private_class_method :append_separator!
72
+
73
+ def visible_child?(node)
74
+ !Tags::INVISIBLE_NAMES.include?(node.name) &&
75
+ !(node.name == :a && node.attrs.href&.start_with?('#'))
76
+ end
77
+ module_function :visible_child?
78
+ private_class_method :visible_child?
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Html2rss
4
+ ##
5
+ # Simplified Semantic Tree namespace for the heuristic auto-source pipeline.
6
+ module SST
7
+ end
8
+ end
data/lib/html2rss/url.rb CHANGED
@@ -21,6 +21,7 @@ module Html2rss
21
21
  # url = Url.from_relative('/foo-bar/baz.txt', 'https://example.com')
22
22
  # url.titleized # => "Foo Bar Baz"
23
23
  # url.channel_titleized # => "example.com: Foo Bar Baz"
24
+ # rubocop:disable Metrics/ClassLength -- value object owns resolve/sanitize/titleize/identity
24
25
  class Url
25
26
  include Comparable
26
27
 
@@ -189,6 +190,14 @@ module Html2rss
189
190
  self.class.from_absolute(uri.normalize.to_s)
190
191
  end
191
192
 
193
+ ##
194
+ # Returns a copy of the URL with the fragment removed (dedup / self-link identity).
195
+ #
196
+ # @return [Url] this URL when already fragment-free; otherwise a new URL
197
+ def without_fragment
198
+ @uri.fragment ? self.class.from_absolute(@uri.omit(:fragment).normalize.to_s) : self
199
+ end
200
+
192
201
  ##
193
202
  # Returns a copy of the URL with the provided query values.
194
203
  #
@@ -259,13 +268,7 @@ module Html2rss
259
268
  # @param other [Object] the other object to compare with
260
269
  # @return [Boolean] true if the URLs are equal
261
270
  def ==(other) = other.is_a?(Url) && to_s == other.to_s
262
-
263
- ##
264
- # Supports hash-based comparisons by ensuring equality semantics match `hash`.
265
- #
266
- # @param other [Object] the other object to compare with
267
- # @return [Boolean] true if the URLs are considered equal
268
- def eql?(other) = other.is_a?(Url) && to_s == other.to_s
271
+ alias eql? ==
269
272
 
270
273
  ##
271
274
  # Returns the hash code for this URL.
@@ -279,4 +282,5 @@ module Html2rss
279
282
  # @return [String] the debug representation
280
283
  def inspect = "#<#{self.class}:#{object_id} @uri=#{@uri.inspect}>"
281
284
  end
285
+ # rubocop:enable Metrics/ClassLength
282
286
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Html2rss
4
4
  # Current application version.
5
- VERSION = '0.24.0'
5
+ VERSION = '0.26.0'
6
6
  public_constant :VERSION
7
7
  end
data/lib/html2rss.rb CHANGED
@@ -3,7 +3,11 @@
3
3
  require 'zeitwerk'
4
4
 
5
5
  loader = Zeitwerk::Loader.for_gem
6
- loader.inflector.inflect('cli' => 'CLI')
6
+ loader.inflector.inflect(
7
+ 'cli' => 'CLI',
8
+ 'sst' => 'SST',
9
+ 'mcp' => 'MCP'
10
+ )
7
11
  loader.setup
8
12
 
9
13
  require 'logger'
@@ -67,7 +71,10 @@ module Html2rss
67
71
  # rubocop:disable Metrics/ParameterLists
68
72
 
69
73
  ##
70
- # Scrapes the provided URL and returns an RSS object.
74
+ # Scrapes the provided URL without hand-written selectors and returns an RSS object.
75
+ #
76
+ # Builds an auto_source config, then FeedPipeline runs structured scrapers and
77
+ # (when needed) the SST heuristic path — see docs/auto_source.md.
71
78
  #
72
79
  # @param url [String] source page URL
73
80
  # @param strategy [Symbol] request strategy to use
@@ -75,18 +82,23 @@ module Html2rss
75
82
  # @param max_redirects [Integer, nil] optional redirect limit override
76
83
  # @param max_requests [Integer] optional request budget override (default: 4 for sitemap sub-fetches)
77
84
  # @param local_file_path [String, nil] optional local HTML file path
85
+ # @param limit [Integer, nil] max articles to keep (default: {AutoSource::DEFAULT_LIMIT})
78
86
  # @return [RSS::Rss] generated RSS feed
79
87
  def self.auto_source(url,
80
88
  strategy: :auto,
81
89
  items_selector: nil,
82
90
  max_redirects: nil,
83
91
  max_requests: 4,
84
- local_file_path: nil)
85
- feed(build_auto_source_config(url:, strategy:, items_selector:, max_redirects:, max_requests:, local_file_path:))
92
+ local_file_path: nil,
93
+ limit: nil)
94
+ feed(build_auto_source_config(url:, strategy:, items_selector:, max_redirects:, max_requests:,
95
+ local_file_path:, limit:))
86
96
  end
87
97
 
88
98
  ##
89
- # Scrapes the provided URL and returns a JSONFeed 1.1 hash.
99
+ # Scrapes the provided URL without hand-written selectors and returns a JSONFeed 1.1 hash.
100
+ #
101
+ # Same auto_source pipeline as {.auto_source}; see docs/auto_source.md.
90
102
  #
91
103
  # @param url [String] source page URL
92
104
  # @param strategy [Symbol] request strategy to use
@@ -94,15 +106,47 @@ module Html2rss
94
106
  # @param max_redirects [Integer, nil] optional redirect limit override
95
107
  # @param max_requests [Integer] optional request budget override (default: 4 for sitemap sub-fetches)
96
108
  # @param local_file_path [String, nil] optional local HTML file path
109
+ # @param limit [Integer, nil] max articles to keep (default: {AutoSource::DEFAULT_LIMIT})
97
110
  # @return [Hash] JSONFeed-compliant hash
98
111
  def self.auto_json_feed(url,
99
112
  strategy: :auto,
100
113
  items_selector: nil,
101
114
  max_redirects: nil,
102
115
  max_requests: 4,
103
- local_file_path: nil)
116
+ local_file_path: nil,
117
+ limit: nil)
104
118
  json_feed(build_auto_source_config(url:, strategy:, items_selector:, max_redirects:, max_requests:,
105
- local_file_path:))
119
+ local_file_path:, limit:))
120
+ end
121
+
122
+ ##
123
+ # Analyzes a URL and produces a reusable YAML-ready feed config hash.
124
+ #
125
+ # Uses auto-source discovery to extract articles, then derives CSS selectors
126
+ # from the structural analysis.
127
+ #
128
+ # @param url [String] source page URL
129
+ # @param strategy [Symbol] request strategy (+:auto+, +:faraday+, +:botasaurus+)
130
+ # @param items_selector [String, nil] optional CSS selector hint for items
131
+ # @param max_redirects [Integer, nil] optional redirect limit override
132
+ # @param max_requests [Integer, nil] optional request budget override
133
+ # @param limit [Integer, nil] max articles to keep
134
+ # @param local_file_path [String, nil] optional local HTML file path
135
+ # @return [Hash] feed config hash with +:channel+ and +:selectors+
136
+ def self.capture(url,
137
+ strategy: :auto,
138
+ items_selector: nil,
139
+ max_redirects: nil,
140
+ max_requests: nil,
141
+ limit: nil,
142
+ local_file_path: nil)
143
+ Capture.build(url,
144
+ strategy:,
145
+ items_selector:,
146
+ max_redirects:,
147
+ max_requests:,
148
+ limit:,
149
+ local_file_path:).config
106
150
  end
107
151
 
108
152
  # rubocop:enable Metrics/ParameterLists
@@ -154,11 +198,14 @@ module Html2rss
154
198
  class << self
155
199
  private
156
200
 
157
- def build_auto_source_config(url:, strategy:, items_selector:, max_redirects:, max_requests:, local_file_path: nil) # rubocop:disable Metrics/ParameterLists
201
+ # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
202
+ def build_auto_source_config(url:, strategy:, items_selector:, max_redirects:, max_requests:,
203
+ local_file_path: nil, limit: nil)
158
204
  config = Config.auto_source_config(
159
205
  url:,
160
206
  items_selector:,
161
- request_controls: shortcut_request_controls(strategy:, max_redirects:, max_requests:)
207
+ request_controls: Config::RequestControls.from_shortcut(strategy:, max_redirects:, max_requests:),
208
+ limit:
162
209
  )
163
210
  if local_file_path
164
211
  config[:request] ||= {}
@@ -166,23 +213,7 @@ module Html2rss
166
213
  end
167
214
  config
168
215
  end
169
-
170
- def shortcut_request_controls(strategy:, max_redirects:, max_requests:)
171
- Config::RequestControls.new(
172
- strategy:,
173
- max_redirects:,
174
- max_requests:,
175
- explicit_keys: explicit_request_control_keys(strategy:, max_redirects:, max_requests:)
176
- )
177
- end
178
-
179
- def explicit_request_control_keys(strategy:, max_redirects:, max_requests:)
180
- keys = []
181
- keys << :strategy unless strategy.nil? || strategy == Config.default_strategy_name
182
- keys << :max_redirects unless max_redirects.nil?
183
- keys << :max_requests unless max_requests.nil?
184
- keys
185
- end
216
+ # rubocop:enable Metrics/ParameterLists, Metrics/MethodLength
186
217
  end
187
218
 
188
219
  logger.level = defaults.log_level if logger.respond_to?(:level=)