@madpilot makes

WordPress 2.0 mod: Insert highlighted code.

I have complained before about not being able to insert source code into WordPress when using the TinyMCE Rich Text Editor. Sure, I could switch to plain text mode, but frankly I would prefer an RTE as it makes entering posts much quicker. There is a TinyMCE plugin called insertcode written by by Maxime Lardenois which does exactly what I wanted. In fact, it even uses the GeSHI cde highlighter class (written in PHP) to do code highlighting. There is something like 50 different languages which are supported – Very cool.

Unfortunately, installation was not quite as simple as I hoped. Here is what I had to do (Substitute [blog_root] with the absolute path to your wordpress install):

  1. Untar the GeSHI tarball and copy the geshi.php files and geshi directory to [blog_root]/wp-includes
  2. Unrar (yes rar) the insertcode RAR file to [blog_root]/wp-includes/js/tinymce/plugins/insertcode (Create the insertcode directory first)
  3. THe RAR file I go didn’t have a directory structure – I don’t know whether I just messed up or what, but I had to create the following directories: config, css, images, jscripts, langs, webservice
  4. Copy config.php into the config directory, insertcode.css into the css directory, insertcode.gif into the images directory, insertcode.js into the jscripts directory all the php files into the webservice directory and all of the language files (e.g. en.js) into the langs directory. You should only have editor_plugin.js editor_plugin_src.js and insertcode.htm files in the insertcode dir.
  5. Open get_highlighted_code.php and change the $geshi->set_header_type(GESHI_HEADER_PRE) to $geshi->set_header_type(GESHI_HEADER_DIV)
  6. Open [blog_root]/wp-includes/js/tinymce/tiny_mce.gzip.php and find the line $plugins = apply_filters – add ‘insertcode’ to the array
  7. Still in tiny_mce.gzip.php add ‘insertcode’ to the line that starts with $mce_buttons = apply_filters
  8. Finally add the following to lines to the initArray javascript array:

content_css : “/[blog_url_path]/wp-includes/js/tinymce/plugins/insertcode/css/insertcode.css”,

encoding : “xhtml”,

This should add a new button to your toolbar with the letter “C” into. Pressing the button should pop a dialog box in which you can cut and paste code.

To make it look nice on the front end cut and paste the contents of insertcode.css into you themes’ style.css file:

That SHOULD be it.

<li class="li1">
  <div class="de1">
    [echo</span>](http://www.php.net/echo) print_message<span class="br0">(</span><span class="br0">)</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <span class="kw2">function</span> print_message<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <span class="kw1">return</span> <span class="st0">&#8220;Hello World!&#8221;</span>;
  </div>
</li>

<li class="li1">
  <div class="de1">
    <span class="br0">}</span>
  </div>
</li>

<li class="li1">
  <div class="de1">
    <span class="kw2">?></span>
  </div>
</li>

You will notice that the indentation gets stripped – this is (I think) a WordPress thing. I’ll post a solution once I figure one out :) Also, it can be a little tempermental – you have been warned. And as always: YMMV.

Edit: I’ve fixed the indent bug: check out this post.

3rd Degree e-news site publishes it’s first edition

The Edith Cowan University journalism students have launched the first edition of 3rd Degree – an online e-news site. The site was designed by Paul Bui and was developed by me in CakePHP.

The site allows the 3rd year students to understand the pressure of publishing a weekly news publication, with different teams controlling different parts of the process. If you would like to receive the weekly newsletter, you can register here.

3rd Degree is the brainchild of Kayt Davies, who is the lecturer in the unit.

New port80 event announced.

Port80 – the Australian Web Industry, of which I’m the membership officer has announced the next event – Ideas3.

The event features John Allsop from Sydney, who is a directory of Westciv, creators of Stylemaster; and Mark Boulton from the UK who is a noted typographer and designer who is currently working at the BBC.

For those of you who aren’t in Perth for the event on April 11, we will be posting the podcasts and photos after the event.

As an aside, the new Port80 site, which drives the event and memeber management systems was written by me in CakePHP.

Interested in starting a Port80 group in your local area? We are currently preparing an information pack. Port80 started as a way for web designers and developers in Perth could catch up in a casual, informal environment – usually at a pub. It is amazing how well it works – it’s a great way to meet other like minded people. You can read a better history on the Port80 website. Don’t forget to check out the port80 forums too!

Adding automatic JavaScipt validators to CakePHP

