Zurix
Zurix

Reputation: 45

How to print HTML format content of arrays Nokogiri builder

I'm a kind of new to ruby and I'm trying to print in HTML format the content of arrays using Nokogiri HTML builder.

May somebody point me in rigth direction please.

Thanks in advance.

The code I have so far is:

#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'

a = ["Header1","Header2","Header3"]
b = [["x","y","z"],["w","q","t"]]

builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
    doc.body{
            doc.table { 
                doc.tr { 
                    doc.td { 
                        a.each { |v| p v }
                        b.each {|x| p v}
                    }
                }
            }
        }
    }
end
puts builder.to_html

The output I'm looking for is:

<html>
<body>
<table>
<tr>
    <td>Header1</td>
    <td>Header2</td>
    <td>Header3</td>
</tr>
<tr>
    <td>x</td>
    <td>y</td>
    <td>z</td>
</tr>
<tr>
    <td>w</td>
    <td>q</td>
    <td>t</td>
</tr>
</table>
</body>
</html>

I'm getting several errors:

    C:\AWK\XMLs>ruby Nokogiri_HTMLBuilder.rb
"Header1"
"Header2"
"Header3"
Nokogiri_HTMLBuilder.rb:17:in `block (7 levels) in <main>': undefined local variable or method `v' for main:Object (NameError)
        from Nokogiri_HTMLBuilder.rb:17:in `each'
        from Nokogiri_HTMLBuilder.rb:17:in `block (6 levels) in <main>'
        from C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.2.rc2-x86-mingw32/lib/nokogiri/xml/builder.rb:391:in `call'
        from C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.2.rc2-x86-mingw32/lib/nokogiri/xml/builder.rb:391:in `insert'
        from C:/Ruby200/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.2.rc2-x86-mingw32/lib/nokogiri/xml/builder.rb:375:in `method_missing'

Updated data:

###################################################################

Thank you antpaw for your help. I'm going better with your suggestion. It's like you said!!

I only have issues now to print in desired way the output.

I have this code so far: #!/usr/bin/env ruby require 'rubygems' require 'nokogiri'

a = ["Header1","Header2","Header3"]
b = [["x","y","z"],["w","q","t"]]

builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
    doc.body{
            doc.table { 
                doc.tr { 
                    a.each { |v| doc.td { doc.text(v) } }
                }
                doc.tr { 
                    b.each { |v| v.each { |z| doc.td { doc.text(z) } } }
                }
            }
        }
    }
end
puts builder.to_html

I'm getting this output (2nd and 3rd row are merged in a single one, should be separated):

<html>
  <body>
    <table>
      <tr>
        <td>Header1</td>
        <td>Header2</td>
        <td>Header3</td>
      </tr>
      <tr>
        <td>x</td>
        <td>y</td>
        <td>z</td>
        <td>w</td>
        <td>q</td>
        <td>t</td>
      </tr>
    </table>
  </body>
</html>

and output I'm looking for is:

<html>
<body>
<table>
<tr>
    <td>Header1</td>
    <td>Header2</td>
    <td>Header3</td>
</tr>
<tr>
    <td>x</td>
    <td>y</td>
    <td>z</td>
</tr>
<tr>
    <td>w</td>
    <td>q</td>
    <td>t</td>
</tr>
</table>
</body>
</html> 

Thanks for the help so far

2nd update:

#------------------------------------------------------------------#

Hello again,

I was able to get the desired output. I only want to add some empty td's before Headers and a td containing colspan and a class but I'm failing to add correctly the colspan and class. May you help me with this please.

This is the code I have so far, it's working except for the commented line'.

For the commented line I want to get this output:

<td colspan="3" class="MyClass">Some text</td>

Code I have so far:

builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
    doc.body{
            doc.table { 
                doc.tr { 
                    doc.td {}
                    doc.td {}

                    #doc.td {colspan "3"; doc.text("Org")} #Here I want to add a colspan and class

                    a.each { |v| doc.td { doc.text(v) } }
                }   
                b.size.times do |j|
                    doc.tr { 
                        b.inject(0) {|m,l| [m, l.size].max}.times do |i|
                            doc.td {
                                doc.text("#{b[j][i]}")
                            }
                        end 
                    }
                end

            }
        }
    }
end
puts builder.to_html

Thanks again for the help.

Upvotes: 0

Views: 423

Answers (1)

antpaw
antpaw

Reputation: 15985

b.each {|x| p v} needs to be b.each {|v| p v}

but this wont give you the html, try this

             a.each {|v| doc.text(v) }
             b.each {|v| doc.text(v)}

Upvotes: 0

Related Questions