#! /usr/bin/perl -w # jrju.pl (The Spider) # Hypertext Notebook Weaver # Version 0.9 (10/24/00) # Richard Rathe # Copyright 1997-2000 by Richard Rathe # Free for non-commercial use! ### localization stuff ######################################################## # specify your operating system $os = 'Mac'; # Mac, DOS or Unix # specify HTML file extension $html = '.html'; # html (best!) or htm # specify creator for Mac files $browser = 'MSIE'; # MSIE (Explorer) or MOSS (Netscape) # turn debugging on or off $debug = 'off'; # on or off ############################################################################### ### special data ### $vers = '0.9'; $htxinfo = ""; $geninfo = ""; $dtdinfo = ""; $body = ""; $date = &date_stamp; $time = &time_stamp; ### start program ### chomp($path = `pwd`); # save current path $] > 5.0 || die "Must use Perl5! ($])\n"; print "\nStarting Jrju v$vers ($os) on $date at $time...\n\n"; while (@ARGV) { # drag and drop dir(s) on Mac droplet # or from command line under DOS, Unix $dir = shift(@ARGV); # $os =~ /dos/i and $dir .= "\\"; $n{'Table of Contents'}[0] = 'index' . $html; $n{'Table of Contents'}[1] = ""; $n{'Table of Contents'}[2] = "i"; if (-d $dir) { print "directory: $dir\n"; chdir("$dir") || die "Can't change directory $dir!\n"; &read_title; &make_index; &make_pages; $debug =~ /on/i and &dump_info; chdir("$path") || die "Can't change directory $path!\n"; print "\n"; } } print "Done!\n\n"; $os =~ /mac/i and MacPerl::Quit(1); ### subroutines ### sub read_title { my $infile = 'title.ttx'; if (-e $infile) { open (INFILE,$infile) || die "Can't open input file $infile!\n"; chomp($cover = ); chomp($author = ); chomp($email = ); chomp($home = ); chomp($url = ); chomp($text = ); close (INFILE); } !$cover and $cover = 'Untitled'; # defaults !$author and $author = 'Anonymous'; !$email and $email = 'nobody@nowhere.com'; !$home and $home = 'Homeless'; !$url and $url = ''; !$text and $text = 'No title.ttx file!'; $email =~ s/(\w+@[\w.]*\w)/mailto:$1/i; } sub make_index { my ($item, @list); @list = <*>; foreach $item (@list) { if (-f $item) { &index_file($item); } elsif (-d $item) { unshift (@ARGV, $item); } } &output_index; } sub make_pages { my ($item, @list); @list = <*>; foreach $item (@list) { if (-f $item) { &read_links($item); } } @list = <*>; foreach $item (@list) { if (-f $item) { &read_file($item); } } } sub read_links { my $file = $_[0]; my $infile = $file; my ($title, $text, $key) = ('','',''); if ($file =~ /(\w*)\.jtx/i) { open (INFILE,$infile) || die "Can't open input file $infile!\n"; # print " linking: $infile\n"; while () { last if ($_ eq "\n"); return if ($_ =~ /mailto:/i or $_ =~ /ftp:/i); return if ($_ =~ /http:/i or $_ =~ /\.htm/i); return if ($_ =~ /\.gif/i or $_ =~ /\.jpg/i); chop; $title = $_; } while () { $text .= $_; } close (INFILE); foreach $key (keys %n) { next if ($key eq $title); if ($text =~ /$key/i) { $k{$key}{$title} = ""; } } } else { # print " skipping: $infile\n"; } } sub read_file { my $file = $_[0]; my $infile = $file; my ($outfile, $title, $text) = ('','',''); my ($head, $tail, $info) = ('','',''); if ($file =~ /(\w*)\.jtx/i) { $outfile = $1 . $html; $info = ""; open (INFILE,$infile) || die "Can't open input file $infile!\n"; # print " reading: $infile\n"; while () { last if ($_ eq "\n"); return if ($_ =~ /mailto:/i or $_ =~ /ftp:/i); return if ($_ =~ /http:/i or $_ =~ /\.htm/i); return if ($_ =~ /\.gif/i or $_ =~ /\.jpg/i); chop; $title = $_; } while () { $text .= $_; } close (INFILE); $text = mark_text($text, $title); open(OUTFILE,">$outfile") || die "Can't open output file $outfile!\n"; print " writing: $outfile\n"; $head = "$dtdinfo\n\n$htxinfo\n$info\n\n\n\n"; $head .= "\n$cover - $title\n$geninfo\n\n\n"; $head .= "$body\n$cover |\n$n{$title}[1]\n"; $head .= "
\n$title\n

