Formatting code in WordPress

What a pain in the rear! WordPress appears nice as a blogging tool, but my attempts to format some code snippets was painful beyond belief.

Every time I pasted some code in and surrounded it with the code tags, the generated HTML had extra paragraph markers and line breaks, and screwed up the position of the closing code marker.

Even a search for plugins did not cure things. I installed one or two “code formatting” plugins that made things a little better, but still required a lot of post-paste editing of code to get anything like presentable.

It turns out that the Rich Text Editor in WordPress 2 is to blame – it completely messes up. So my advice is to disable this from your user profile.

The thing that rankles is that the rich editor is enabled by default. I did not even know it was optional until I did a Google on code formatting in WordPress. It all just seemed much much harder than it should.

Anyway, hopefully this snippet will save others the problem in the future.

Useful CSV reader

To aid my unit testing I needed to populate a database with a load of test data. This data needs to be generated by some of the less technical guys on the project and so exporting CSV (Comma Separated Values) files from Excel seemed a sensible approach. So I then cast around for any existing code that would save me having to write a robust CSV parser. The search turned up this on CodeProject:

LumenWorks.Framework

The neat thing about it is that you can get field values either by index position or column name, assuming you put column headers in your file. The column name feature really helps out with maintainance of the test data as we evolve and vary the schema slightly – we can insert columns with no worries about upsetting existing reader code.
It’s easy to use and install in your own project, and the CodeProject article describes the basic usage pattern. At the moment it is just linked in to the unit test framework, but is so useful I will probably make it a part of the core application libary to facilitate importing data from customers as we anticipate some of our customers could have thousands of records to import when they move to our system.