S5 with textile

I’ve started working on a presentation for skillswap. Like any paranoid coder, I want to keep my work under revision control, so I prefer a text based format. Of these, the best option presently seems to be S5. But it does require you to write a fair chunk of HTML. These days though, I prefer textile.

So, I wrote a small standalone textile processor in ruby:

#!/usr/bin/env ruby
require 'rubygems'
require_gem 'RedCloth'

This, combined with a small Makefile lets me build the S5 presentation quite simply from the textile file.

index.html: s5-head.html vc-intro-svn.txt s5-foot.html
        (cat s5-head.html; 
         ./textile vc-intro-svn.txt; 
         cat s5-foot.html) > $@

The only mildly irritating limitation is that you still have to explicitly wrap the slides in <div class='slide'>...</div>. But I can live with that.

There was still one thing missing though. If you printed out the presentation, all the links wouldn’t show up. Because textile outputs well formed xhtml, it was a simple matter to use a small bit of XSLT to harvest the links on each slide and insert a handout div listing them.

<xsl:stylesheet version="1.0"
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:template match="@* | node()">
      <xsl:apply-templates select="@* | node()"/>
  <xsl:template match="div[@class='slide']">
    <div class="slide">
      <xsl:if test=".//a">
        <div class="handout">
          <p>Links from the slide:</p>
            <xsl:for-each select=".//a">
                <xsl:value-of select="."/>
                <xsl:text>: </xsl:text>
                <a href="{@href}"><xsl:value-of select="@href"/></a>

Like all XSLT, it looks more verbose than it actually is (something it shares with Java, IMHO). But it serves a useful purpose. Now, the printed slides aren’t lacking in information.


Ruby’s Lisp Heritage

There’s an article The Ruby VALUE on O’Reillynet today. It shows a rather neat hack whereby ruby uses a few bits of an integer to specify extra information, like whether the value is nil or not.

It does mean that FixNums only go up to 230 instead of 232, but that’s not a serious limitation, because it transparently turns them into BigNums instead.

All of this reminded me of a very similiar situation in another piece of software: Emacs. Like ruby, it uses a few extra bits in the word to signify the type of an object (Integer Type).

Note that Lisp is unlike many other languages in that Lisp objects are self-typing: the primitive type of the object is implicit in the object itself. For example, if an object is a vector, nothing can treat it as a number; Lisp knows it is a vector, not a number.

But it’s not only Emacs, lisps in general have a history of doing this. This leads to the lovely notion that the data has a type as opposed to the variable, which seems to confuse C programmers so much. 🙂


Vim Syntax for Textile

I’ve been using textile more and more these days. It’s quite convenient for writing.

But what’s annoying is that there is no support for it in Vim.

So, after a bit of messing around with the vim manual Your own syntax highlighted, I now have textile.vim.

It’s definitely a first attempt at such things. There’s a lot that it doesn’t do. But for an hours work, it’s started to highlight textile files well enough for me.

Update: PlasticBoy has a similar Vim syntax for Markdown. I should take a look and get some ideas from there…


XML::Genx Compiler Warnings

Naturally, one of the first things I did when I got the new mac was to try compiling my software, XML::Genx, on it. Sadly, it throws up a number of errors.

Genx.xs: In function 'string_sender_write':
Genx.xs:209: warning: pointer targets in passing argument 3 of 'Perl_sv_catpv' differ in signedness
Genx.xs: In function 'string_sender_write_bounded':
Genx.xs:223: warning: pointer targets in passing argument 3 of 'Perl_sv_catpvn_flags' differ in signedness
Genx.xs: In function 'XS_XML__Genx_ScrubText':
Genx.xs:606: warning: pointer targets in passing argument 2 of 'genxScrubText' differ in signedness
Genx.xs:606: warning: pointer targets in passing argument 3 of 'genxScrubText' differ in signedness
Genx.c: In function 'XS_XML__Genx__Namespace_GetNamespacePrefix':
Genx.c:1068: warning: pointer targets in passing argument 3 of 'Perl_sv_setpv' differ in signedness

