HtmlGen

A small and fast framework to parse and generate HTML in Ruby.

Install

Add to your Gemfile and bundle:

gem "html_gen"

Usage

Generate elements

div = HtmlGen::Element.new(:div, classes: ["class1", "class2"], attr: {width: "100px"}, css: {height: "50px"})
div.add_str "Hello world"

p = div.add_ele(:p)
p.add_str "Test"

div.html #=> '<div width="100px" style="height: 50px;" class="class1 class2">Hello world<p>Test</p></div>'

With text in initializer:

HtmlGen::Element.new(:div, str: "Hello world")

Parse HTML into elements

parser = HtmlGen::Parser.new(str: "<html><head><title>Test</title></head><body>This is the body</body></html>")
html = parser.eles.first
head = html.eles.first

head.name #=> "head"

title = head.eles.first
title.html #=> "Test"
title.attr #=> {}
title.css #=> {}
title.data #=> {}

Contributing to html_gen

Copyright © 2012 Kasper Johansen. See LICENSE.txt for further details.