#!/usr/bin/perl -w $| = 1; #===================================================================== # create_learning_page # # Creates a "Learning" page - Categories and subcategories of links to # resources, in order of appearance in the xml file. Main categories # get links on the top, and are in big highlight color headings. # Subcategories are h2 headers, resources are ul lists. #===================================================================== use XML::Parser; my $AKI_HOME = $ENV{'AKI_HOME'}; # Do the usual intro stuff, get input file. (@ARGV == 1) || die <<"-30-"; Usage: create_learning_page output file will be named "learning.html" You need to move that to "/docs/learning.html (and then to tcp)" -30- my $input_file = shift; # Set up various page variables. Content is stored in an array. # Have an array of categories, each category is a hashref with keys # name, anchor, and subcats. Subcats is an arrayref, in the subcats # array are hashrefs - each subcat is a hashref, with keys name, anchor, # and refs. Refs is another array ref, containing refs - each ref is # a hashref, with keys href, name, and comments. Whew. my $content = []; my $current_container; my $current_category; my $current_subcat; my $current_reference; my $DEBUG = 0; # Create a parser object. my $parser = new XML::Parser (Handlers => {Start => \&start, End => \&end, Char => \&char}, ErrorContext => 2); # Parsing the file should set some variables, but not print anything. $parser->parsefile($input_file); # Once it's all parsed, THEN we print. open(OUT,"> learning.html") || die "Error: Cannot open output file: $!\n"; # Print the beginning of the page, including the link to the CSS file. # This contains the section (with menu javascript and link to the # CSS file), and the title logo. open(FILE,"< $AKI_HOME/lj/static/learning.header") || die "Error: Can't open static page parts! $!\n"; while () { print OUT; } close(FILE); # Print a skeleton of links to main categories. print OUT <<"-30-";
This page contains various references and information about subjects I'm learning or using, in the following categories:
-30- # Print the full nested categories, in order: foreach $cat (@$content) { $anchor = $cat->{'anchor'}; $name = $cat->{'name'}; print OUT "\n"; print OUT "
$name
\n"; print OUT "
\n"; } # Print the end of the page. open(FILE,"< $AKI_HOME/lj/static/common.footer") || die "Error: Can't open static footer! $!\n"; while () { print OUT; } close(FILE); #======================= HANDLER ROUTINES ================================= sub start { my $expat = shift; my $element = shift; my %attr = @_; print "Calling start on |$element|\n" if $DEBUG; if ($element eq "page") { return; #Do nothing. } #MINK: All getting overwritten? need my in here? elsif ($element eq "category") { # A new main category. Make new one, put current category at this. # Push this new category into the main content array. $current_category = {}; $current_category->{'name'} = $attr{'name'}; $current_category->{'anchor'} = $attr{'anchor'}; $current_category->{'subcat_list'} = []; print "Category: name = ", $current_category->{'name'}, " anchor = ",$current_category->{'anchor'},"\n" if $DEBUG; push(@$content,$current_category); } elsif ($element eq "subcategory") { # A new subcat. Make new one, put current_subcat at this. # Push this new subcat into the current category's subcat_list. $current_subcat = {}; $current_subcat->{'name'} = $attr{'name'}; $current_subcat->{'anchor'} = $attr{'anchor'}; $current_subcat->{'resource_list'} = []; print "Subcategory: name = ", $current_subcat->{'name'}, " anchor = ",$current_subcat->{'anchor'},"\n" if $DEBUG; push(@{$current_category->{'subcat_list'}},$current_subcat) if $current_category; } elsif ($element eq "resource") { # A new resource. Make new one, put current_resource at this. # Push this new resource into the current subcat's resource_list. $current_resource = {}; $current_resource->{'name'} = $attr{'name'}; $current_resource->{'href'} = $attr{'href'}; $current_resource->{'desc'} = []; push(@{$current_subcat->{'resource_list'}},$current_resource) if $current_subcat; } else { # Default for all other HTML. # Dump it, tags and all, into the current resource's desc area. # The ONLY time we'll see any html is in a desc on a resource. my $tag = "<$element"; my ($attr,$val); foreach $key (keys %attr) { $value = $attr{$key}; $tag .= " $key=\"$value\""; } $tag .= ">"; push(@{$current_resource->{'desc'}},$tag) if $current_resource; } } sub char { # The ONLY time we care to save text is in a desc section. my $expat = shift; my $content = shift; push(@{$current_resource->{'desc'}},$content) if $current_resource; } sub end { my $expat = shift; my $element = shift; print "Calling start on |$element|\n" if $DEBUG; if ($element eq "page") { return; # Do nothing. } elsif ($element eq "category") { $current_category = ''; } elsif ($element eq "subcategory") { $current_subcat = ''; } elsif ($element eq "resource") { $current_resource = ''; } else { # Default for all other HTML. # Dump it, tags and all, into the current resource's desc area. # The ONLY time we'll see any html is in a desc on a resource. my $tag = ""; push(@{$current_resource->{'desc'}},$tag) if $current_resource; } }