#!/usr/bin/perl

# hotexplode --  a program for "exploding" a xmosaic hotlist or Netscape 
# bookmark file into a hierarchial multi-page structure.
# acb  60 Chs 3162

# revision history:
# v1.0:   1-3-1996:	initial version

$date = `date`;

# customise below

# header:  some arbitrary HTML text which is appended below the title and 
#          above the hotlist data

$header = <<FOO;
<hr width="50%">
<blockquote>
This hotlist was generated with 
<a href="http://www.zikzak.net/~acb/hacks/hotexplode.html">hotexplode</a>
on $date.
<p>
<b>WARNING:</b>  The inclusion of a link to a page on 
this hotlist is not an indication of the maintainer's 
approval of or agreement with its content.
</blockquote>
<hr width="50%">
<blockquote>
Please <b>DO NOT</b> bookmark this page. Bookmark the
<a href="http://phd.pp.ru/Bookmarks/">main page</A> instead.
Any other page in the hierarchy can disappear at any time.
</blockquote>
FOO

$footer = <<FOO;
<hr>
FOO

# which directory shall contain the hotlist?

$outdir = "hotlist";


#  end of customisable portion

require "getopts.pl";

&Getopts("o:t:v");

$outdir = $opt_o if $opt_o;


# seek forward to the title
while (<>) {
  if (/<TITLE>([^\<\>]*)<\/TITLE>/) {
    $title = $1;
    last;
  }
}

$title = $opt_t if $opt_t;

# seek forward to the start of the list


while (<>) {
  if(/<UL>/) { warn "Detected xmosaic hotlist format\n" if $opt_v; 
               &parse_mosaic_hotlist($outdir, $title); last; }
  if(/<DL>/) { warn "Detected Netscape bookmark format\n" if $opt_v;
               &parse_netscape_bookmarks($outdir, $title); last; }
}

# parse an xmosaic hotlist
# exit when we meet a </UL>
# arguments:  pathname of directory in which output is to be placed,
#             title

sub parse_mosaic_hotlist {
  # we write the file at the very end, because (I think) filehandles do
  # not have local scope, and this is recursive
  local($prefix, $title) = @_;
  local($result) = "<HTML><HEAD><TITLE>$title </TITLE></HEAD>\
<BODY>\n<CENTER><H1>$title </H1></CENTER>\n $header \n<hr>\n<ul>";

  warn "Creating $prefix...\n" if $opt_v;
  
  # create the directory, if needed
  mkdir($prefix, 0755) unless -d $prefix;

  while (<>) {
    last if (/<\/UL>/);

    if(/<LI> *<A HREF=\"([^\"]*)\"[^\>]*>([^\<]*)<\/A>/) {
      #
      #  A URL
      #
      local($url,$name) = ($1, $2);
      $result = $result."<li><a href=\"$url\">$name </a>\n";
      next;
    }
    if(/<LI> (.*)$/) {
      #
      #  we've got a live one here...
      #
      local($subtitle)=local($filename)=$1;
      $filename =~ tr/0-9A-Za-z//cd;
      $filename =~ tr/A-Z/a-z/;
      <>;  # eat the "<UL>" line.
      $result .= "<li><b><a href=\"${filename}/index.html\">${subtitle}</a></b>\n";
      &parse_mosaic_hotlist("${prefix}/${filename}", "${title}:${subtitle}");
      next;
    }
    
  }

  $result = $result . $footer . "</body></html>";
  # write it to a file
  open(FILE, ">${prefix}/index.html");
  print FILE $result;
  close(FILE);
}

# parse a Netscape bookmarks list
# exit when we meet a </DL>
# arguments:  pathname of directory in which output is to be placed,
#             subtitle

sub parse_netscape_bookmarks {
  # we write the file at the very end, because (I think) filehandles do
  # not have local scope, and this is recursive
  local($prefix, $title) = @_;
  local($result) = "<HTML><HEAD><TITLE>$title </TITLE></HEAD>\
<BODY>\n<CENTER><H1>$title </H1></CENTER>\n $header \n<hr>\n<dl>";

  warn "Creating $prefix...\n" if $opt_v;
  
  # create the directory, if needed
  mkdir($prefix, 0755) unless -d $prefix;

  while (<>) {
    last if (/<\/DL>/);
    if (/<DT><H3[^\>]*>([^\<]*)<\/H3>/) {
      #
      #  a nested list
      #
      local($subtitle)=$1;
      local($filename)=$1; 
      $filename =~ tr/0-9A-Za-z//cd;
      $filename =~ tr/A-Z/a-z/;
      # parse the description here
      local($desc)="";
      while(<>) {
        last if (/<DL>/);
        $desc = $desc . $_;
      }
      $result = $result . "<dt><b><a href=\"${filename}/index.html\">${subtitle}</a></b>\n";
      unless("$desc" eq "")  { $result = $result . $desc; }
      &parse_netscape_bookmarks("${prefix}/${filename}", 
        "${title}:${subtitle}");
      next;
    }
    if (/<DT><A HREF=\"([^\"]*)\"[^\>]*>([^\<]*)<\/A>/) {
      #
      #  A URL
      #
      local($url, $name) = ($1, $2);
      $result = $result."<dt><a href=\"$url\">$name </a>\n";
      next;
    }
    $result = $result . $_;
  }
  $result = $result . $footer . "</body></html>";
  # write it to a file
  open(FILE, ">${prefix}/index.html");
  print FILE $result;
  close(FILE);
}