\n"; $tail = "
\n"; $tail .= "
\n$author ($date)\n"; $tail .= "
\n\n\n"; print OUTFILE $head; print OUTFILE $text; print OUTFILE $tail; close (OUTFILE); $os =~ /mac/i and MacPerl::SetFileInfo("$browser", "TEXT", $outfile); } else { # print " skipping: $infile\n"; } } sub mark_text { my $text = "\n" . $_[0] ."\n"; my $title = $_[1]; my ($links, $key) = ('',''); $text =~ s/\015\012/\n/g; # fix eol chars $text =~ s/\012/\n/g; $text =~ s/\015/\n/g; $text =~ s/&/&/g; # standard escapes $text =~ s//>/g; $text =~ s/\"/"/g; $text =~ s/\^\n/\n
/g; # blank line $text =~ s/\n-+/\n
\n/g; # divider line $text =~ s/(.)\n(\S)/$1 $2/g; # combine adjacent lines $text =~ s/\n{2,}(\s+)/\n$1/g; # collapse blank lines $text =~ s/\n{2,}(.+)/\n

$1<\/P>/g; # make paragraphs $text =~ s/_([^_]*)_/$1<\/I>/g; # italics $text =~ s/\*([^*]*)\*/$1<\/B>/g; # bold $text =~ s/(\w+\.gif)//gi; # gif images $text =~ s/(\w+\.jpg)//gi; # jpg images $text =~ s/

\$(.*<\/P>)/


$2/gi; # center images $text =~ s/\>(.*)<\/P>/$2
<\/P>/gi; # float right $text =~ s/\<(.*)<\/P>/$2
<\/P>/gi; # float left $text =~ s/(\w+@[\w.]*\w)/$1<\/A>/gi; # email links $text =~ s/([^#])(http:[\w\/\.#-~]*[\w\/])/$1$2<\/A>/gi; # html links $text =~ s/([^#])(ftp:[\w\/\.#-~]*[\w\/])/$1$2<\/A>/gi; # ftp links $text = &build_lists($text); foreach $key (keys %n) { next if ($key eq $title); $text =~ s/($key)/$1<\/A>/gi; } foreach $key (keys %{$k{$title}}) { next if ($text =~ /($key)/i); $links .= "$key<\/A> |\n"; } if ($links ne "") { chop($links); chop($links); chop($links); $text .= "\n


\nRelated Topics:\n$links"; } return $text; } sub index_file { my $file = $_[0]; my $infile = $file; my ($outfile, $lastkey) = ('',''); my ($lastlink, $type, $temp) = ('','',''); if ($file =~ /(\w*)\.jtx/i) { $outfile = $1 . $html; open (INFILE,$infile) || die "Can't open input file $infile!\n"; # print " indexing: $infile\n"; while () { chop; $lastkey = 'Table of Contents' if ($lastkey eq ""); if ($_ eq "") { last unless ($file =~ /links/i); $lastkey = ""; $lastlink = ""; } elsif ($_ =~ /mailto:/i) { $outfile = $_; $type = 'm'; $n{$lastkey}[0] = $outfile; $n{$lastkey}[1] = $lastlink; $n{$lastkey}[2] = $type; } elsif ($_ =~ /http:/i or $_ =~ /\.htm/i) { $outfile = $_; $type = 'l'; $n{$lastkey}[0] = $outfile; $n{$lastkey}[1] = $lastlink; $n{$lastkey}[2] = $type; } elsif ($_ =~ /\.txt/i) { $outfile = $_; $type = 'l'; $n{$lastkey}[0] = $outfile; $n{$lastkey}[1] = $lastlink; $n{$lastkey}[2] = $type; } elsif ($_ =~ /\.gif/i or $_ =~ /\.jpg/i) { $outfile = 'extras/' . $_; $type = 'p'; $n{$lastkey}[0] = $outfile; $n{$lastkey}[1] = $lastlink; $n{$lastkey}[2] = $type; } else { $temp = $_; $temp =~ s/\W//gi; $temp =~ tr/A-Z/a-z/; $temp =~ s/(^.{4}).*(.{4}$)/$1$2/gi; $n{$_}[0] = $temp . $html; $n{$_}[1] = $lastkey; $n{$_}[2] = 'i'; $type = 'n'; $l{$lastkey}{$_} = ""; $lastlink = $lastkey; $lastkey = $_; } } $n{$lastkey}[0] = $outfile; $n{$lastkey}[1] = $lastlink; $n{$lastkey}[2] = $type; close (INFILE); } else { # print " skipping: $infile\n"; } } sub output_index { my ($outfile, $head, $tail) = ('','',''); my ($icon, $item, $key, $info) = ('','','',''); foreach $key (sort keys %l) { $outfile = $n{$key}[0]; $info = ""; open(OUTFILE,">$outfile") || die "Can't open output file $outfile!\n"; print " writing: $outfile\n"; if ($key eq 'Table of Contents') { $head = "$dtdinfo\n\n$htxinfo\n$info\n\n\n\n"; $head .= "\n$cover - $key\n$geninfo\n\n\n"; $head .= "$body\n\n$cover\n

