Build Status Dependency Status

Former turns an html string into a set of editable fields. For instance, if you want to give users the ability to edit certain parts of pregenerated HTML (like the href's of a's), this gem makes it easy to create the form and handle user input.


Add this line to your application's Gemfile:

gem 'former'

And then execute:

$ bundle

Or install it yourself as:

$ gem install former


First, create a new parser that extends Builder class.

class Parser < Former::Builder
  attr "a.important", :href
  attr "img", :src
  text "p", "a.important"

In this example, we want to be able to edit the location of all links that have a class of “important”, the location of all images, and the text in any paragraphs or important links. Note that the text method takes any number of parameters which each will be evaluated for text children (or, if you give no parameters, all text nodes will be included).

To produce the input fields from some example input HTML, first create an instance of your parser.

parsed = "<p>some text<a class='important' href=''>some link text<img src='/an/image/path' /></a>last text</p>"

Then, we can get information about the editable fields:

parsed.each { |elem| puts "#{} is #{elem.value}" }

And, even create an html form from this:

parsed.each { |elem| puts "<input name='former_0' type='text' value='#{elem.value}' />" }

You can also convert the original html to json:

puts parsed.to_json

You can then set fields individually or as a group (with, say, the params from a POST/GET):

parsed[0] = "A New Description"
parsed[1] = ""
# or...
parsed.set_values :former_0 => "A New Description", :former_1 => ""

When the new values have been set, you can spit out the new HTML version:

puts parsed.to_html

Will produce the original html with the new field values:

<p>A New Description<a class='important' href=''>some link...

If you want finer grained control over what's considered an editable element, you can pass a block in as well to the attr method. For instance, if you want only text fields that are not empty, you could use:

class Parser < Former::Builder
  text("p", "a.important") { |elem| not elem.text.strip.empty? }

p ="<p>\n</p><p>some text</p>").length # this is only 1
p[0] = 'other text'
p.to_html # => '<p>\n</p><p>other text</p>'