Archive for the ‘Programming’ Category

Late night session

Saturday, November 12th, 2005

There’s nothing like a late night programming/writing session with plenty of candy, Dr Pepper, and good music. Ok, so technically it’s not late night yet, but when you’re used to going to bed at 10 pm it is. Only thing that could make it better is a batch of Mountain Dew…

… which we have!!!

International Characters in Textile 2 (Improved)

Wednesday, May 18th, 2005

Recently, I received a comment on the Super Archives about the lack of support for international characters in the implementation. Being a dane I should of course have caught this, but I guess I write too little of my native language.

The comment, however, tipped me off to a problem with both the live preview and the Textile 2 (Improved) plugin. The problem with the live preview was in the Javascript and has been corrected by Jeff Minard, while I haven’t been able to contact Adam Gessaman who’s responsible for the textile plugin (for some reason I can’t leave comments on his blog).

Anyway, for the rest of you who also use the Textile 2 (Improved) plugin I have here included the corrections needed for international character support (this also includes characters like ‘ and – ).

Changes

Change line 2721 from


$html = htmlentities($html);

to


$html = htmlentities($html, ENT_NOQUOTES, $this->options['char_encoding']);

This makes sure the character encoding is taken into account when converting characters to their relevant HTML entities. The default character encoding for the plugin is utf-8 (see below).

Optionally, change line 37 from


$textile->options['char_encoding'] = 'utf-8';

to


$textile->options['char_encoding'] = get_bloginfo('charset');

This is only necessary if you use a character encoding on your blog which is different from utf-8 (not likely, but could happen).

Help needed with the javascript event model in Internet Explorer

Monday, May 9th, 2005

My Super Archive plugin is quite dependent on javascript and uses XMLHttpRequests to fetch the lists that make up the live archive. For each item in the list of years and list of months a function is attached, to run when the item is clicked. In Mozilla based browsers and Safari this is done using the W3C event model and the function addEventListener(), in Internet Explorer I use the attachEvent() functions. So far so good. To find out which item was clicked I rely on the current event being passed into the attached function. This is appearantly part of the W3C event model, and doesn’t work in Internet Explorer, and that is where I’m stumped.

Question: How do I figure out which item was clicked in Internet Explorer, for several items having onclick events attached using attachEvent()?

Super Archives Plugin for WordPress

Sunday, May 8th, 2005

Comments have been closed as the plugin has not been updated almost forever. I would refer people to the Extended Live Archive which is the Super Archives with extra features.

As I mentioned earlier I had problems with javascripts interfering with each other while loading. While I sorted out my problems with regards to the javascript, the reason why I was playing around with javascripts has already been leaked, and by none other than Michael Heilemann of Binary Bonsai. Since this is inspired by his ideas I guess it’s only fair.

Introduction overwith, I hereby give you: The Engineered Boulderer’s Super Archives Plugin to WordPress

For a demonstration, head over to Michael’s archives page, I am going to put it on my archives page, but it requires a minor redesign before I do.

IMPORTANT The javascript functions in my script have been tested, however, if there are other function set to run when the body loads, and they have problems then the super archive Will Not Run. The process of debugging javascripts is arduous work as they simply fail silently, because of this I do not have time to perform technical support on other people’s javascripts. That said, if Michael could get Super Archives to run on the B with the combination of javascripts he uses, then you can too.

I have included some documentation below, and in the plugin package.

Changelog

Version 1.6.2: Fixed a bug where draft posts in a month would make it show up in the list of months, thanks for the heads up Matthew. Requires a rebuild of the cache (edit a post or post a comment to rebuild).

Version 1.6.1: Fixed a regression which would prevent the php script from reading the settings and cache.

Introduced a more robust Javascript method of attaching events to the loading of the window.

Version 1.6: Fetches the character set from the blog to show international characters correctly.

New functionality for truncating post titles, see documentation below or with the plugin.

Version 1.5.2: Added a missing " in the description. Check the result from each use of document.getElementById() so as to not call null objects.

Version 1.5.1: Put a more stringent check on the FAT object for the Fade Anything Technique.

Version 1.5: Fixed bug where moderated comments were counted. Also, made sure cache was updated for new comments, and post deletions, and pingbacks, etc.

Updated cache structure so the cache can update (for example due to a new comment) while people are browsing the archives without requiring a reload (the plugin should automatically update your cache if you are using an old version).

Included new functionality, see the documentation included below or with the plugin.

Version 1.2: Implements support for Internet Explorer. Many thanks to Chris Boulton for his solution which worked wonders.

Version 1.1: Fixed a bug in the javascript (simple typo, oops).

System Requirements

  • Wordpress 1.5 or later
  • PHP 4.3.0 or later
(more…)

Problems with the (Real) Live Textile Formatting

Saturday, May 7th, 2005

