11 Apr 2004 fxn   » (Master)

Pasting code in Advogato

Pasting code is something not to be abused here, but I think it would be normal to see it now and then in the diary of software developers more than we do.

That has a couple of inconveniences though, not a big deal, but:

  • Special characters have to be substituted with HTML entities.
  • mod_virgule doubles blank lines in pre blocks and adds a spurios leading space.
I faced that once again yesterday and said no more. I wrote a simple filter anyone can use. It does the following:
  • Escapes special characters.
  • Puts a dummy space in blank lines to avoid the bug.
  • Indents the whole block.
  • Wraps the result in a pre tag.
  • Puts the result in the clipboard on Mac OS X and Win32.
In particular, if the stuff to paste is in you editor and the program supports passing some selected text to an external program (M-| in Emacs) the code can be easily copied that way from the very source.

Of course the first application of that script has to be to blog his very listing, but since it is a bit long I'll just post a few lines:

   #!/usr/bin/perl
   
   use strict;
   use warnings;
   
   # indentation of the whole block
   my $indent = ' ' x 3;
   
   # coderef for setting the clipboard
   my $clip = $^O eq 'darwin'  ? \&clip_macosx :
              $^O eq 'MSWin32' ? \&clip_win32  :
                                 \&clip_unsupported;
   
   # ...

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!