Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR Incorporated.

Katrin and Jim

Month List

Parsing bookmarks and images

by Jim Dec 13, 2005 9:54 AM

I uploaded my Firefox bookmarks.html file to the website, so that I could access it from anywhere; I have about a million of them, and it's useful to bring them up at work. The file isn't very user-friendly in its raw form.

So I decided to parse the file, extract the icons from it (they're in there) and add some script to expand and collapse the folders. I could get a little practice with regular expressions along the way.

// Rip the icon data out and replace with images
// I match to the end of the anchor tag so I can use the length
// to put the image at the end of the tag.
Regex regex = new Regex("ICON=\"data:image/x-icon;base64,(.*?)\".*?\\>");
i = 0;
icon = 1;
Match match = regex.Match(file, i);
while (match != null && match.Length != 0)
{
  Debug.WriteLine(match.Value);
  iconname = ExtractIcon(match.Groups[1].Value, dest, icon);
  file = file.Insert(match.Index + match.Length, string.Format(
    "<img src=\"{0}\" border=\"0\" width=16 height=16>&nbsp;", iconname));
  icon++;
  i = match.Index + match.Length;
  match = null;
  if (i < file.Length)
  {
    match = regex.Match(file, i);
  }
}

The image is in the .ico format, so it can be streamed right into a file with that extension:

private static string ExtractIcon(string data, string folder, int index)
{
  int i;
  string filename;
  // Open a binary file for writing
  filename = string.Format("bookmark_icon_{0}.ico", index);
  BinaryWriter bw = new BinaryWriter(
    File.Open(Path.Combine(folder, filename), FileMode.Create));
  bw.Write(Convert.FromBase64String(data));
  bw.Flush();
  bw.Close();
  return filename;
}

Piece of cake. The javascript for showing and hiding the DIVs is pretty straightforward; a little more regex does the trick. Full code for console application here.

In Firefox, image tags can contain data in base64 format. This isn't supported in IE6, but I've tested it in IE7 beta, and that does support it. Unfortunately, I won't have that at work until it's officially released.

Tags: ,

Blog Software

by Jim Dec 08, 2005 12:41 PM

Cat pointed out to me that my comment count is not updating on my blog(s). It hadn't occurred to me to think about it, but it was immediately obvious that the blog file itself is static; and if it's not hosted on blogger, then it has no way of knowing a comment was added. Shoot.

So I embarked on a quest to get my own blog software. I'm a .NET developer, and the choices are slim. There are about a zillion free PHP packages out there, but for .NET there's DasBlog and dotText. The former is way more than I want to maintain, and having Brinkster as my server makes the whole operation trickier. dotText hasn't been updated in two years, but if I'm going to install and modify my own, I think I'll use that as a jumping-off point.

I almost went with haloscan for the comments, because I'm perfectly happy with the Blogger interface. But then I realized that my trackback was going to point to haloscan's site always and forever, making an eventual port of the blog impossible.

I don't have the time or inclination to write a blog from scratch, but I think I can do a comment system, and my research has given me some nifty ideas. Stay tuned.

Tags: