Archive for April, 2009

LUA: Classes and Instanciations

Monday, April 27th, 2009

If, like me, you tend to be a bottom up learner you usually never read the manuals except when you need to. And this is, for once, one of these times. Only reading the class parts of the manuals/wikis (IE: http://lua-users.org/wiki/SimpleLuaClasses and http://www.lua.org/pil/16.1.html ) is actually not really enough to be able to make classes always work as you want them to.
If you also tend to adapt examples to your requirements (no need to reinvent the wheel), here it might actually go wrong. In the examples given in the two previous link, the writers tended to forget about the differences between metatables and tables completely confounding me :) .
In the example below:

function Account:new (o)
o = o or {} — create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end

self equals Account and the metatable of Account is also Account which creates an infinite loop which needs to be treated if you try to save / dump the instanced variables.
To solve this, use instead the following syntax:

function Account:new (o)
o = o or {} — create object if user does not provide one
setmetatable(o, {__index = self})
return o
end

When you retrieve the saved variables don’t forget to set the metatables to what they should be too :)

And if I am totally wrong in what I just posted, I ll be glad to be corrected. I really do have a hard time with the Lua OOP model, guess I still have lots to read / learn.

WordPress Xslt Processor plugin – follow up

Saturday, April 18th, 2009

So, a user of the plugin asked me on how to make the plugin also have parameters for the xsl file. And I ended up coding it because it seemed simpler that way.

This this post servers as much as a test bed as a presentation of the different kind of options one can use:

So lets start with the most basic way to use this plugin:

Use:

[XmlProcessor wp-content/plugins/CPT_XslProcessor/example]

Result

[XmlProcessor wp-content/plugins/CPT_XslProcessor/example]

or:

[XmlProcessor xml=”wp-content/plugins/CPT_XslProcessor/example.xml” xslt=”wp-content/plugins/CPT_XslProcessor/example.xsl”]

Result:

[XmlProcessor xml="wp-content/plugins/CPT_XslProcessor/example.xml" xslt="wp-content/plugins/CPT_XslProcessor/example.xsl"]

Now, lets say you want to add options to the xsl file:

Use:

[XmlProcessor wp-content/plugins/CPT_XslProcessor/example params=”default_param=this is the replaced default parameter”]

Result:

[XmlProcessor wp-content/plugins/CPT_XslProcessor/example params="default_param=this is the replaced default parameter"]

or:

[XmlProcessor xml=”xml_filepath” xslt=”xslt_filepath” params=”default_param=this is the replaced default parameter”]

Result:

[XmlProcessor xml="wp-content/plugins/CPT_XslProcessor/example.xml" xslt="wp-content/plugins/CPT_XslProcessor/example.xsl" params="default_param=this is the replaced default parameter"]

And that should actually be working now :)

LUA and overloading

Tuesday, April 7th, 2009

I am coding some mod in LUA for some game and I hit a snag and it actually took me quite some time to solve it due to several functionalities not available through LUA to control some of the interface elements and thus transformed the snag into lots of small snags to get what I wanted to work.

But the last snag I hit, after clearing the others, was really annoying, in the API there was a function like: removeSmthg(ID) but also removeSmthg(NAME). And being unfamiliar with the language as always I forgot that LUA doesn’t handle type-based function overloading, so only one of the function actually worked… the one I was not using. Once I found that out, it ended up being quite easy to actually do what I wanted at the start.

And because it is blogged, I should remember it next time!

WordPress Xslt Processor plugin development

Sunday, April 5th, 2009

Need

If any of you actually read my about me page, you know that this blog ended up being more than foreseen. Thus I required to have more liberty in some of the things i wanted to do.

Typically you may have a resume in a pdf or document format. How do I include such a document inside the theme of WordPress ? How do I include content from a webservice / an xml document in WordPress?

To answer part of this need, I made a request on the wordpress forum to no avail and I ended up coding it. Fortunately the WordPress API is very well made though the documentation is a bit lacking in some places. In WordPress you can have:

First I tried using filters but due to the encoding and escaping done by wordpress my regular expressions would not work. I loaded my prefered IRC client and connected to the #wordpress channel on freenode. Lots of help and advice there and information too. Taking their advice into account I used Shortcode which actually remove all the requirements for parsing the code yourself. WordPress provides the Shortcode API for that.

What about the Plugin ?

The plugin that I am now using takes an XML and XSLT files, process it and output to the window. For an example of how to use it see my “resume” pages.

If you want to use this plugin, you can find the latest version on WordPress: XslProcessor

(At the time of this note, the code is not yet uploaded. Approval from WordPress has not yet been received)

Hope you find it as usefull as I do.