Create HTML files using RUBY

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

Krithika San wrote:

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

Use a module such as Haml or ERb. If you need a full Web application,
use one of the Ruby Web frameworks (Rails, Merb, Sinatra, Ramaze…).

Thanks,
K

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi Marnen,

Thanks for your suggestion. I purely have to use only RUBY. Does cgi
script allows to do the below mentioned features?

Thanks in advance,
Krithika

Marnen Laibow-Koser wrote:

Krithika San wrote:

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

Use a module such as Haml or ERb. If you need a full Web application,
use one of the Ruby Web frameworks (Rails, Merb, Sinatra, Ramaze…).

Thanks,
K

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I am sorry I was not clear in my previous reply. I am told not to use
Rails or rather not implementing Web Framework.

My previous project was to create elements like text and table, edit
them in memory and then write them into a file. This is an extension of
it where I need to provide HTML support.
I need to provide options for changing the color of text, font change
and then create tables with rows having diferent colors.

Thanks,
Krithika

Marnen Laibow-Koser wrote:

Krithika San wrote:

Hi Marnen,

Thanks for your suggestion. I purely have to use only RUBY. Does cgi
script allows to do the below mentioned features?

Everything I mentioned is only Ruby. Use it – otherwise you’ll be
reinventing the wheel for no good reason.

Thanks in advance,
Krithika

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Krithika San wrote:

Hi Marnen,

Thanks for your suggestion. I purely have to use only RUBY. Does cgi
script allows to do the below mentioned features?

Everything I mentioned is only Ruby. Use it – otherwise you’ll be
reinventing the wheel for no good reason.

Thanks in advance,
Krithika

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

How do you create HTML files using RUBY.

A html file is nothing else than a long string.

You can generate that string via any language easily. Or you could use
something “ready-made” like cgi or the template engines suggested above.

I really am not sure where the problem is because a .html file is
nothing than a big string…

On Wed, Nov 4, 2009 at 9:40 PM, Marnen Laibow-Koser [email protected]
wrote:

Krithika San wrote:

I am sorry I was not clear in my previous reply. I am told not to use
Rails or rather not implementing Web Framework.

So don’t use a Web framework. Haml and ERb can both work on their own.

As a clarifying point for the OP, ERb is part of standard Ruby. It is
not a third party library.
(Which is not the case with Haml)

But FWIW, Haml can easily be vendored, so you can probably sneak it in
just as easily.

-greg

Krithika San wrote:

I am sorry I was not clear in my previous reply. I am told not to use
Rails or rather not implementing Web Framework.

So don’t use a Web framework. Haml and ERb can both work on their own.

My previous project was to create elements like text and table, edit
them in memory and then write them into a file. This is an extension of
it where I need to provide HTML support.
I need to provide options for changing the color of text, font change
and then create tables with rows having diferent colors.

Use one of the libraries I mentioned above.

Thanks,
Krithika

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I challenge that statement

You can generate that string via any language easily. Or you could use

something “ready-made” like cgi or the template engines suggested above.

have a look at this language

On Wed, Nov 4, 2009 at 10:45 PM, William Manire
[email protected] wrote:

have a look at this language

Brainfuck - Wikipedia

Brainfuck is Turing complete, so it can do anything Ruby can do. As
for making it easier… just compile whatever higher level language
you’d like down to Brainfuck. Or is that cheating? :slight_smile:

-greg

Krithika San wrote:

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

If I was going to write this myself I’d try something like:

def h1
print “


yield
print “


end

h1 do
print “I’m a heading!”
end

=>

I’m a heading!

By yielding to the block you allow yourself to nest other tags
inside of tags which is something you often have to do when writing
html.

Good luck.

Greg B. wrote:
[…]

If I was going to write this myself I’d try something like:

def h1
print “


yield
print “


end

h1 do
print “I’m a heading!”
end

=>

I’m a heading!

By yielding to the block you allow yourself to nest other tags
inside of tags which is something you often have to do when writing
html.

Good luck.