Looking around the web, it appears that this is a new warning in gcc 4.0. It also baffles me that that apple would have considered using signed characters for anything by default. However, the obvious fix doesn’t work.

-        sv_catpv( *svp, s );
+        sv_catpv( *svp, (signed char *)s );

At this point, I suspect that my knowledge of C is fundamentally lacking. Does anybody out there have any ideas what I need to do to fix this warning? The source is string_sender_write() in Genx.xs, although you’ll probably also want genx.h to look for the typedefs.

Sadly, the gcc manual doesn’t have much to say, except to note the existence of -Wno-pointer-sign to disable the warning. But I’d rather fix it if I can.


Typo TimeZones

One minor annoyance that I’ve noticed since I started using typo is that the times are out by about 8 hours. For instance, this post probably says “created 8 hours ago”, even though I created it only a few minutes ago. Looking at the previous post in the database, which has never been edited:

created_at     | 2006-01-24 00:29:00
updated_at     | 2006-01-24 08:52:04

The updated_at field is correct.

So now I have to figure out which of the many components is defaulting to Eastern Standard Time. Could it be Ruby? Rails? PostgreSQL? Apache? JavaScript? Safari? Who knows, I have to go to work now…

Update: It turns out that HowtoSetDefaultTimeZone has the answer. Put this into config/environment.rb:

ActiveRecord::Base.default_timezone = :utc

Last 100 lines in a file

Yesterday, I had to pick out the last 100 lines in a file, for a small log viewer I was writing. Neither the Perl FAQ nor the Perl Cookbook had quite the right answer. So, this is what I ended up with.

