Posts Tagged ‘php’

20091117 Delicatessen v2

Let me introduce Delicatessen v2, the new version of my WordPress plug-in for finding out who’s bookmarked your posts in delicious.com

It has taken a bit too long to produce this version, but it at last behaves like a nice netizen, and won’t spam delicious.com with too many queries in a very little time, therefore you won’t get banned ;)

Delicatessen

More info and downloads in the Delicatessen page.

20091102 SyHi: my minimalistic syntax highlighting plug-in for WordPress

I had been using Code Snippet for a year, or a couple of years (I can’t really remember), and was more or less happy with it. Apart from the fact that it didn’t preserve some stuff properly, like double dashes and quotes. So then I tried adding another plug-in to the mix, Preserve Code Formatting. But both at the same time didn’t work out as I expected hoped.

The solution?

I made a new plug-in. It’s very minimalistic, starting with its name: SyHi.

How does it work?

The problem with other plug-ins is that they apply their formatting and then let WordPress continue modifying the posts’ text. That’s a totally bad idea, and that’s why quotes and dashes were systematically altered. On the other hand, this little clever plug-in takes the code blocks apart, sets them aside while leaving placeholder text where they were, and when WordPress has finished with all its filtering and texturizing, SyHi replaces the placeholder texts with the preserved, syntax highlighted beatiful pieces of code that the author entered.

The resulting code not only is nice to look at, but can be copied and pasted into a compiler and it will work with no modifications at all. No more please replace em dashes with double hyphens because WordPress modified the snippet’s output…: your blog readers can now simply copy and paste!

Alphatesters needed

So far I have tested SyHi with a test blog and this blog too. It looks fine, but I would be more than happy if somebody else wants to have a go at testing it. I would even add you to the Thanks section!

Unfortunately, the plug-in is not yet available in the wp-plugins directory; I’m waiting for them to approve my request so that you can install it from within the plugins admin page.

Plug-in page at WordPress directory: SyHi. You can also clone the SyHi github repository or download the latest version from there.

Samples

So you don’t believe me when I say this plug-in works great?

Using Python

noise_output = wave.open('noise.wav', 'w')
noise_output.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))

for i in range(0, SAMPLE_LEN):
        value = random.randint(-32767, 32767)
        packed_value = struct.pack('h', value)
        noise_output.writeframes(packed_value)
        noise_output.writeframes(packed_value)

noise_output.close()

What about php?

class SyHi
{
        protected $code_blocks;
        protected $geshi_instance = null;

        public function __construct()
        {
                // Execute pre and post process functions before and after each post content is processed
                add_filter('the_content', array(&$this, 'pre_process'), 2);
                add_filter('the_content', array(&$this, 'post_process'), 1000);

                // Same for each comment
                add_filter('comment_text', array(&$this, 'pre_process'), 2);
                add_filter('comment_text', array(&$this, 'post_process'), 1000);

                // Add the css stylesheet link to the <head>
                add_action('wp_head', array(&$this, 'add_css'), 1);
        }
}

OK and what about C/C++?

void luisita_reportErrors(lua_State *L, int st)
{
    if (st != 0)
    {
        std::cerr << "ERROR -- " << lua_tostring(L, -1) << std::endl;
        lua_pop(L, 1); // removes error message
    }
}

As you can see, double dashes are being respected, quotes are kept as they are, and code is nicely highlighted. And if you’re still feeling skeptical, keep browsing posts in this blog, since all code snippets go through SyHi.

Update: added the link to the plug-in’s repository :)

Update 2: let me clarify something: this plug-in can beautify snippets in pretty much every programming language you can think of, thanks to the huge language support provided by GeSHi. I have shown you only three examples as a simple demonstration, but you can even show Z80 assembler code!

The full list of supported languages is in the left column of the GeSHi website, under the Supported Languages header. Heck, it even has support for a language called BrainFuck!

20090710 “Blue Tuesday” sources released

Blue Tuesday by xplsv

Blue Tuesday” is a direct evolution from the codecolors code base. Since it was all done in a hurry, there were lots of things which didn’t work as expected. I somehow got rid of some of them when I ported the demo to mac, then I got rid of some extra things these days when I made the demo work in Linux, prior to releasing these sources. I am not happy with this code; with so many modifications it has grown way too much to be still readable. There are effects whose behaviour is not predictable, things like the ribbons are very inefficient and so on.

