[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby in XML.

John Carter

7/8/2005 12:22:00 AM

23 Answers

David Mitchell

7/8/2005 12:45:00 AM

0

How does this work for loops? For example this won't work:

<!-- for @i in 0...5 -->
<img src="#{@i}.jpg"/>
<!-- end -->

How would you suggest I achieve this? Perhaps:

<!-- for @i in 0...5
print "<img src='#{@i}.jpg'/>"
end -->

I can quickly see that becoming an escaping nightmare.

I really like the idea but I would want it to be this flexible.

David

John Carter wrote:
> I have just stuck this on..
> http://www.rubygarden.org/ruby...
>
> I like XML.
>
> There is a firm standard, there is a rich toolset to work on it.
>
> I like HTML. It is simply the fastest way to deliver good looking
> documents to the widest audience.
>
> No surprise. I like XHTML it _is_ HTML in XML. I can validate my XHTML
> documents and know that they conform exactly to the standard, and hence
> will render properly on a wide set of browsers.
>
> I love ruby. It is quite the easiest way to program. It has a lovely XML
> API called REXML.
>
> I sometimes need to do spreadsheet sort of things. Basically a document
> that describes my reasoning and findings, supported by numbers.
>
> Long time ago, when I still did Perl, I found by actual trials that I
> was about as fast in Perl as the average guy is using a Spreadsheet.
> Sometimes faster, sometimes slower. But for the next hundred data sets,
> my perl scripts where a thousand times faster.
>
> So I don't do spreadsheets these days, I write ruby scripts.
>
> So I have taken to combining Ruby & HTML. Sometimes via cgi. It works
> for me.
>
> But sometimes I have documents that are more HTML than ruby. So it makes
> sense to write them in HTML, with a bit of Ruby embedded. That's where
> erb and eruby live.
>
> But I don't like erb and eruby's tags. I can't validate my XHTML.
>
> So add REXML and I present a very small script I call rubyexml. Ruby
> Embedded in XML.
>
> #!/usr/bin/ruby -w
>
> require 'rexml/document'
> require 'rexml/streamlistener'
> require 'pp'
>
> # All eval's are evaluated in the context of an instance of this class.
> # Extend this, or add this method to a class of your own.
> class Context
>
> def eval_value( value)
> value.gsub( %r{ \#\{ ( [^\}]+ ) \} }x) do | match|
> instance_eval( $1).to_s
> end
> end
> end
>
>
> # This does the work.
> class Listener
> include REXML::StreamListener
>
> def initialize( context)
> @context = context
> end
>
> def comment( text)
> print @context.instance_eval( text)
> rescue SyntaxError => details
> pp @context
> pp text
> raise "Failed to compile '#{text}' in context : #{details}"
> end
>
> def tag_start(name,attrs)
> print "<",name
> attrs.each_pair do |key, value|
> print " #{key}=\"#{@context.eval_value( value)}\""
> end
> print ">"
> end
>
> def tag_end( name)
> print "</", name, ">"
> end
>
> def text( text)
> print @context.eval_value(text)
> end
>
> def cdata( ctext)
> text( ctext)
> end
> end
>
> # This comes for free from REXML. Stream parse an XML document.
> REXML::Document::parse_stream( REXML::SourceFactory::create_from(
> STDIN), Listener::new( Context.new))
>
>
> So take a chunk of XHTML...
>
> <?xml version="1.0"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
> "xhtml11.dtd" >
> <html xmlns="HTTP://www.w3.org/TR/xhtml"
> xmlns:xlink="HTTP://www.w3.org/XML/XLink/0.9"
> xml:lang="en" >
> <head>
> <title>
>
> </title>
> </head>
>
> <body>
> <h1>
> The answer to life, the universe and everything is <!-- 44 - 2 -->
> </h1>
>
> <p>
> The following image is <!-- @file_name =
> "pretty_picture.jpg" -->
>
> <img src="#{@file_name}" alt = "#{@file_name.sub(/\.jpg/,'')}"/>
> </p>
> </body>
> </html>
>
> It validates as correct xml against the XHTML DTD.
>
> Feed it through rubyexml and get...
>
> <html xmlns:xlink="HTTP://www.w3.org/XML/XLink/0.9" xml:lang="en"
> xmlns="HTTP://www.w3.org/TR/xhtml">
> <head>
> <title>
>
> </title>
> </head>
>
> <body>
> <h1>
> The answer to life, the universe and everything is 42
> </h1>
>
> <p>
> The following image is pretty_picture.jpg
>
> <img src="pretty_picture.jpg" alt="pretty_picture"></img>
> </p>
> </body>
> </html>
>
> Just so blooming simple.
>
> And if you have a big hairy object that knows all the deeper secrets of
> life, just change rubyexml to...
>
> REXML::Document::parse_stream( REXML::SourceFactory::create_from(
> STDIN), Listener::new( BigHairyObjectThatKnowsTheDeeperSecretsOfLife.new))
>
> And you can refer to all it's instance variables and methods.
>
> It all so blooming simple!
>
>
>
> John Carter Phone : (64)(3) 358 6639
> Tait Electronics Fax : (64)(3) 359 4632
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz
> New Zealand
>
> "At first I hoped that such a technically unsound project would
> collapse but I soon realized it was doomed to success. Almost
> anything in software can be implemented, sold, and even used given
> enough determination. There is nothing a mere scientist can say that
> will stand against the flood of a hundred million dollars. But there
> is one quality that cannot be purchased in this way---and that is
> reliability. The price of reliability is the pursuit of the utmost
> simplicity. It is a price which the very rich find most hard to
> pay." -- C.A.R. Hoare in The Emperor's Old Clothes,
> Turing Award Lecture (27 October 1980)
>
>
>
>


--
David Mitchell
Software Engineer
Telogis


John Carter

7/8/2005 1:04:00 AM

0

james_b

7/8/2005 1:26:00 AM

0

John Carter wrote:
...

> So add REXML and I present a very small script I call rubyexml. Ruby
...

>
> So take a chunk of XHTML...
>
> <?xml version="1.0"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
> "xhtml11.dtd" >
...

>
> <body>
> <h1>
> The answer to life, the universe and everything is <!-- 44 - 2 -->
> </h1>
>

Question: If your document has instructions for processing, why not use
processing instructions? Why munge the semantics of the comments syntax?

Maybe take a look at how Nitro does this.


--

http://www.ru... - The Ruby Documentation Site
http://www.r... - News, Articles, and Listings for Ruby & XML
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys


David Mitchell

7/8/2005 2:32:00 AM

0

Hey,

John Carter wrote:
>> I really like the idea but I would want it to be this flexible.
>
>
> Given flexible or simple, I chose simple.
> ..
> Perhaps flexible and simple is possible. I wanted it to be able to
> validate as vanilla XHTML.
>

Ok, I don't think the example I posted falls outside the bounds of
simple. Maybe it makes your script a little more complicated but not the
syntax that the end user must use. I suspect if you hold off the actual
evaluation of the comments until the page is output then you might find
this task simpler. That is, build a single ruby script in memory that
contains the logic for outputting the page, then just eval it at the end.

--
David Mitchell
Software Engineer
Telogis


John Carter

7/8/2005 3:51:00 AM

0

John Carter

7/8/2005 4:07:00 AM

0

David Mitchell

7/8/2005 5:13:00 AM

0

I hate to break this to you John, but the link you posted is to a blank
wiki page. Perhaps you got the wrong link? The page
http://www.rubygarden.org/ruby... doesn't exist.

Yes, you end up with a hairy state machine, but it doesn't mean you lose
the one-line StreamParser.

Cheers

David

John Carter wrote:
> On Fri, 8 Jul 2005, David Mitchell wrote:
>
>> Ok, I don't think the example I posted falls outside the bounds of
>> simple. Maybe it makes your script a little more complicated but not
>> the syntax that the end user must use. I suspect if you hold off the
>> actual evaluation of the comments until the page is output then you
>> might find this task simpler.
>
>
> I guess where I started was I wanted to loop on the rows of a table.
>
> <table>
> <!-- (0..5).each { |i| -->
> <tr>
> <td>
> #{i} - <!-- @name[i] -->
> </td>
> <td>
> <img src="#{i}.jpg" alt="#{i}"/>
> </td>
> </tr>
> <!-- } -->
> </table>
>
>
> But then I'm holding all kind of state and what happens if I want to
> iterate over the columns of the table as well? (Nested loops.)
>
>
> <table>
> <!-- (0..5).each { |i| -->
> <tr>
> <td>
> #{i} - <!-- @name[i] -->
> </td>
> <!-- (0..6).each do |j| -->
> <td>
> #{i*10 + j}
> </td>
> <!-- end -->
> </tr>
> <!-- } -->
> </table>
>
>
> I'm then operating a fairly hairy state machine in my Listener class or
> I'm no longer using the one (longish) line StreamParser.
>
> However, if anyone gets the urge to embellish what I have done, as I
> say, that's what Wiki's are for.
>
>
>
>
>
> John Carter Phone : (64)(3) 358 6639
> Tait Electronics Fax : (64)(3) 359 4632
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz
> New Zealand
>
> Carter's Clarification of Murphy's Law.
>
> "Things only ever go right so that they may go more spectacularly wrong
> later."
>
>> From this principle, all of life and physics may be deduced.
>
>
>
>


--
David Mitchell
Software Engineer
Telogis


John Carter

7/8/2005 5:40:00 AM

0

Dominik Bathon

7/8/2005 11:11:00 AM

0

On Fri, 08 Jul 2005 07:13:17 +0200, David Mitchell
<david.mitchell@telogis.com> wrote:

> I hate to break this to you John, but the link you posted is to a blank
> wiki page. Perhaps you got the wrong link? The page
> http://www.rubygarden.org/ruby... doesn't exist.

You have probably been caught by the RubyGarden tarpit:

http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...


Daniel Brockman

7/9/2005 1:05:00 AM

0