hashtml [ ![Codeship Status for MRod15/hashtml](https://codeship.io/projects/6dd49080-19a4-0132-515f-0a39251edeca/status)](https://codeship.io/projects/34440) [![Build Status](https://travis-ci.org/MRod15/hashtml.svg?branch=master)](https://travis-ci.org/MRod15/hashtml) ======= HashTML is a gem for parsing HTML documents to Ruby Hash-like objects ## Installation HashTML is available as a RubyGem: gem install hashtml ## Usage HashTML parses a Nokogiri::HTML::Document or anything that responds to to_s with a string of valid HTML. A HashTML object corresponding to the data structure of the given HTML is generated. ### Example: html = <<-HTML

hello world!

HTML hashtml = HashTML.new(html) hashtml.inspect # => #, #"d1", "style"=>"color: blue"}, @children=[#, #]>, #]>, #, #"d2", "style"=>"color: green"}, @children=[#, #]>, #]>, #]>]>]>> HashTML allows you to convert the object to a Ruby Hash with to_h. ### Example: html = <<-HTML

hello world!

HTML hashtml = HashTML.new(html) hashtml.to_h # => {"document"=>{:attributes=>{}, :children=>[{"html"=>{:attributes=>{}, :children=>[{"body"=>{:attributes=>{}, :children=>[{:text=>"\n "}, {"div"=>{:attributes=>{"id"=>"d1", "style"=>"color: blue"}, :children=>[{:text=>"\n "}, {"h1"=>{:attributes=>{}, :children=>[{:text=>"hello world!"}]}}, {:text=>"\n "}]}}, {:text=>"\n "}, {"div"=>{:attributes=>{"id"=>"d2", "style"=>"color: green"}, :children=>[{:text=>"\n "}, {"p"=>{:attributes=>{}, :children=>[{:text=>"Lorem ipsum dolor sit amet, consectetur adipiscing elit."}]}}, {:text=>"\n "}]}}, {:text=>"\n "}]}}]}}]}} You can access elements and change them simply by "navigating" trough them. And when you're done, simply regenerate your HTML by doing to_html! ### Example: html = <<-HTML

hello world!

HTML hashtml = HashTML.new(html) hashtml.document.hmtl.body.div.inspect # => #"d1", "style"=>"color: blue"}, @children=[#, #]>, #]> hashtml.document.hmtl.body.div.attributes['id'] = 'new_id1' hashtml.document.hmtl.body.div.inspect # => #"new_id1", "style"=>"color: blue"}, @children=[#, #]>, #]> hashtml.document.hmtl.body.div.h1.text # => 'hello world!' hashtml.document.hmtl.body.div.h1.text = 'such edit! wow' hashtml.document.hmtl.body.div.h1.text # => 'such edit! wow' hashtml.to_html # =>

such edit! wow

Worried about navigating and having tons of elements with the same tag at the same level? That's not a problem! Just identify the node by it's attributes! ### Example: html = <<-HTML

hello world!

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

HTML hashtml = HashTML.new(html) hashtml.document.html.body.div.span({'id' => 's2'}).attributes['id'] = 'new_id2' hashtml.document.html.body.div.span({'id' => 's1'}).h1.text = 'such edit! much navigation! wow' hashtml.to_html # =>

such edit! much navigation! wow

Lorem ipsum dolor sit amet, consectetur adipiscing elit.