As I mentioned earlier I implemented the (Real) Live Textile Formatting (RLTF) to enable comment previews. I thought all was honky-dory until I tried working with some hefty javascript which was also initialized by the onload function of the body. My javascript initialize function was set to be called after the RLTF initialization, and it wouldn’t get called. A day and a half of debugging and I found the error which was in the javascript for the RLTF.

The RLTF is initialized by the liveReqInit function. In this function the “key down” (or whatever browser dependent) event is assigned to the “search field” using the addEventListener function. The problem occurs on a page where the search field doesn’t exist. The element is found using getElementById which returns null when the element doesn’t exist on the page, and the addEventListener is therefore called on a null object causing the javascript to crash. The crash means the chain of functions for the body onload will abort.

One solution is to check that the search field exists on the page. I do this by inserting the following code at the beginning of the liveReqInit:


	var searchElement = document.getElementById(searchFieldId);
	if( searchElement == null ) return;

Similarly, we should check that the result field exist by inserting


	var resultElement = document.getElementById(resultFieldId);
	if( resultElement == null ) return;

before if(emptyString == '') { near the end of the function.

I hope my headaches will prevent some for others.

Word Count Plugin for WordPress

Friday, May 6th, 2005

Inspired by Chris’ discovery of odd behavior in the wp-word-count plugin I set out to create a version which would not be cheated by occurrences of the word “img” in an entry. I am a coder and web-developer myself and could see myself writing snippets of HTML in my posts.

The solution I found was to run the relevant filters on the content, after I get it back from the database. Using the function apply_filters I was left with a chunk of real HTML. I could strip the tags of this HTML to count the words, and search the HTML for occurences of the <img ... /> tag by searching for the string “<img”.

So there you have it: The Engineered Boulderer’s Word Count Plugin.

Changelog

Version 1.1: Does not count wordpress included smilies as images. Added an argument to allow the plugin to count smilies as images.

Version 1.2: Corrected a bug in the implementation of version 1.1

Documentation

The template function the plugin provides is teb_word_count(display) which accepts two arguments.

display
(boolean) sets whether the function should display the number of words and images, or if it should return them.

  • true (default)
  • false

count_smilies
(boolean) set whether smilies should be counted as images. If false, the plugin will simply disregard smilies (neither count them as text, nor images).

  • true
  • false (default)

Disclaimer: This plugin has been tested to work on my test blog and here, but it could break in real life. If you see any problems with my implementation be sure to let me know.

Live Comment Preview

Thursday, May 5th, 2005

I have now implemented live comment preview on the comment form. The preview is the same (Real) Live Textile Preview as seen on Binary Bonsai and described in one of Joen’s amazing articles.

The (Real) Live Textile Preview is not a plugin such as several of the other preview solutions, rather it is a number of javascripts and php scripts. The javascripts attach a function to the “key pressed” event of the comment field. Every time a key is pressed the text is passed to the php scripts through the magic of XMLHttpRequest (or AJAX) and returned as HTML. This returned batch of HTML is then shown to the user in the preview box.

This solution is a little more complex that the pure javascript or even simpler WordPress plugins. However, it has the clear advantage of being correct Textile. One simple example is links, if you write a link as the last part of a sentence, the period will in the javascript solutions be counted as part of the link, the (Real) Live Textile Preview shows this correctly. Having used Textile since I started using MovableType, this is a big plus for me.

As Joen pointed out, however, there are also disadvantages to this solution. Each keypress has the potential of sending a request to you server, if many people write comments at the same time, this can become a real burden. To be realistic though, the number of comments I receive are very small and the added load to the server will probably only serve to get it a bit more in shape.

I’ll run with it as is for now, try out my comments and tell me what you think.

Category Count plugin for WordPress

Wednesday, May 4th, 2005

It’s official I have made my first WordPress plugin. As far as plugins go, however, it’s quite meager. The plugin contains one functionality, it counts the number of categories in which there are published posts. You can see it in action in the sidebar of my archives.

The template function the plugin provides is teb_category_count(display) which accepts a single argument.

display
(boolean) sets whether the function should display the number of categories, or if it should return it.

  • true (default)
  • false

Disclaimer: This plugin has been tested to work on my test blog and here, but it could break in real life. If you see any problems with my implementation be sure to let me know.

What Should I Do?

Friday, March 4th, 2005

I was looking through my files on my work computer, and found this little “gem”. The application What Should I Do? is an example of a decision maker application. The main application window contains a text field surrounded by the text “Should I” and “?” as shown below

The user, you, writes a question in the text field and clicks the “ask” button. Through the powers of randomization the answer is provided.

As you might have imagined, the answer to the question in the picture was yes.

Included in the dmg file is the program itself, a small “read me” file, and the source code for the program. The program requires Mac OS X.

Link: What Should I Do?