tags:

views:

68

answers:

2

I have to create a report, and in order to do some colouring and tables, I decided on HTML. Is there any gem I could use to do this? I'd like to avoid having to write the tags myself.

+1  A: 

You can take a look at Markaby which lets you generate HTML through a Ruby DSL.

An example from the official docs:

  require 'markaby'

  mab = Markaby::Builder.new
  mab.html do
    head { title "Boats.com" }
    body do
      h1 "Boats.com has great deals"
      ul do
        li "$49 for a canoe"
        li "$39 for a raft"
        li "$29 for a huge boot that floats and can fit 5 people"
      end
    end
  end
  puts mab.to_s

Alternatively you can look into one of the many template engines available. For example:

m5h
Added RedCloth to list of template engines as suggested by Jonas in another answer.
m5h
I find Markaby the easiest to work with. Thanks!
Geo
+1  A: 

Check out the html-table gem.

sudo gem install html-table

require 'html/table'
include HTML
report=[["Customer1",2042.3],["Customer2",12345.6],["Customer3",4711.0]]
table=Table.new(report)
puts table.html
<table>
   <tr>
      <td>Customer1</td>
      <td>2042.3</td>
   </tr>
   <tr>
      <td>Customer2</td>
      <td>12345.6</td>
   </tr>
   <tr>
      <td>Customer3</td>
      <td>4711.0</td>
   </tr>
</table>

Also I've used RedCloth for something similar.

require 'redcloth'
RedCloth.new("|{background:#ddd}. Cell with background|Normal|").to_html
Jonas Elfström