#!/usr/bin/perl

# converts netscape's and lynx's bookmark files to a blackbox menu
# (c) Jeremy C. Reed 2000
# created 22/Mar/2000
# it is free but use at your own risk

# use by adding something like the following to your main menu:
#  [submenu] (Bookmarks) {Visit webpage...}
#    [include] (/home/reed/.blackbox/bookmarks.menu)
#  [end]       
#
# then run bookmarks_to_blackbox_menu > ~/.blackbox/bookmarks.menu      

$browser = "/usr/local/netscape/netscape -remote 'openURL(XXXURLXXX)'";
$lynx_bookmarks_file = $ENV{'HOME'} . '/lynx_bookmarks.html';
$netscape_bookmarks_file = $ENV{'HOME'} . '/.netscape/bookmarks.html';

$max_length = 30; ## characters
$separator = "[nop]";  ## comment out if you don't want it or change to:
#$separator = "[nop] (--------)";

# lynx is commented out because the bookmarks are sooo long; the bookmarks
# need to be organized into subcategories first -- another program I am
# planning to write!
#&get_lynx_bookmarks;
&get_netscape_bookmarks;

sub get_lynx_bookmarks {

  open (BOOKMARKFILE, $lynx_bookmarks_file) ||
    die ("$lynx_bookmarks_file could not be opened!");

  while ($line=<BOOKMARKFILE>) {
#<LI><a href="http://www.openssh.com/">OpenSSH</a>

    if ($line =~ /<LI><a href="(.+)">(.*)<\/a>/) {
      &output_menu_item ($1, $2);
    }

  }

  close (BOOKMARKFILE);

} # get lynx bookmarks

sub get_netscape_bookmarks {

  open (BOOKMARKFILE, $netscape_bookmarks_file) ||
    die ("$netscape_bookmarks_file could not be opened!");

  while ($line=<BOOKMARKFILE>) {

#    </DL><p>
   if ($line =~ /\s*<\/DL><p>/) {
     print "[end]\n";
   }
#    <HR>
   if ($line =~ /\s*<HR>/) {
     print "$separator\n";
   }
#<DL><p>
#    <DT><H3 ADD_DATE="951171441">Personal Toolbar Folder</H3>   
   if ($line =~ /\s*<DT><H3.*>(.*)<\/H3>/) {
     print "[submenu] ($1)\n";
   }

#    <DT><A HREF="http://home.netscape.com/bookmark/4_7/netcenter.html" ADD_DATE="951171441" LAST_VISIT="0" LAST_MODIFIED="0">Netscape.com</A>

    if ($line =~ /\s*<DT><A HREF="(.+)" ADD_DATE.*>(.*)<\/A>/) {
      &output_menu_item ($1, $2);
    }

  }

  close (BOOKMARKFILE);

} # get netscape bookmarks


sub output_menu_item {

#netscape -remote 'openURL(http://home.netscape.com)'

#  [submenu] (Bookmarks) {Visit webpage...}
#      [exec] (ReedMedia)    {/usr/local/netscape/netscape -remote 'openURL(http://www.reedmedia.net)'}
#  [end]
 
  my $command = $browser;
  my $title = $_[1];

  if (($max_length) && (length($title) > $max_length)) {
    $title = substr ($title, 0, $max_length);
  }

  $command =~ s/XXXURLXXX/$_[0]/;
  $command =~ s/\{/\\\{/g;
  $command =~ s/\}/\\\}/g;
  $title =~ s/\(/\\\(/g;
  $title =~ s/\)/\\\)/g;
 
  print "[exec] ($title)\t{$command}\n";

} # output menu item

