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…).
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…).
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.
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?
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.
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.
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.
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
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 (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.
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.
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.
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”)