I’m reposting the validators.php file that I uploaded in my previous post, but this time adding a function that will automatically generate a javascript function called validate() that returns an array of error strings. This is made possible because CakePHP’s validation model uses regular expressions.

NOTE: This function has been written using my modified validator class. Please read my previous post to see what it is all about.

The strings and expressions are identical to the ones you get by callling $this->modelName->validates() in a controller, so you can avoid a round trip to the server in many cases.

To use, drop the validators.php into your app helpers directory (as before), and call the function $validators->javascriptErrors(‘modelName’); where modelName is the name of the model you want to pull the validation data from. For those of you playing at home, you may notice that the $modelNames variable gets converted to an array – this is because you can actually supply an array of modelNames if you need to validate the inputs from multiple models. Why? I had a need for it in a project, which I might go through later :)

Now, this isn’t quite complete, because it only returns an array of strings, you still need to output the string somehow. I usually output the strings to div, as I hate JavaScript alerts. The code for which is here. In that file I have defined a function called validateForm(form); which updates the div and returns false on an error, so you can use it in the onSubmit handler for the form.

Improved validation using CakePHP

I quite like CakePHP – in fact, I find it rather difficult to do site without it now. It makes everything so much easier, especially the ActiveModel classes. However, I have found the validation model a little lacking. The way it is currently implemented only allows one validation expression for each database column, and you need to re-write the validation message on each page the model is used on. This is quite restrictive and error prone – if you need to add a field to the database, you have to update more than one page.

I have managed to modify the way the validators work, so that you can define a message and have multiple validation expressions for each field.

I used the modifications on the AccountancyAge Careers Fair by modifying the model.php file that lives in the Cake library directory and it worked really well, but I wanted to set it up so that I didn’t have to modify these files everytime – It is really inconvienient to have to re-make the changes when updating the library. This is expecially a problem at the moment, because there seems to be a new release candidate every other day (Which is great – can’t wait for version 1!)

Anyway, after a little digging, I found that it is possible to override the app_model.php file in the same way as you can override the default app_controller.php file. By placing a copy of the file in the app/ directory, Cake will use this file instead.

The modification

The modification as it stands overrides the invalidFields function of model.php (this is based on RC3 – RC4 should work ok as well). You can download the new app_model.php here.

To define validation rules, you add an associative array with the paramaters expression and message. This is probably best explained using an example. Say you have a required field called “post_code” that must not be empty and must be 4 digits, you could do the following:

var $validate = array(

‘post_code’ => array(

array(‘expression’ => VALID_NOT_EMPTY, ‘message’ => ‘You have not entered a post code’),

array(‘expression’ => ‘/d{4}/’, ‘message’ => ‘A postcode must be four digits’)

)

);

Easy as! There is one last thing though. To make it easier to display the messages, I have created the following user helper called validators.php (Drop it in to app/views/helpers) (Download validators.php here)

To use, you put the function < ?php echo $validators->tagErrorMessages() ?> (Don’t forget to include Validators in your helper array) which will generate a block message wrapped in div tags with the class “message”. You can customise this file if you don’t like the way it is displayed.

Happy baking :)

Review: Siemans SpeedStream 6520 ADSL2+ Modem Router

With my move to my new house/office, and with Amnet offering static IPs on their ADSL 2 plans it has become time to retire my trusty Alcatel ADSL modem and WRT54G modem/router combination. I went through this process about a year ago when I blew the power supply on my WRT54G and was not impressed with the quality of many of the routers out there. Instead, I modified another power supply and got a UPS (I learnt my lesson)

Anyway, my luck hasn’t changed. Amnet offer a Siemans SpeedStream 6520 with their accounts, so I thought I would give it a go. As far as I can tell, this modem/router is only being offered by Telstra and Amnet as a package and as a result, you can’t purchase it from a retail store.

Problems:

  • The router doesn’t seem to handle persistent connections well. If I open an SSH session, it will often drop out after a period of inactivity – using the PuTTY “keep-alive” feature fixes the problem, so it isn’t the quality of my connection. The same occurs with my Outlook IMAP connection. Very annoying.
  • It doesn’t support port forwarding beyond DMZ – not a huge problem, but I liked this feature of the WRT54G
  • For some weird reason, my VoIP software – eyeBeam, can’t connect through the firewall. It’s little brother, X-Lite works fine, but eye-Beam is so much better I’m quited annoyed at this. Note this may not be a problem with the router, but it worked fine with the WRT54G.