\n$text\n"; $head .= "
\n How to Use This Notebook
\n"; $tail = "
\n"; $tail .= " Note  \n"; $tail .= " Index  \n"; $tail .= " Link  \n"; $tail .= " Email  \n"; $tail .= " Picture\n"; $tail .= "
\n\nThis notebook was created using "; $tail .= "Jrju v$vers on $date.\n
\n"; $tail .= "$home\n
"; $tail .= "
$author
\n"; $tail .= "\n\n\n"; } else { $head = "$dtdinfo\n\n$htxinfo\n$info\n\n\n\n"; $head .= "\n$cover - $key\n$geninfo\n\n\n"; $head .= "$body\n$cover |\n$n{$key}[1]\n"; $head .= "
\n$key\n

\n
\n"; $tail = "
\n
\n"; $tail .= "
\n$author ($date)\n"; $tail .= "
\n\n\n"; } print OUTFILE $head; foreach $item (sort keys %{$l{$key}}) { if ($n{$item}[2] eq 'n') { $icon = ""; } elsif ($n{$item}[2] eq 'i') { $icon = ""; } elsif ($n{$item}[2] eq 'l') { $icon = ""; } elsif ($n{$item}[2] eq 'm') { $icon = ""; } elsif ($n{$item}[2] eq 'p') { $icon = ""; } print OUTFILE "$icon $item
\n"; } print OUTFILE $tail; close (OUTFILE); $os =~ /mac/i and MacPerl::SetFileInfo("$browser", "TEXT", $outfile); } } sub build_lists { my $text = $_[0]; my $level = 0; my ($x, $tmp, $item, $next, @stag, @etag, @itag); while ($text =~ /\n([ \t]+)(.)(.*)\n(.)/) { $item = $3; $next = $4; $x = length($1); if($2 eq "#") {$stag[$x] = "
    "; $etag[$x] = "
"; $itag[$x] = "
  • "; } elsif($2 eq "@") {$stag[$x] = "
      "; $etag[$x] = "
    "; $itag[$x] = "
  • "; } elsif($2 eq "!") {$stag[$x] = "
      "; $etag[$x] = "
    "; $itag[$x] = "
  • "; } elsif($2 eq "+") {$stag[$x] = "
    "; $etag[$x] = "
    "; $itag[$x] = "
    "; $item = "$item"; } elsif($2 eq "-") {$stag[$x] = "
    "; $etag[$x] = "
    "; $itag[$x] = "
    "; } else {$stag[$x] = "
      "; $etag[$x] = "
    "; $itag[$x] = "
  • "; $item = $2 . $3; } if ($x > $level) { # start list $tmp = ""; $level = $x; if ($next =~ /[^ \t]/) { while ($level > 0) { $tmp .= "\n$etag[$level--]"; } } $text =~ s/\n[ \t]+.*/\n$stag[$x]\n$itag[$x]$item$tmp/i; } elsif ($next =~ /[^ \t]/) { # end list $tmp = ""; while ($level > $x) { $tmp .= "\n$etag[$level--]"; } $tmp .= "\n$itag[$x]$item"; while ($level > 0) { $tmp .= "\n$etag[$level--]"; } $text =~ s/\n[ \t]+.*/$tmp/i; } elsif ($x < $level) { # end sublist $tmp = ""; while ($level > $x) { $tmp .= "\n$etag[$level--]"; } $text =~ s/\n[ \t]+.*/$tmp\n$itag[$x]$item/i; } else { # list item $text =~ s/\n[ \t]+.*/\n$itag[$x]$item/i; } } if ($level > 0) { # clean up $tmp = ""; while ($level > 0) { $tmp .= "\n$etag[$level--]"; } $text .= $tmp . "\n"; } return $text; } sub dump_info { print "\n-------------\n\n"; print " info: TITLE = $cover\n"; print " info: AUTHOR = $author\n"; print " info: EMAIL = $email\n"; print " info: HOME = $home\n"; print " info: URL = $url\n"; print " info: TEXT = $text\n"; print "\n-------------\n\n"; foreach $key (sort keys %n) { print "names: $key = $n{$key}[0] = $n{$key}[1] = $n{$key}[2]\n"; } print "\n-------------\n\n"; foreach $key (sort keys %l) { foreach $item (sort keys %{$l{$key}}) { print "lists: $key = $item = $l{$key}{$item}\n"; } } print "\n-------------\n\n"; foreach $key (sort keys %k) { foreach $item (sort keys %{$k{$key}}) { print "links: $key = $item = $k{$key}{$item}\n"; } } print "\n\n"; } ### date/time routines ### sub date_stamp { my @months = ("January","February","March","April","May","June", "July","August","September","October","November","December",); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; return "$months[$mon] $mday, $year"; } sub time_stamp { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # if ($hour < 10) { $zero = "0"; } else { $zero = ""; } # return "$months[$mon] $mday, 19$year at $zero$hour$min"; if ($hour > 11) { $mm = "PM"; } else { $mm = "AM"; } if ($hour > 12) { $hour -= 12; } if ($hour < 1) { $hour = 12; } if ($min < 10) { $min = "0" . $min; } return "$hour:$min $mm"; }