MarkupBuilder in Smalltalk

Borrowing a leaf from Groovy..

[please read comments: for changes made and my addl replies to critical notes in pharo mailing list.. accepting many as good suggestions to improve the framework for general benefit..]

newBldr := XMLBuilder new.
newBldr
xmlPreprocessor; attributes: #(‘a=”6″‘ ‘b=”7″‘); test: ‘welcome to builders’ with: [

newBldr attributes: #(‘a=”1″‘ ‘b=”2″‘); trial: ”; attributes: #(‘a=”3″‘ ‘b=”4″‘); trial: ‘testing..’ ;trial ].

or: without going through DNU and array of assocs for attributes:

newBldr xmlPreprocessor; attributes: (Array with: #a->6 with: ‘b’->7); tag: ‘test’ text: ‘welcome to builders’ with: [

newBldr attributes: #(‘a=”1″‘ ‘b=”2″‘); tag: ‘trial’ text: ”; attributes: #(‘a=”3″‘ ‘b=”4″‘); tag: ‘trial’ text: ‘testing..’ ;tag: ‘trial’ ].

newBldr valueStringTrimmed

to get :

‘<?xml version=”1.0″ ?><test a=”6″  b=”7″ >welcome to builders<trial a=”1″  b=”2″ ></trial><trial a=”3″  b=”4″ >testing..</trial><trial></trial></test>’

has its charms to generate the tons of export XML, or SGML in general.. and remember all calls to methods #trial: and #test: are dynamically resolved as they do not exist..! and now extend this to a PropertyBean..

aSimpleBean := PropertiesBean new beanAlias: ‘NewXML’.
aSimpleBean text: ‘TopRoot’.
(aSimpleBean elementProperties) add: #root -> 123; add: #child1 -> 123.

aSimpleBean valueStringTrimmed generates:

‘<?xml version=”1.0″ ?><NewXML>TopRoot<root>123</root><child1>123</child1></NewXML>’

The idea gets a little more interesting.. as we can even cause the same PropertyBean to generate an ini thorugh IniBuilder, json through JSONBuilder, etc..

Specialize the builder with more constructs for say HtmlBuilder now..:

html := HtmlBuilder new.
html html: ” with: [ html
attributes: #(‘onLoad=”1″‘); body: ” with: [ html h1: ‘text’ ] ].

html valueStringTrimmed generates:

‘<html><body onLoad=”1″ ><h1>text</h1></body></html>’

*****

html body: ” with: [  html htmlTableTag tableAttributes: #(‘width=”80%”‘);
headers: #(‘ColumnField01’ ‘ColumnField02’)
rowsArray: #(#(123 32) #(223 32) #(‘asd’ ‘asd’)) ].

can now generate a whole table…!.. just given the header and rows..of any rational size

add plugins to this for href rows on are after each row.. pagination, reading a csv file of data to generate a table.. or db table.. on the fly.. the possibilities are many

********

Having built up the basic framework now more as an internal trial.. moving from XML outputs to now, I am working on plugging this to Seaside to see if we can use it more potently for renderContentOn: html methods to render easier and extensible html components with JSF like plugin capability..

Wondering if this has extended use for the Pharo.. Smalltalk community to reuse.. the entire capability of a MarkupBuilder system that has:

* Construction of a markup text ( possibly non text..) that can be extended to output to many possible tree based format..: XML, JSON, INI, Properties, etc..

* PropertiesBean that extends the ability to create a class/ instances that can emit at last stage the reqd one/ multiple output formats..

* HtmlBuilder that extends and enables faster html turnaround especially with potential extensible components of Tables, Forms, media/ browser plugin players etc.. Rails like easier access to dump multiple js, css files and little more assist here n there..

* A generic plugin mechanism that can allow per builder instance registry of plugin initialized and respond to..

Just my two cents.. love to see someone comment on this here.. can post the monticello package for anyone to explore if reqd..

Pick the mcz package from the link.. (http://squeaksource.com/PharoGoodies/MarkupBuilder-Skrish.alpha.1.17.mcz).

Advertisements

One Response to MarkupBuilder in Smalltalk

  1. skrishnamachari says:

    Many a good happens in accepting the criticism from all around and making the best out of it..and here it goes:

    * Main changes is to api.. attributes: with: tag: instead of mbAttr:, cNode: and nodeTag:..

    “Lukas: The approach with #doesNotUnderstand: absolutely doesn’t scale.Tools break in subtle ways (editors, debuggers, program checkers).”

    * doesNotUnderstand: is a mode for those interested in using the shorter syntax it does give.. and by the by I find the debugging clean and neat using “Through” on the debugger..till you need to dive in to the particular node handled.. yes its a pain accepting method as declared when it does not exist.. will create a nuisance in method finder polluting it with methods that actually do not exist..etc..

    The alternate tag:’tag’ text:’text’ with:[..] does exist for the prim and proper.. just that the other reads so well in markups..

    jaayer:”namespace prefixes”

    this most certainly will be handled in the next commit..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: