Dominic J. Thoreau

buildme.pl

~\Desktop\hackweb\buildme.pl.html

#! /perl/bin/perl

use strict;
use Net::FTP;
$|=1;

my $line; # what's my line? this is.....
my (%labels, %nocode, %ord_count, %order, %noind,%parent,%has_children);

my ($tag, $lc, $output_fn, $content_fn)=();
my ($site,$server,$s_login,$s_pass, $s_dir, $template, $upload, $delete)=();
open (INDEX,"index.xml");
my @index=<INDEX>;
close INDEX;

print "Reading data";
foreach $line (@index) {
        # get overall data
        if ($line =~/<site>(.*)<\/site>/ )      { $site=$1;     }
        if ($line =~/<server>(.*)<\/server>/ )  { $server=$1;   }
        if ($line =~/<s_login>(.*)<\/s_login>/ ){ $s_login=$1;  }
        if ($line =~/<s_pass>(.*)<\/s_pass>/ )  { $s_pass=$1;   }
        if ($line =~/<s_dir>(.*)<\/s_dir>/ )    { $s_dir=$1;    }
        if ($line =~/<template>(.*)<\/template>/){$template=$1; }
        if ($line =~/<upload>(.*)<\/upload>/)   { $upload=$1;   }
        if ($line =~/<delete>(.*)<\/delete>/)   { $delete=$1;   }
        # identify the handle of the file. Should be unique, but can do paths
        if ($line =~/<filename>(.*)<\/filename>/ ) { $tag=$1; $lc++; }
        # the text displayed on any link
        if ($line =~/<label>(.*)<\/label>/ ) {
                $labels{$tag}=$1;
                $ord_count{$tag}=$lc;
        }
        # if nocode is mentioned, *don't* generate page (ie static w/link)
        if ($line =~/<nocode>true<\/nocode/ ) { $nocode{$tag}='true'; }
        # order on menu - if you ignore it, it will mix it up
        if ($line =~/<ord>(\S*)<\/ord>/ ) {$order{$tag}=$1;}
        # noind pages generate, but don't appear in other menus
        if ($line =~/<noind>true<\/noind>/ ) {$noind{$tag}='true';}
        if ($line =~/<parent>([a-z]*)<\/parent>/ ) {
                my $dad=$1;
                $parent{$tag}=$1;
                $has_children{$dad}='true';
        }
        print '.';
}

open (TEMPL,$template);
my @templ=<TEMPL>;
close TEMPL;
my $template_cont=join('',@templ);


FILE:
print "\nWriting temporary files";
foreach $line (keys %labels) {
        unless (defined $nocode{$line}) {
                $output_fn=">".$line.".html";
                $content_fn=$line.".content";
                # if you haven't got a content file, skip the entry.
                open(CONTENT,$content_fn) || next FILE;
                my @cont_arr=<CONTENT>; close CONTENT;
                my $cont_str=join('',@cont_arr);
                my $out_cont=$template_cont;
                $out_cont=~ s/TAG/$labels{$line}/g;
                my $index_dis=&build_index("$line");
                $out_cont=~ s/INDEX LIST/$index_dis/g;
                $out_cont=~ s/SITE/$site/g;
                $out_cont=~ s/FN/$line/g;
                my $ddate=localtime(time);
                $out_cont=~ s/DATE/$ddate/g;
                $out_cont=~ s/BODY TEXT/$cont_str/g;
                open (OUT,"$output_fn"); print OUT $out_cont; close OUT;
                print '.';
        }
}


my ($ftp,$file);
my @file_exep=('TEMPLATE.html','iTunesLib.html');
my %filehash;

foreach $file (@file_exep) {
        $filehash{$file}='true';
}

opendir(DIR,'.') || die "Directory open failed: $!";
my @files = readdir DIR;
closedir DIR;

# connect and cwd
unless (lc($upload) eq 'false') {
  print "\nConnecting to remote host";
  $ftp = Net::FTP->new($server,Debug => 0) ||
        die "Connect failed to : $server";
  $ftp->login($s_login,$s_pass) ||
        die "Login failed: ".$ftp->message;
  $ftp->cwd($s_dir) || die "CWD failed: ".$ftp->message;

  print "\nPublishing ";
  foreach $file (@files) {
        if ($file =~ /html\Z/ && not defined $filehash{$file}) {
                        $ftp->put($file)|| die "Put failed ".$ftp->message;
                unless (lc($delete) eq 'false') { unlink($file); }
                print '.';
        }
  }

  $ftp->quit;
}

sub build_index {
        my $tt=shift; # used to be called $target_tag , but name was too long...
        my ($bob, $kate);
        my $outp="<UL>\n";
        foreach $bob (sort keysort keys %labels) {
            # current page, 
            if ($bob eq $tt && not defined $parent{$bob}) {
                $outp.= "<LI><b>$labels{$bob}</b></LI>";
            } elsif ($bob eq $tt && defined $parent{$bob}) {
                $outp.= "<UL><LI><b>$labels{$bob}</b></LI></UL>";
            } elsif ($bob ne $tt && not(defined $noind{$bob})&& not defined $parent{$bob}) {
                $outp.= "<LI><A href=\"$bob.html\">$labels{$bob}</A></LI>\n";
            }
            if (defined $has_children{$bob} && $bob eq $tt) {
                $outp.= "<UL>";
                foreach $kate (sort keysort keys %parent) {
                   if ($bob eq $parent{$kate}) {
                     $outp.="<LI><a href=\"$kate.html\">$labels{$kate}</A></LI>\n";
                   }
                }
                $outp.='</UL>';
           }

        }
        $outp.="</UL>\n";
}

sub keysort {$order{$a} <=> $order{$b}; }

© 2004 Dominic J. Thoreau - this is http://www.thoreau-online.net/buildme.pl.html
Updated and uploaded Fri Dec 29 11:45:07 2006