Home: Project: Bookmark Converter
Code:
#!/g3/fsb/share/synario/bin/perl/perl

###############################################
# File:    bmc
# Author:  Kanting Tsai
#          kanting@email.com
# Purpose: bookmark converter
# Date:    1/19/2000
###############################################
# History:
# 10/20/99 fix https not being processed
# 10/10/99 take off $argc = @_, because
#          of failing to detect arguments
# 1/4/00   tr slash into bar to avoid
#          invalid file naming
# 1/14/00  use newgetopt library
# 7/25/00  finished i2n part
###############################################

$opt_ie = "/user/ktsai/.microsoft/Favorites";
$opt_ns = "/user/ktsai/.netscape/bookmarks.html";
$usage = "soc/user/ktsai/bin, 10/1/1999
Usage: bmc [options]
Options:
  -n2i              Perform a Netscape-to-IE conversion
  -i2n [title]      Perform an IE-to-Netscape conversion, and
                    specify the title for the bookmark
  -ie <path>        Specify Internet Explorer Favorites path;
                    default is \"~/.microsoft/Favorites\"
  -ns <path\/file>   Specify Netscape bookmark file;
                    default is \"~/.netscape/bookmarks.html\"\n";

$bookmark_title = "Bookmarks Converted by bmc";
$bookmark_desc = 
"<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is generated by bmc, a perl script written
by Kanting Tsai, kanting\@email.com -->\n
";
$url_file_content =
"[DEFAULT]
BASEURL=%s\n
[InternetShortcut]
URL=%s\n
";

require "/mercury/users/ktsai/bin/newgetopt.pl";
NGetOpt ("ie=s", "ns=s", "i2n:s", "n2i:s");

if ((defined $opt_n2i and defined $opt_i2n) 
or (not defined $opt_n2i and not defined $opt_i2n)) {
  print $usage; exit;
} elsif (defined $opt_n2i) {                               ## netscape-to-ie
  print "Converting ...\n";
  chdir $opt_ie;
  open IN, "<$opt_ns";
  populate_n2i ("nil", "  ");
  close IN;
  print "\"$opt_ie\" done.\n";
} else {                                                   ## ie-to-netscape
  unless ($opt_i2n eq '') {$bookmark_title = $opt_i2n;}
  print "Converting \"$bookmark_title\" ...\n";
  open OUT, ">$opt_ns";
  print OUT $bookmark_desc;
  print OUT "<TITLE>$bookmark_title<\/TITLE>\n";
  print OUT "<H1>$bookmark_title<\/H1>\n\n<DL><p>\n";
  populate_i2n ($opt_ie, "  ");
  close OUT;
  print "\"$opt_ns\" done.\n";
}

sub populate_i2n ($$) {
  my $folder = shift;
  my $spaces = shift;
  my @dirlist;                  ## critical
  chdir $folder;
  @dirlist = glob "*";
  foreach $entry (@dirlist) {
    if (-d $entry) {                                              ## if entry is a directory
      $foldername = $entry;
      $foldername =~ s/^Links$/Personal Toolbar Folder/;
      print OUT "$spaces<DT><H3 FOLDED>$foldername<\/H3>\n$spaces<DL><p>\n";
      print $spaces."explore $entry ...\n";
      populate_i2n ($entry, $spaces."  ");
    } elsif (($entry =~ /.url$/) and (-f $entry)) {               ## something .url and existed
      $entry =~ s/.url$//;
      open URLFILE, "<$entry.url";
      while (<URLFILE>) {if (/BASEURL=(.*)/) {print OUT "$spaces<DT><A HREF=\"$1\">$entry</A>\n";}}
      close URLFILE;
    }
  }
  chop ($spaces);
  chop ($spaces);
  print OUT "$spaces</DL><p>\n";
  chdir "..";
}

sub populate_n2i ($$) {
  my $state = shift;
  my $spaces = shift;
  while (<IN>) {
    if ($state eq "nil") {
      if (/<DL>/) {$state = "start";}
    } elsif ($state eq "start") {
      if (/<\/DL>/) {
        chdir "..";
        last;
      } elsif (/<DT><A HREF=\"(https?:\/\/.+?)\".*>(.+)<\/A>/) {    # a link
        $linkhttp = $1;
        $linkname = $2;
        $linkname =~ s/&amp[;]/&/;
        $linkname =~ s/\//|/;
        open URL, ">$linkname.url";
        printf URL $url_file_content, $linkhttp, $linkhttp;
        close URL;
      } elsif (/<DT><H\d.*>(.+)<\/H\d>/) {                 # a folder
        $foldername = $1;
        $foldername =~ s/^Personal Toolbar Folder/Links/;
        print $spaces, "populate $foldername ...\n";
        rmdir $foldername;
        mkdir $foldername, 0700;
        chdir $foldername;
        populate_n2i ("nil", $spaces."  ");
      }
    }
  }
}

Example:
% bmc -n2i
Converting ...
  populate Linux ...
    populate Download ...
    populate Distribution ...
      populate NASDAQ Listed ...
  populate Windows ...
    populate Crack ...
      populate Reboot ...
        populate Hang ...
"/user/ktsai/.microsoft/Favorites" done.
%