I’ve asked for a refund for it and have ordered a Linksys WAG54Gv2 which is the combination big brother of the WRT54G. Hopefully Linksys is on other winner with this product. I’ll let you know.

Flogging a dead horse – technologies that don’t belong on the web

The web has come a long way since it’s inception. No longer is it a mismash of ugly looking static pages, posted by scientists. Ecommerce has gone nuts, allowing people to perform online banking and shopping. The advent of blogging has allowed anyone with a net connection to become an author and post their opinions on stuff. Web 2.0 social websites have brought the net back to the people – all in all a pretty exciting time.

However! There are some technologies that seem to be gaining steam because that is what everyone expected the web to do two years ago. The reason that these technologies will fail is because they are trying to force a paradigm where it doesn’t fit. This technique was fine a couple of years ago, but now that the web has started to find it’s feet, the incompatibility is becoming clearer.

One that immediately comes to mind is video-blogging (vblogs). Ever since people realised you could download a video off the net, on-demand video has been touted as the next big thing. Unfortunately, I don’t think it will happen. Why? It is an old medium. It is still one-way. You can’t interact with the video – you are forced to sit and watch and listen to the story. You can’t skim view it, nor can you do other stuff while watching it (You can almost get away with listening to it and doing something else, in which case, it would make more sense to listen to an audio podcast). Not to mention the speed issues – broadband still hasn’t gotten fast enough to really make it work. We young and part of the now generation – we don’t want to wait for for a video download only to find out it is crap.

Another is online newspapers. Sure, these have been around for a while but they have really missed the mark on the web. What is the point of setting up a newspaper to look and feel like it’s real-world counterpart? If I wanted to read my news like that, I would go and buy the paper. The way slashdot distributes it’s news is much closer to the mark. Give me tidbits from every story. That way I can make up my mind straight away if I want to continue reading. Trying to “flick” through a paper online in a traditional sense is too much effort.

What other technologies can you think that are being pushed, but don’t really belong?

Network issues yesterday

Apologies for those wanting to read my blog yesterday – my service provider managed to kill most of their users connection for about 12 hours :(

But I’m back online now which is the main thing…

Web 2.0 frameworks – fundamental or fluff?

Over at Port 80, we recently added a Ruby on Rails forum. This was met with both excitement and doubt from forum members. There were those that were excited about a new framework that promises to reduce development time by taking the tedium out of development. There were those who were doubtful about a system that has had a lot of hype, but hasn’t had the market infiltration to match it. Many people think that anyting that rises so quickly will only fall just as quickly. Will this happen to rails? No idea – only time can give us the answer.

However, I think there is an underlying factor here that has been over looked. The idea of frameworks designed specifically to speed up web development is what we should be getting excited about.

Web development, and to a degree, software development has changed. Customers are more than ever expecting cheap software that is good. It doesn’t make sense for us as developers to waste our precious time re-implementing the same parts of a system for different jobs. Introduction of frameworks that do this simply can only be a good thing.

One hurdle for many professional coders is the lead-time required for learning a new framework. Be the very definition, frameworks expect the coder to do things in a certain way. This may put many coders off, because they have to spend time learning, not coding and this costs money. What they may not realise, though, is that every hour they spend re-coding form validators instead of learning a framework that does it for them, adds up over time.

The old adage of work smarter not harder is extremely relevent here. There will never be more hours in a day. Trying to use all of them is impossible. Trying to improve productivity with out changes in thinking won’t work. We as web programmers know how this web thing works. The designers of these frameworks know how this web thing works. We really should be working together to work smarter.

Hmmm, CakePHP

As many of you would know, I’m playing with Ruby on Rails at the moment. I am really liking the MVC model that is presents, however, my biggest gripe about it, is that there is very little support hosting wise. Pixelbox over here in Perth is hosting it, but that doesn’t really help me for those clients that don’t want to migrate. I also feel a little loathed to move clients sites to a new technology as the support is not there yet.

Shane over at Bam sent me a link to CakePHP which looks really interesting. It started as a port of rails for PHP, but they are starting to evolve into a different beast. I’ve been playing with it for the last couple of days and I must say I like it. It is still a little rough around the edges, but it hasn’t hit beta yet, so give it time. The documentation is also a little sparse, but it’ll catch up.

I really like the fact that it runs on both PHP4 and PHP5. I think it may be something I will use a lot more once it becomes stable.

I’m planning on doing the Port80 membership system in it.

Previous Next