[Bio] / KBaseTutorials / publish_site.pl Repository:
ViewVC logotype

View of /KBaseTutorials/publish_site.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Wed Jun 13 21:09:01 2012 UTC (7 years, 5 months ago) by olson
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +91 -15 lines
Update of the tutorial publication stuff.

use strict;
use Cwd;
use Data::Dumper;
use HTML::TreeBuilder;
use File::Copy;
use File::Basename;
my $scriptdir = dirname($0);

@ARGV == 2 or die "Usage: $0 mode destination-dir\n";
my $mode = shift;
my $dest = shift;

my @valid_modes = qw(copy link show kbase);
my %valid_modes = map { $_ => 1 } @valid_modes;
if (!$valid_modes{$mode})
{
    die "mode must be one of @valid_modes\n";
}

my $tut_url = "http://kbase.us/developer-zone/tutorials";
#
# Links using API
#
#my $cdmi_url = "http://kbase.us/developer-zone/api-documentation/cdmi-api/#";
#my $cdmi_entity_url = "http://kbase.us/developer-zone/api-documentation/cdmi-entity-api/#";
#my $idserver_url = "http://kbase.us/developer-zone/api-documentation/idserver-api/#";

#
# Links using command line scripts
#
my $cdmi_url = "http://kbase.us/developer-zone/api-documentation/cdm-command-line-scripts/#";
my $cdmi_entity_url = "http://kbase.us/developer-zone/api-documentation/cdm-entity-relationship-command-scripts/#";
my $idserver_url = "http://kbase.us/developer-zone/api-documentation/id-server-commandline-scripts/#";

my $add_links_args = "'$cdmi_url' '$cdmi_entity_url' '$idserver_url' $scriptdir/../KBaseCDMI";

my %tut_dir_map;
if ($mode eq 'kbase')
{
    #
    # Read the tutorial-to-directory mapping
    #
    if (!open(MAP, "<", "$scriptdir/tutorial-dir-map"))
    {
	warn "Tutorial directory map not found in $scriptdir/tutorial-dir-map: $!";
    }
    else
    {
	while (<MAP>)
	{
	    chomp;
	    my($dir, $file) = split(/\t/);
	    $tut_dir_map{$file} = $dir;
	}
	close(MAP);
    }
}

open(ORDER, "<", "ORDER") or die "cannot open ORDER: $!";

my @dirs = <ORDER>;
chomp @dirs;
close(ORDER);

my @files;
my @other_files;
for my $dir (@dirs)
{
    my($f, $of) = find_titles_in_dir($dir, $dest);
    push(@files, @$f);
    push(@other_files, @$of);
}

if ($mode eq 'show')
{
    print join("\t", @$_), "\n" foreach @files;
    exit 0;
}

open(INDEX, ">", "$dest/index.html") or die "Cannot write $dest/index.html: $!";
print INDEX "<ul>\n";

publish_files(\@files, \@other_files);

print INDEX "</ul>\n";
close(INDEX);

sub find_titles_in_dir
{
    my($dir, $dest) = @_;

    my $here = getcwd;

    my @files;
    if (open(O, "<", "$dir/ORDER"))
    {
	@files = <O>;
	chomp @files;
	close(O);
    }
    else
    {
	opendir(D, $dir) or die "cannot opendir $dir: $!";
	@files = sort { $a cmp $b } grep { /\.html$/ && -f "$dir/$_" } readdir(D);
	closedir(D);
    }

    my %seen;
    my @out;
    for my $file (@files)
    {
	$seen{$file} = 1;
	my $title = get_title("$dir/$file");

	my $kbfile = lc($title);
	$kbfile =~ s/[^-\w\s]//g;
	$kbfile =~ s/\s+/-/g;

	push(@out, [$file, $title, $kbfile, "$dir/$file"]);
    }

    #
    # Build the list of other files we want to copy.
    #
    opendir(D, $dir) or die "cannot opendir $dir: $!";
    my @other = map { [$_, "$dir/$_"] } grep { -f "$dir/$_" && !$seen{$_} && !/^\./ } readdir(D);
    closedir(D);
    return \@out, \@other;
}

sub publish_files
{
    my($html_files, $other_files) = @_;

    if ($mode eq 'kbase')
    {
	#
	# Write the mapping file for add_links_to_tutorials to use
	#
	open(H, ">html.map") or die "Cannot write html.map: $!";
	for my $fent (@$html_files)
	{
	    my($file, $title, $kbtitle) = @$fent;
	    my $sdir = $tut_dir_map{$file};
	    $sdir .= "/" if $sdir;
	    print STDERR "Look up $file: $sdir\n";
	    print H join("\t", $file, "$tut_url/$sdir$kbtitle/"), "\n";
	}
	    
	close(H);
    }

    my $here = getcwd;
    for my $fent (@$html_files)
    {
	my($file, $title, $kbtitle, $src) = @$fent;

	my $skip_commands = ($title =~ /Index/) ? "--skip-commands" : "";

	if ($mode ne 'show')
	{
	    print INDEX "<li><a href='$file'>$title</a></li>\n";
	    unlink("$dest/$file");
	}
	
	if ($mode eq 'copy')
	{
	    run("perl $scriptdir/add_links_to_tutorials.pl $skip_commands $add_links_args < $src > $dest/$file");
	}
	elsif ($mode eq 'kbase')
	{
	    run("perl $scriptdir/add_links_to_tutorials.pl $skip_commands --linkmap html.map $add_links_args < $src > $dest/$file");
	}
	elsif ($mode eq 'link')
	{
	    symlink("$here/$src", "$dest/$file");
	}
	elsif ($mode eq 'show')
	{
	    print "$file\t$title\t$dest/$file\n";
	}
    }
    for my $fent (@$other_files)
    {
	my($file, $src) = @$fent;
	copy($src, "$dest/$file");
    }
}

sub run
{
    my(@cmd) = @_;
    # print "RUN @cmd\n";
    my $rc = system(@cmd);
    # print "DONE $rc\n";
    $rc == 0 || die "Failed with $rc: @cmd\n";
}

sub get_title
{
    my($file) = @_;
    my $tree = HTML::TreeBuilder->new;
    $tree->parse_file($file);
    my $h1 = $tree->look_down("_tag", "h1");
    if ($h1)
    {
	return $h1->as_text;
    }
    else
    {
	return basename($file);
    }
}

sub get_title_old
{
    my($file) = @_;
    open(F, "<", $file) or die "Cannot read $file: $!";
    my $title;
    while (<F>)
    {
	if (/<h1>(.*?)</)
	{
	    $title = $1;
	    last;
	}
    }
    close(F);
    return $title;
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3