Hi,

How do you create HTML files using RUBY. I have a requirement where I need to set color and font of the text, and also provide formatting options for tables having different colors for rows and border(on/off).

Can anybody tell how to do this

Thanks,
K

Recommended Answers

All 4 Replies

There's really nothing special other than just outputting HTML syntax inside of a file named with an htm or html extension.

fileHtml = File.new("fred.html", "w+")
fileHtml.puts "<HTML><BODY BGCOLOR='green'>"
fileHtml.puts "<CENTER>this is neat</CENTER>"
fileHtml.puts "</BODY></HTML>"
fileHtml.close()

...here's a more robust example (with sloppy html):

fileHtml = File.new("fred.html", "w+")
fileHtml.puts "<HTML><BODY BGCOLOR='green'>"
fileHtml.puts "<CENTER>this is neat</CENTER><br>"
fileHtml.puts "<CENTER><FONT COLOR='yellow'>this is neat</FONT></CENTER>"

fileHtml.puts "<TABLE BORDER='1' ALIGN='center'>"
fileHtml.puts "<TR><TH>HEADER</TH></TR>"
fileHtml.puts "<TR><TD>Cell in <FONT COLOR='RED'>TableThing</FONT></TD></TR>"
fileHtml.puts "</TABLE>"

fileHtml.puts "<TABLE BORDER='0' ALIGN='center'>"
fileHtml.puts "<TR><TH>HEADER on borderless</TH></TR>"
fileHtml.puts "<TR><TD>Cell in borderless<FONT COLOR='white'>TableThing</FONT></TD></TR>"
fileHtml.puts "</TABLE>"
fileHtml.puts "</BODY></HTML>"
fileHtml.close()

system("start fred.html") #...on windows

You also can use a (rather antiquidated, but included) cgi class.
The CGI class would be like

#!/usr/bin/env ruby
# *-ruby-*-
require 'cgi'

# Create an instance of CGI, with HTML 4 output
cgi = CGI.new("html4")

# Send the following to the CGI object's output
cgi.out {
  cgi.html {

# Produce a header
cgi.head { cgi.title { "This is a title" }
} +

# Produce a body
cgi.body {
  cgi.h1 { "This is a headline" } +
    cgi.p { "Hello, world." }
}
  }
}

Docs: http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html
If your looking for templating etc, try looking at haml-lang.org

Sub: Create Api to create document and add, edit, delete text and
table(add row, modify and delete row)

Can somebody please tell me how to implement this using inheritence /
composition / patterns ?

Thanks,
skrithikaa Sub: Create Api to create document and add, edit, delete text and
table(add row, modify and delete row)

Can somebody please tell me how to implement this using inheritence /
composition / patterns ?

Thanks,
skrithikaa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.