Crystagiri
An Html parser library for Crystal like amazing Nokogiri Ruby gem.
I not pretend that Crystagiri does much as Nokogiri. All help will be welcome! :)
Installation
Add this to your application's shard.yml
:
dependencies:
crystagiri:
github: madeindjs/crystagiri
and then run
$ crystal deps
Usage
require "crystagiri"
Then you can simply instanciate a Crystagiri::HTML
from a Html String
like this
doc = Crystagiri::HTML.new "<h1>Crystagiri is awesome!!</h1>"
... or directly load it from a Web Url or a pathname:
doc = Crystagiri::HTML.from_file "README.md"
doc = Crystagiri::HTML.from_url "http://example.com/"
Then you can search all XML::Node
by:
- CSS query
puts doc.at_css("a.method-permalink")
# => <a class="method-permalink"> .. </a>
puts doc.css("li > strong.title") { |node| puts node}
# => <strong class="title"> .. </strong>
# => <strong class="title"> .. </strong>
- HTML tag
doc.where_tag("h2") { |node| puts node }
# => <h2> .. </h2>
# => <h2> .. </h2>
# => <h2> .. </h2>
- HTML id
puts doc.at_id("main-content")
# => <div id="main-content"> ... </div>
- HTML class attribute
doc.where_class("summary") { |node| puts node }
# => <div class="summary"> .. </div>
# => <div class="summary"> .. </div>
# => <div class="summary"> .. </div>
Know limitations: For the moment you can't use css query with complex search like
:nth-child
Development
Clone this repository and go in it:
$ git clone https://github.com/madeindjs/crystagiri.git
$ cd crystagiri
You can generate the complete documentation with
$ crystal doc
And run spec tests to ensure all work correctly
$ crystal spec
Contributing
- Fork it ( https://github.com/madeindjs/crystagiri/fork )
- Create your feature branch
git checkout -b my-new-feature
- Commit your changes
git commit -am "Add some feature"
- Push to the branch
git push origin my-new-feature
- Create a new Pull Request
Contributors
- madeindjs - creator, maintainer