Create HTML files using RUBY

K

Krithika San

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
 
M

Marnen Laibow-Koser

Krithika said:
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,
 
K

Krithika San

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
 
M

Marnen Laibow-Koser

Krithika said:
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,
 
K

Krithika San

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
 
M

Marnen Laibow-Koser

Krithika said:
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,
 
G

Gregory Brown

So don't use a Web framework. =A0Haml 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
 
M

Marc Heiler

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...
 
W

William Manire

[Note: parts of this message were removed to make it a legal post.]

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

http://en.wikipedia.org/wiki/Brainfuck
 
G

Greg Barozzi

Krithika said:
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 "<H1>"
yield
print "</H1>"
end

h1 do
print "I'm a heading!"
end

=> <H1>I'm a heading!</h1>


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.
 
M

Marnen Laibow-Koser

Greg Barozzi wrote:
[...]
If I was going to write this myself I'd try something like:

def h1
print "<H1>"
yield
print "</H1>"
end

h1 do
print "I'm a heading!"
end

=> <H1>I'm a heading!</h1>


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,
 
J

Justin Collins

Krithika said:
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

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
 
W

William Manire

[Note: parts of this message were removed to make it a legal post.]

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 :)

Good luck

2009/11/4 Marnen Laibow-Koser said:
Greg Barozzi wrote:
[...]
If I was going to write this myself I'd try something like:

def h1
print "<H1>"
yield
print "</H1>"
end

h1 do
print "I'm a heading!"
end

=> <H1>I'm a heading!</h1>


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,
 
K

Ken Bloom

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).
 
E

Ehsanul Hoque

If I was going to write this myself I'd try something like:
=20
def h1
print "<H1>"
yield
print "</H1>"
end
=20
h1 do
print "I'm a heading!"
end
=20
=3D> <H1>I'm a heading!</h1>
=20
=20
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.
=20
Good luck.


That's nice. I suggest that if the OP wants to use your suggested method=2C=
maybe do it this way:


class MyHtml
def self.generate(&block)
"<html>\n" +
self.new.instance_eval(&block) +
"\n</html>"
end
def body

=20
"<body>\n#{ yield }\n</body>"
end

def h1
=20
"<h1>#{ yield }</h1>"

end
end

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


Or just use builder or something :p (though it seems like that just might n=
ot be allowed for the OP=2C as I suspect whoever is assigning this work exp=
ects a real html generator implementation.

- Ehsan
=20
_________________________________________________________________
Hotmail: Trusted email with powerful SPAM protection.
http://clk.atdmt.com/GBL/go/177141665/direct/01/=
 
E

Ehsanul Hoque

=20
Umm only if you're allowed to use a framework that supports=20
"vendoring" (whatever that is).

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

If you're using rubygems=2C as almost everyone=2C just use the "gem unpack=
haml" in your application if you want to vendor haml. Then you just add "r=
equire 'haml/lib/haml'" or whatever the path is to your application. No fra=
mework required. Without rubygems=2C just copy the source in=2C easy enough=
 
G

Gregory Brown

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.
 
D

David

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

I don't think the OP said he couldn't use frameworks. I think he can't
use a *web* framework, probably because he wants something more
lightweight.

I second the HAML or ERb suggestion.

http://haml-lang.com/

David
 
K

Krithika San

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 "<HTML><BODY BGCOLOR='green'>"
file_html.puts "<CENTER>This is a color</CENTER><br>"
file_html.puts "<CENTER><FONT COLOR='yellow'>This is
yellowww</FONT></CENTER>"
file_html.puts "</BODY></HTML>"
file_html.close()
system("start sample.html")


Thanks,
Krithika
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top