sub last_lines {
  my ( $fh, $howmany ) = @_;
  my ( @lines, $i );
  while ( <$fh> ) {
    $lines[ $i++ ] = $_;
    $i = 0 if $i == $howmany;
  return grep { defined }
    @lines[ $i .. $howmany - 1 ], @lines[ 0 .. $i - 1 ];

This is reasonably memory efficient, more so than the example I found which read the entire file in and then just picked the last $howmany items from the array.

The last line is interesting. We have to “swivel” the array around a partition point, in order to get the entries into the correct order. Also, we have to get rid of undefined entries. These crop up if you have less than $howmany lines in the file.


Cygwin / X

At work, I get quite a bit of use out of cygwin. In particular, I find the X server really handy. But I had been using a small batch file to start up the X server, which annoyingly left behind a DOS box whilst the X server was running.

But yesterday, I found the c:/cygwin/usr/X11R6/bin/startxwin.bat script. This manages to start up the X server without leaving behind the DOS box. Much nicer. I did have to edit the script to not start up an xterm, but apart from that it works great.

One other little feature that’s quite handy: If you create an ~/.XWinrc file with this contents:

MENU systray {
  xterm EXEC "xterm -ls -display %display%"
ROOTMENU systray

Then you have a convenient link on the X server menu to fire up an xterm. Very handy. My next step is to get it to automatically ssh over to the dev server and fire up Emacs. But I’m not sure if cygwin’s ssh client will interact with the pageant that I’m already running.


New Server

So I went ahead and bought a new Dell PowerEdge SC430, as my normal server is falling over on a far too regular basis. Lovely. Except that when I got it home, I made the discovery that Dell no longer fit PS/2 keyboard connectors. There are 7 USB slots, however. And guess which kind of keyboard I don’t posess?

On top of that, I also found that the only free-standing monitor I posess only had a DVI cable, not a VGA.

So, back to work to liberate some parts from the stores for an evening. And now I have a very nice shiny new server running FreeBSD.

Now starts the hard bit, configuring it the same as the other one…


London Weekend

We spent the weekend in London. A nice little break after the christmas period (which was fun, but it’s nice to get some time alone). The initial reason was that my wife’s work was treating everybody with tickets to the Queen musical on Saturday night. Frankly, I hate musicals and it was the low point of the weekend. I’d never realised quite how good Freddy Mercury was at singing until I heard other musical professionals murder Bohemian Rhapsody.

But anyway, that was the least interesting bit. On Saturday, we went up and dropped our bags off at the hotel (cheap but with very cheery australians running the shop) and went straight to the Natural History Museum to see the Wildlife Photographer of the Year competition results. If you get a chance, it’s really worth seeing. It really makes you realise what wondrous things a camera can actually do. It does cost £6, but it’s well worth it.

Afterwards, we stayed and wandered around the museum. We lunched in the cafe in the Earth sciences section (because it was the emptiest) and spent the rest of the afternoon meandering through the Earth sciences. Volcanoes, earthquakes, gem stones, it was all there and thoroughly enjoyable. By the time we’d finished, we had to go back to the hotel to get ready for the evening out.

Kudos to Mr Blair—making the museums free was a very good move indeed.

On Sunday morning, we dashed off to Madam Tussauds to see the waxworks. The ticket price was £23.99 per person, but thankfully, I’d picked up a 2-for-1 offer the day previously. This made the price seem a lot more reasonable… Anyway, the waxworks were superb. The first few rooms were full of “celebrities” of which I knew only a few. But the quality was superb. If you weren’t looking hard, and you turned round to see a crowd of people, you’d have a job picking out the wax one. They also had a section of world leaders, which I recognised a few more of.

The next bit was the chamber of horrors. Some of the waxworks of serial killers happened to have been replaced by actors, who were all too keen to jump out at you. Usually when you’d just walked past. Needless to say, it was a heart-stopping experience.

Finishing there, we wandered over to London Zoo (via tube, which was a mistake — we should have walked through Regents Park). Sadly, this was when the rain started up. 😦 But we went in and started meandering around the exhibits. It was a bit of a shame, because the zoo is clearly underfunded. Most of the enclosures look shabby, and the information by the enclosures isn’t very helpful. Much of it looks quite sad, instead of welcoming. The bits which are new are excellent, though. I spent ages in the B.U.G.S. section. It was visually appealing and very informative. Just a shame that the rest of the zoo can’t be more like it.

Finally, we rounded off with lunch at the zoo cafe, “Graze”. The food was excellent, if a little pricy. Big portions of good solid food. Just what you need on a rainy day.

Coming out of the zoo, we headed straight back to Victoria and on to Brighton. Sitting on the train was bliss, letting our feet relax after all the traipsing around… But definitely worth it.


Shiny New Toy

I’ve been so busy with my shiny new toy recently, that I completely forgot to blog about it. I’ve recently acquired an iMac G5. I love it. I promised myself that I wouldn’t buy another PC because they’re generally rubbish, error prone and don’t come with much good software. The iMac comes with Emacs, Vim,
Perl, and Apache. What more could you ask for?

On top of that, the display on the iMac is superb. Lovely, bright and colourful. I’ve seen lots of macs around in the Perl community and consequently I’m pretty happy with the interface. The only slight downside so far is the odd keyboard layout, which has double-quote on the right hand side of the keyboard (american style) instead of on Shift-2 like every other british keyboard. But that’s minor.

When it comes to software, I happily downloaded everything I needed pretty quickly: graphical Carbon Vim, Aquamacs, Firefox and Thunderbird. Everything worked like a charm. And it’s all incredibly nippy compared to my old laptop.

However, I was particularly pleased to see that the copy of Myst IV Revelation also supported OSX. On the same CD. Bonus! And so did World of Warcraft when we bought that a few days later. Overall, the mac is an excellent gaming machine.

I’d definitely recommend getting one of these. With a bit of luck, they’ll only get cheaper as Apple release the new Intel products later on this year.