[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

HTML Generation (Next Generation CGI

John W. Long

11/22/2003 4:50:00 PM

Hi,

This idea for the next generation of CGI has me thinking (see
http://rubygarden.org/ruby?NextGen...). It seems that CGI would be
best if it were broken in two. One side handled generating html, and the
other handled all the other stuff (params, headers, etc...).

Last night I started prototyping some of my ideas and came up with the
following:

class HtmlGenerator
def render(klass, &blk)
hg = klass.new
hg.render(&blk)
end
end
class Html4
attr_accessor :encoding
def render(&blk)
"<html>#{self.instance_eval(&blk)}\n</html>"
end
def head(&blk)
"\n<head>#{self.instance_eval(&blk)}\n</head>"
end
def title(&blk)
"\n<title>#{self.instance_eval(&blk)}</title>"
end
def meta(hash = {})
"\n" + '<meta name="' + hash[:name] + '" content="' + hash[:content] +
'">'
end
def body(&blk)
"\n<body>#{self.instance_eval(&blk)}\n</body>"
end
def h1(&blk)
"\n<h1>#{self.instance_eval(&blk)}</h1>"
end
def h2(&blk)
"\n<h2>#{self.instance_eval(&blk)}</h2>"
end
def p(&blk)
"\n<p>#{self.instance_eval(&blk)}</p>"
end
def b(&blk)
"<b>#{self.instance_eval(&blk)}</b>"
end
def i(&blk)
"<i>#{self.instance_eval(&blk)}</i>"
end
end

hg = HtmlGenerator.new
puts hg.render(Html4) {
encoding = "US/English"
head {
title { "My Document" } +
meta(:name => "description", :content => "this is a description of this
document")
} +
body {
h1 { "Heading 1" } +
p { "A small paragraph." } +
h2 { "Heading 2" } +
p { b { "Bold" } + " " + i { "Italic" } }
}
}

I like the syntax of this very much, but I would like input on two things.

The first this would require heavy use of instance_eval. Would this be a
good thing? It strikes me that the main problem with instance_eval is that
you can begin to change the interface of the class. However, as this is
implemented above you can see that changes to the implementation would only
effect an instance of the HtmlXX class, which would go into never never land
as soon as the render call is completed.

The second, a bit harder, I would like to remove the need for the pluses in
order to chain the results together. Any Ruby gurus that would like to try
and take a stab at it? I can think of one possible solution, but I'm not
sure if the results would really be satisfactory.

--
John Long
contact me through: www.wiseheartdesign.com



4 Answers

Mauricio Fernández

11/23/2003 9:20:00 AM

0

On Sun, Nov 23, 2003 at 01:50:29AM +0900, John W. Long wrote:
> Last night I started prototyping some of my ideas and came up with the
> following:
[...]
> hg = HtmlGenerator.new
> puts hg.render(Html4) {
> encoding = "US/English"
> head {
> title { "My Document" } +
> meta(:name => "description", :content => "this is a description of this
> document")
> } +
> body {
> h1 { "Heading 1" } +
> p { "A small paragraph." } +
> h2 { "Heading 2" } +
> p { b { "Bold" } + " " + i { "Italic" } }
> }
> }

You can use a technique similar to flgr's Junction or oGMo's criteria to build a
"parse tree".

What would be really cool would be taking a DTD and generating the Ruby
code from that that validates the document as it is built.
It would be possible to define how blocks can be nested, etc, in
practice, ensuring that no illegal sequence of calls is made.

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Are Linux users lemmings collectively jumping off of the cliff of
reliable, well-engineered commercial software?
-- Matt Welsh

Ara.T.Howard

11/23/2003 4:27:00 PM

0

John W. Long

11/24/2003 4:11:00 AM

0

> You can use a technique similar to flgr's Junction or oGMo's criteria to
build a
> "parse tree".

I'm not familiar with either library. How would you see a parse tree helping
in this situation?

> What would be really cool would be taking a DTD and generating the Ruby
> code from that that validates the document as it is built.
> It would be possible to define how blocks can be nested, etc, in
> practice, ensuring that no illegal sequence of calls is made.

Honestly I don't think it would be that hard to do. The hairiness of the
project would be understanding the finer points of dtd. Simply creating a
generator object with a smart method_missing shouldn't be that hard. This
object could then recursively create other generator objects that contain
the appropriate subset of the dtd for the section of the document you are
working on.

--
John Long
http://wiseheart...




John W. Long

11/24/2003 4:25:00 AM

0

> -- Ara Howard wrote: --
> what advantage would this give over:
<snip amrita stuff here />

Actually I have used Amrita. And yes Amrita is a good library. I think it
will be even better after it gets some of the kinks worked out of it. (Maybe
it already has. I last looked at Amrita in the spring.)

I'm not so interested in creating a template library. CGI currently has the
kind of functionality I suggested. Personally I'm not sure I would
personally use it, but it does meet another need. It can generate HTML code
in several different flavors. From HTML3 - XHTML transitional if I remember
right. This is kind of a neat idea, although as I said I'm not totally sure
I would use it myself.

--
John Long
http://wiseheart...