Good point. This is the approach that Builder and Markaby take. In Web
apps, I prefer my markup not to look like Ruby, but in a situation like
that of the OP, Builder might be useful.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Krithika San wrote:

Krithika

What you are trying to do is still unclear to me, but the CGI library
included with Ruby probably does what you need, if you are not allowed
to use any external libraries.

http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html

-Justin

You’ve gotta remember all of the attributes and stuff to properly
support
CSS and the standard.

You have to decide what standard you want to implement. HTML X.X,
XHTML
etc…

w3schools is an awesome site that has a lot of information about the
standards, but I would also check out the RFCs. Also remember that some
of
the standards seem to be universally ignored by all (or most) and you’ll
have to deal with related maintenance issues as a result :slight_smile:

Good luck

2009/11/4 Marnen Laibow-Koser [email protected]

On Thu, 05 Nov 2009 12:31:43 +0900, Gregory B. wrote:

But FWIW, Haml can easily be vendored, so you can probably sneak it in
just as easily.

-greg

Umm only if you’re allowed to use a framework that supports
“vendoring” (whatever that is).

end

=>

I’m a heading!

By yielding to the block you allow yourself to nest other tags
inside of tags which is something you often have to do when writing
html.

Good luck.

That’s nice. I suggest that if the OP wants to use your suggested
method, maybe do it this way:

class MyHtml
def self.generate(&block)
“\n” +
self.new.instance_eval(&block) +
“\n”
end
def body

“\n#{ yield }\n”
end

def h1

#{ yield }

end
end

MyHtml.generate do
body do
h1 { “This heading was generated by pure ruby” }
end
end

Or just use builder or something :stuck_out_tongue: (though it seems like that just
might not be allowed for the OP, as I suspect whoever is assigning this
work expects a real html generator implementation.

  • Ehsan

But FWIW, Haml can easily be vendored, so you can probably sneak it in
just as easily.

-greg

Umm only if you’re allowed to use a framework that supports
“vendoring” (whatever that is).

Errr, that has nothing to do with the framework. Vendoring just means
including the entire source as part of your project, instead of assuming
the environment will have it set up (through rubygems or whatever).

If you’re using rubygems, as almost everyone, just use the “gem unpack
haml” in your application if you want to vendor haml. Then you just add
“require ‘haml/lib/haml’” or whatever the path is to your application.
No framework required. Without rubygems, just copy the source in, easy
enough.

On Thu, Nov 5, 2009 at 12:08 AM, Ken B. [email protected] wrote:

On Thu, 05 Nov 2009 12:31:43 +0900, Gregory B. wrote:

But FWIW, Haml can easily be vendored, so you can probably sneak it in
just as easily.

-greg

Umm only if you’re allowed to use a framework that supports
“vendoring” (whatever that is).

All it means is packaging someone elses source with yours and adding
it somewhere in the loadpath. No framework needed.

I am told to create static HTML page and asked to write something like
what Marnen has quoted. External library and CGI script are also not
required to use.
So let me try what Marnen has mentioned.

Could you tell me if I can do something like this

file_html = File.new(“sample.html”, “w+”)
file_html.puts “”
file_html.puts “This is a color

file_html.puts “This is
yellowww”
file_html.puts “”
file_html.close()
system(“start sample.html”)

Thanks,
Krithika

Justin C. wrote:

Krithika San wrote:

Krithika

What you are trying to do is still unclear to me, but the CGI library
included with Ruby probably does what you need, if you are not allowed
to use any external libraries.

http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/HtmlExtension.html

-Justin

I cant try it for next few days, there is some issue with my computer.
How do you think the code can be made better.

Marnen Laibow-Koser wrote:

Krithika San wrote:

I am told to create static HTML page and asked to write something like
what Marnen has quoted. External library and CGI script are also not
required to use.
So let me try what Marnen has mentioned.

Could you tell me if I can do something like this

file_html = File.new(“sample.html”, “w+”)
file_html.puts “”
file_html.puts “This is a color

file_html.puts “This is
yellowww”
file_html.puts “”
file_html.close()
system(“start sample.html”)