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).
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...).
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.
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...).
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.
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.
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)
"<html>\n" +
self.new.instance_eval(&block) +
"\n</html>"
end
def body
"<body>\n#{ yield }\n</body>"
end
def h1
"<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 (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.
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 <marnen@marnen.org>
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.
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.
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.
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.
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
Justin Collins 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.
Umm only if you're allowed to use a framework that supports
"vendoring" (whatever that is).
···
On Thu, 05 Nov 2009 12:31:43 +0900, Gregory Brown wrote:
But FWIW, Haml can easily be vendored, so you can probably sneak it in
just as easily.
-greg
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/
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")
Why are you asking? In the time it took you to type that, you could
have executed it and inspected the result.
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?
-greg
···
On Wed, Nov 4, 2009 at 10:45 PM, William Manire <williamkmanire@gmail.com> 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).
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.
···
_________________________________________________________________
Bing brings you maps, menus, and reviews organized in one place.