I am coming in half way through a thread, apologies if I am saying something that has already been said or is not relevant.
On Sun, 2011-02-06 at 22:59 +0100, Rainer Schuetze wrote: > Tomek Sowiński wrote: > > auto xml = xmlWriter(outputRange); > > > > xml.comment(books.length, " favorite books of mine."); > > foreach (book; books) { > > xml.book("year", book.year, { > > foreach (author; book.authors) { > > xml.tight.authorName({ > > xml.first(author.first); > > xml.middle(author.middle); > > xml.last(author.last); > > }); > > } > > xml.tight.title(book.title); > > }); > > } This looks to be heading down the road Groovy trod 6 years ago with the MarkupBuilder, indeed the whole builders concept. Validation that Groovy's builders framework is a good idea is that Ruby took up the idea wholesale and Python is starting to as well. It seems the idea may fly in D as well even though it is a very different form of meta-object protocol (MOP). Basically Groovy (, Ruby, and Python) allow you to get rid of the xml. in the above code and it makes the function calls and closures work much better as a DSL for describing the markup. This relies on a MOP of course since it relies on the function despatch being redefinable. > This looks nice and compact Using opDispatch to specify the tag (I guess > that is what you are using to create a tag "book" by calling xml.book()) > feels like misusing opDispatch, though. Does it add readability in > contrast to passing the tag as a string to some function? Experience from Groovy, Ruby and Python is a strong yes. Having the tag name as the name of the function with attributes as keyword parameters, string content as an unnamed parameter and nested tag content in a closure leads to beautiful HTML, XHTML, XML, . . . production. well-formedness is guaranteed, though you can still generate invalid documents. Groovy's MarkupBuilder makes for very nice computation of webpages. Here is a real example of generating a part of my website: def writer = new StringWriter ( ) ( new MarkupBuilder ( writer ) ).html { head { 'meta' ( 'http-equiv' : 'Content-Type' , content : 'text/html; charset=UTF-8' ) title ( 'Dr Russel Winder — A Short Biography' ) link ( rel : 'stylesheet' , href : 'style.css' , type : 'text/css' ) } body { div ( id : 'main' ) h1 ( 'Concertant Articles by Russel Winder' ) ul { evaluate ( new File ( 'concertantArticles.groovy' ) ).each { item -> li ( "${ extractPageTitle ( ( new File ( System.properties.'user.home' + concertantWebpagesSourceDirectory + '/Articles/' + it em[0] ) ).text ) }, ${item[2][0..9]}, " ) { a ( href : "http://www.concertant.com/Articles/${item[0]}" , item[1] ) } } } h1 ( 'Articles about SC08 for Concertant by Russel Winder' ) ul { evaluate ( new File ( 'concertantSupercomputing2008Articles.groovy' ) ).each { item -> li ( "${ extractPageTitle ( ( new File ( System.properties.'user.home' + concertantWebpagesSourceDirectory + '/Supercomputing2 008Articles/' + item[0] ) ).text ) }, ${item[2][0..9]}, " ) { a ( href : "http://www.concertant.com/Supercomputing2008Articles/${item[0]}", item[1] ) } } } } } > How do you write a tag named "tight"? Or a tag calculated at runtime? > > Something more conventional would be > > xml.tag("book", attr("year", book.year), { ... > > but I'm not sure that pairing the attribute name and value adds > readability or mere noise. I don't see this being anything like as useful as what Groovy et al. already has via the MarkupBuilder. "Conventional" is not really the way to go for this you need the full DSL approach. At least in my opinion which I think is becoming the norm in the dynamic programming language arena. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@russel.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
signature.asc
Description: This is a digitally signed message part