I’m still not completely happy with the synchronization. In the first version trace did a simple flash app in which he tapped a key each time an event happened (e.g. a snare hit) and then generated a list from there. But due to the way the demo is structured, I had to split the list in three parts so that I could check whether an event happened in each effect. Also with so many changes in the code, the synch had gone slightly awry and when I managed to compile the code in Linux, it clearly was asking for a revision.

So I thought about using Audacity for re-recording the synchronization. I would store each event as a label (Audacity has something called Label Tracks) and then exporting those labels as txt files and processing them to convert them into a .h file with all the events in an array. I had the idea of opening Audacity’s project with a text editor and found out that it was a simple XML file — which would make even easier the task of importing the labels’ information, without having to go through the step of exporting to a text file and parsing it.

I then tried to process the XML file with Python, but it was a horrible experience. Since it has a namespace on it and I wanted to do a simple XPath search, it seems I was doomed to fail without installing a couple of Python libraries — which I didn’t want to install. My main premise is to make my demos simple to compile, and having to install an extension just for parsing XML is not what I consider “simple”. So I resorted to good old PHP’s XML functions and in 20 minutes the import script was done. That was the easy part.

What was really painful was recording the synch points! Audacity took hanging itself up as a hobby. Thankfully, I have the CTRL+S tic –in which whenever I’m working with any program I tend to press CTRL+S regularly, pretty much each time I have typed in something, just in case– and that prevented me from losing my changes more than once. But it was just annoying to have to kill the program, open it again, say yes to “yes please recover the project I was working on when you decided to crash”. Even worse: at some point, the project got corrupted, and when I reopened it again, Audacity just got stuck switching between an sleep/idle status. I had to create a new project with the same mp3, save it and then edit the new project in a text editor, and copy and paste the old label tracks from the initial source project. Luckily it was all XML. I can’t imagine what would have I done if it had been binary data! (Probably scream a good lot!)

So lesson learnt: for making good synchronization you need a dedicated, and probably integrated, piece of software. If I need to do something like this again, I will probably spend a good amount of time in preparing something like the editor blackpawn created. Because it wasn’t only a problem of crashing, it was also the problem of not having a comfortable interface which lets you go back and forth the song, reduce its speed, etc, without having to use the mouse.

This is probably one of the reasons why I prefer to make demos with my own music: it’s easier to access the original song source files and build a list of events from there if I wanted to :-)

Enough chattering, go watch the demo or get a nice headache just by looking at the code :P

20090509 WordPress mistery solved!

Whenever I looked at source code from WordPress there was something that always irked me, and it was their recurrent use of this style of comparisons:

if(2 == $x)

I have been using the totally opposite style from the very first time I typed a comparison into a computer… and that was eons ago, so I can safely assume and proclaim that I have assimilated it as an instinctive act. I mean, when I think of comparison, this is what I think of:

if($x == 2)

And so I couldn’t understand WP’s style. It didn’t make sense… until I found this little paragraph semi-hidden at the very end of their Coding Standards document:

Another important point in the above example, when doing logical comparisons always put the variable on the right side, like above. If you forget an equal sign it’ll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it’s worth it.

So that was it!

But although that now makes sense — I have lost count of the amount of times I have been asked why does my code not work? it looks fine just to quickly find the reason was they were using an assignment instead of a comparison, I am not going to follow that guideline. It just doesn’t feel right, it looks awkward to me, unnatural and overengineered. Besides, I never make those mistakes, of course ;)

Coincidentally, I was testing Netbeans with PHP yesterday. It has a feature which warns you if your comparisons have unintentional side effects, which is the very same problem WP developers are trying to prevent with that coding rule. Maybe the solution is not to change the code style but to use tools like Netbeans.

20090123 Open interval version number

Now this is something funny! I wanted to check the version of GD in my development machine. As I was right in the middle of php coding, I just did a quick print_r(gd_info()). And what did I get?

Array
(
    [GD Version] => 2.0 or higher
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 1
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XPM Support] =>
    [XBM Support] =>
    [JIS-mapped Japanese Font Support] =>
)

hmmm yeah, 2.0 or higher, very useful information! Specially considering it’s been in 2.x for years now!

Had to go to Synaptic to exactly determine which version is installed. But I wonder if there’s another way of finding out the version without having access to the system’s package manager, for example when one is in a shared host and all that?

If anybody knows, please post a comment :-)