sawzall 0.1.0.pre2-x86_64-linux → 0.1.0.pre3-x86_64-linux

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52c393775e4ec6d53f6457b76bf926a2766649cd6c49321615b7207d056f1f2d
4
- data.tar.gz: 195152a13c2046602bbf4f5e7f4499d0fd6549e9f63060f25bb4bb712af32ece
3
+ metadata.gz: b2934b373371c232d43ca9a2e2e3c0c15109144d377220eeca69ef8b73c46069
4
+ data.tar.gz: '049251be4d658dd1294990548e3b051f1152579bf9da2a631233e57721e9586d'
5
5
  SHA512:
6
- metadata.gz: 39ae9d75e4c0aefcb976df8c04be54c6f34eb029ef12e653b354c57bf5378deca1ef41c74332f2dea31c5cb69aa8aa50246fb568984af272ca6d2c47182950d5
7
- data.tar.gz: 9bec2ea88ca835402a6b752cb77f73e59eeb3a6c2ecf825ddcf1386f11e18c808fc634a97c2404925fef74728ebc2f44986a4fcd6c529b8cfa6e038a2c98f3e9
6
+ metadata.gz: de87b92d84b087d83fda72306a6779049050960acf6409eb8d9e8fbfb926d07fc1921f745342a61b619cef6707fef7be3094972767cab7ac97cf15ce7fea154f
7
+ data.tar.gz: f36f2f0884f10fa2fbb23733117b3c6f5629bd767e29cd4dfe5024d9ef4fd75e5063d64e70b57838d4bfb4da5c6e0fa73d0dd5b80b839da62b40f63e81e24003
data/README.md CHANGED
@@ -1,38 +1,41 @@
1
- # Sawzall
1
+ # Sawzall 🪚
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Sawzall wraps the Rust scraper library (https://github.com/rust-scraper/scraper) to make it easy to parse HTML documents and query them with CSS selectors.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sawzall`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ```ruby
6
+ require "sawzall"
7
+ require "net/http"
6
8
 
7
- ## Installation
9
+ doc = Sawzall.parse_document(Net::HTTP.get("example.org", "/"))
10
+ doc.select("title").first.text #=> "Example Domain"
11
+ ```
8
12
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
13
+ > [!NOTE]
14
+ > Sawzall is a hobby project. Expect ongoing development and maintenance to be very much correlated to how much value it brings me as a learning resource and as a tool for my other projects.
15
+ >
16
+ > You are welcome to report bugs you run into or submit pull requests for changes that would make it more useful for your use-case, but please bear the above in mind.
17
+
18
+ ## Installation
10
19
 
11
20
  Install the gem and add to the application's Gemfile by executing:
12
21
 
13
22
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
23
+ bundle add sawzall
15
24
  ```
16
25
 
17
26
  If bundler is not being used to manage dependencies, install the gem by executing:
18
27
 
19
28
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
29
+ gem install sawzall
21
30
  ```
22
31
 
23
32
  ## Usage
24
33
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+ [API documentation](https://davidcornu.github.io/sawzall/)
32
35
 
33
36
  ## Contributing
34
37
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sawzall.
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/davidcornu/sawzall.
36
39
 
37
40
  ## License
38
41
 
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sawzall
4
- VERSION = "0.1.0.pre2"
4
+ VERSION = "0.1.0.pre3"
5
5
  end
data/lib/sawzall.rb CHANGED
@@ -73,112 +73,186 @@ module Sawzall
73
73
  # # @return [Sawzall::Element]
74
74
  # end
75
75
 
76
- # @!parse
77
- # class Element
78
- # # Returns the element's name in lowercase
79
- # #
80
- # # @example
81
- # # doc = Sawzall.parse_fragment("<p>Paragraph</p>")
82
- # # doc.select("p").first.name #=> "p"
83
- # #
84
- # # @!method name
85
- # # @return [String]
86
- #
87
- # # Returns the element's outer HTML
88
- # #
89
- # # @example
90
- # # doc = Sawzall.parse_fragment(<<~HTML)
91
- # # <section>
92
- # # <h1>Heading</h1>
93
- # # </section>
94
- # # HTML
95
- # # section = doc.select("section").first
96
- # # section.html #=> "<section>\n<h1>Heading</h1>\n</section>"
97
- # #
98
- # # @!method html
99
- # # @return [String]
100
- #
101
- # # Returns the element's inner HTML
102
- # #
103
- # # @example
104
- # # doc = Sawzall.parse_fragment(<<~HTML)
105
- # # <section>
106
- # # <h1>Heading</h1>
107
- # # </section>
108
- # # HTML
109
- # # section = doc.select("section").first
110
- # # section.inner_html #=> "\n<h1>Heading</h1>\n"
111
- # #
112
- # # @!method inner_html
113
- # # @return [String]
114
- #
115
- # # Returns the given attribute's value or `nil`
116
- # #
117
- # # @example
118
- # # doc = Sawzall.parse_fragment("<h1 id='title'>Heading</h1>")
119
- # # h1 = doc.select("h1").first
120
- # # h1.attr("id") #=> "title"
121
- # # h1.attr("class") #=> nil
122
- # #
123
- # # @!method attr(attribute)
124
- # # @param attribute [String]
125
- # # @return [String, Nil]
126
- #
127
- # # Returns the child elements that match the given CSS selector
128
- # #
129
- # # https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors
130
- # #
131
- # # @example
132
- # # doc = Sawzall.parse_fragment(<<~HTML)
133
- # # <div class="container">
134
- # # <div>inner div 1</div>
135
- # # <div>inner div 2</div>
136
- # # </div>
137
- # # HTML
138
- # # container = doc.select(".container").first
139
- # # matches = container.select("div")
140
- # # matches.map(&:text) #=> ["inner div 1", "inner div 2"]
141
- # #
142
- # # @!method select(css_selector)
143
- # # @param css_selector [String]
144
- # # @raise [ArgumentError] if the CSS selector is invalid
145
- # # @return [Array<Sawzall::Element>]
146
- #
147
- # # Returns the element's child elements
148
- # #
149
- # # @example
150
- # # doc = Sawzall.parse_fragment(<<~HTML)
151
- # # <div id="parent">
152
- # # <div id="child1">
153
- # # <div id="grandchild1"></div>
154
- # # </div>
155
- # # <div id="child2"></div>
156
- # # </div>
157
- # # HTML
158
- # # parent = doc.select("#parent").first
159
- # # parent
160
- # # .child_elements
161
- # # .map { it.attr("id") } #=> ["child1", "child2"]
162
- # #
163
- # # @!method child_elements
164
- # # @return [Array<Sawzall::Element>]
165
- #
166
- # # Returns the element's text content using a very simplified version of the
167
- # # `innerText` algorithm.
168
- # #
169
- # # https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
170
- # #
171
- # # @example
172
- # # doc = Sawzall.parse_fragment(<<~HTML)
173
- # # <ul>
174
- # # <li>First item</li>
175
- # # <li>Second item</li>
176
- # # </ul>
177
- # # HTML
178
- # # ul = doc.select("ul").first
179
- # # ul.text #=> "First item\nSecond item"
180
- # #
181
- # # @!method text
182
- # # @return [String]
183
- # end
76
+ class Element
77
+ # @!group 1) Querying
78
+
79
+ # Returns the element's name in lowercase
80
+ #
81
+ # @example
82
+ # doc = Sawzall.parse_fragment("<p>Paragraph</p>")
83
+ # doc.select("p").first.name #=> "p"
84
+ #
85
+ # @!method name
86
+ # @return [String]
87
+
88
+ # Returns the element's outer HTML
89
+ #
90
+ # @example
91
+ # doc = Sawzall.parse_fragment(<<~HTML)
92
+ # <section>
93
+ # <h1>Heading</h1>
94
+ # </section>
95
+ # HTML
96
+ # section = doc.select("section").first
97
+ # section.html #=> "<section>\n<h1>Heading</h1>\n</section>"
98
+ #
99
+ # @!method html
100
+ # @return [String]
101
+
102
+ # Returns the element's inner HTML
103
+ #
104
+ # @example
105
+ # doc = Sawzall.parse_fragment(<<~HTML)
106
+ # <section>
107
+ # <h1>Heading</h1>
108
+ # </section>
109
+ # HTML
110
+ # section = doc.select("section").first
111
+ # section.inner_html #=> "\n<h1>Heading</h1>\n"
112
+ #
113
+ # @!method inner_html
114
+ # @return [String]
115
+
116
+ # Returns the given attribute's value or `nil`
117
+ #
118
+ # @example
119
+ # doc = Sawzall.parse_fragment("<h1 id='title'>Heading</h1>")
120
+ # h1 = doc.select("h1").first
121
+ # h1.attr("id") #=> "title"
122
+ # h1.attr("class") #=> nil
123
+ #
124
+ # @!method attr(attribute)
125
+ # @param attribute [String]
126
+ # @return [String, Nil]
127
+
128
+ # Returns the element's attributes as an array of key-value pairs
129
+ #
130
+ # @example
131
+ # doc = Sawzall.parse_fragment("<h1 id='title' class='big'>Heading</h1>")
132
+ # h1 = doc.select("h1").first
133
+ # h1.attrs #=> [["class", "big"], ["id", "title"]]
134
+ #
135
+ # @!method attrs
136
+ # @return [Array<Array(String, String)>]
137
+
138
+ # Returns the child elements that match the given CSS selector
139
+ #
140
+ # https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors
141
+ #
142
+ # @example
143
+ # doc = Sawzall.parse_fragment(<<~HTML)
144
+ # <div class="container">
145
+ # <div>inner div 1</div>
146
+ # <div>inner div 2</div>
147
+ # </div>
148
+ # HTML
149
+ # container = doc.select(".container").first
150
+ # matches = container.select("div")
151
+ # matches.map(&:text) #=> ["inner div 1", "inner div 2"]
152
+ #
153
+ # @!method select(css_selector)
154
+ # @param css_selector [String]
155
+ # @raise [ArgumentError] if the CSS selector is invalid
156
+ # @return [Array<Sawzall::Element>]
157
+
158
+ # Returns the element's child elements
159
+ #
160
+ # @example
161
+ # doc = Sawzall.parse_fragment(<<~HTML)
162
+ # <div id="parent">
163
+ # <div id="child1">
164
+ # <div id="grandchild1"></div>
165
+ # </div>
166
+ # <div id="child2"></div>
167
+ # </div>
168
+ # HTML
169
+ # parent = doc.select("#parent").first
170
+ # parent
171
+ # .child_elements
172
+ # .map { it.attr("id") } #=> ["child1", "child2"]
173
+ #
174
+ # @!method child_elements
175
+ # @return [Array<Sawzall::Element>]
176
+
177
+ # Returns the element's text content using a very simplified version of the
178
+ # `innerText` algorithm.
179
+ #
180
+ # https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
181
+ #
182
+ # @example
183
+ # doc = Sawzall.parse_fragment(<<~HTML)
184
+ # <ul>
185
+ # <li>First item</li>
186
+ # <li>Second item</li>
187
+ # </ul>
188
+ # HTML
189
+ # ul = doc.select("ul").first
190
+ # ul.text #=> "First item\nSecond item"
191
+ #
192
+ # @!method text
193
+ # @return [String]
194
+
195
+ # Checks whether the element has the given class
196
+ #
197
+ # @example
198
+ # doc = Sawzall.parse_fragment("<h1 class='title'>Heading</h1>")
199
+ # h1 = doc.select("h1").first
200
+ # h1.has_class?("title") #=> true
201
+ # h1.has_class?("TITLE", case_sensitive: false) #=> true
202
+ # h1.has_class?("heading") #=> false
203
+ #
204
+ # @!method has_class?(css_class, case_sensitive: true)
205
+ # @param css_class [String]
206
+ # @param case_sensitive [Boolean]
207
+ # Whether matching should be case sensitive. When `false`, only ASCII characters are matched case-insensitively.
208
+ # @return [Boolean]
209
+
210
+ # Returns the element's classes
211
+ #
212
+ # @example
213
+ # doc = Sawzall.parse_fragment("<h1 class='one two'>Heading</h1>")
214
+ # h1 = doc.select("h1").first
215
+ # h1.classes #=> ["one", "two"]
216
+ #
217
+ # @!method classes
218
+ # @return [Array<String>]
219
+
220
+ # @!endgroup
221
+
222
+ # @!group 2) Debugging
223
+
224
+ # Overrides Ruby's default `Object#inspect` so the output is a bit more useful
225
+ def inspect
226
+ "<#{self.class.name} name=#{name.inspect} child_elements=#{child_elements.inspect}>"
227
+ end
228
+
229
+ # Provides a custom pretty-printing implementation for Ruby's `PP`
230
+ def pretty_print(pp)
231
+ pp.group(2, "#(#{self.class.name} {", "})") do
232
+ pp.breakable
233
+
234
+ fields = [:name]
235
+ fields << :child_elements unless child_elements.empty?
236
+
237
+ pp.seplist(fields) do |field|
238
+ case field
239
+ when :name
240
+ pp.text("name = ")
241
+ pp.pp(name)
242
+ when :child_elements
243
+ pp.group(2, "child_elements = [", "]") do
244
+ pp.breakable
245
+ pp.seplist(child_elements) do |child|
246
+ pp.pp(child)
247
+ end
248
+ end
249
+ end
250
+ end
251
+
252
+ pp.breakable
253
+ end
254
+ end
255
+
256
+ # @!endgroup
257
+ end
184
258
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sawzall
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre2
4
+ version: 0.1.0.pre3
5
5
  platform: x86_64-linux
6
6
  authors:
7
7
  - David Cornu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-05-12 00:00:00.000000000 Z
11
+ date: 2025-05-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Sawzall wraps the Rust scraper library (https://github.com/rust-scraper/scraper)