What gem can I use to generate a HTML file for offline use?

瘦欲@ 提交于 2020-01-01 22:15:14

问题


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:


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:

  • RDiscount/Markdown (The markup used in Stack Overflow when editing)
  • Haml
  • RedCloth (See Jonas answer for an example)



回答2:


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


来源:https://stackoverflow.com/questions/1796459/what-gem-can-i-use-to-generate-a-html-file-for-offline-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!