<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">=head1 NAME
 
yahobuilder - yet another homepage builder

=head1 README

Inspired by Zope yahobuilder takes the idea of defining variables
at different levels and substituting the closest one to offline
html preparaion. Yahobuilder includes also a simple upload tool
to a ftp server. Home of Yahobuilder is http://www.gs68.de.

=head1 SYNOPSIS

perl yahobuilder.pl

  -u to upload the site

  -r to erase the target directory

  default is create the offline html

You need to set the variables $g_source_dir (here resides your 
homepage in yahobuilder format) and $g_target_dir (here yahobuilder 
will prepare the html version), as well as the ftp login info
if you intend to use the upload, according your setup.

=head1 DESCRIPTION
 
Yahobuilder takes the idea to define variables in different levels 
(implemented in the directory structure of the source pages) from
Zope and substitute them into the actual pages.

To setup a a site using yahobuilder you write basically simple html
in which you can insert the 

&lt;dtml-var variable_name/&gt;

tag which yahobuilder substitutes with the content of the variable.
There are three ways to define variables:

=over 2

=item * 

files with the extension varname.var will be interpreted as variable
with name varname.

=item * 

short variables can be defined in a file folder.my_hp (in any
directory), this variables must be typed into one line and name 
and content are separated by name~~~content.

=item *
 
there is also a predefined variable navigate_parents that can be used
in conjunction with variables folder_title defined in each directory
to build up a simple navigation structure. See attached example for details
of the usage.

=back


Even the concept of yahobuilder is quite simple it is rather helpful
in achieving a consistant site layout without huge effort. And you
need not to use a complicated html editor. Please see the available
example if you are interested in yahobuilder.  

By using the proper arguments yahobuilder can be also used to
upload your offline prepared html to a ftp server. 

=head1 PREREQUISITES

C&lt;Getopt::Std&gt;

C&lt;Net::FTP&gt;

=head1 COREQUISITES

none

=head1 TODO

Any other good suggestions that people send me!

=head1 BUGS

Probably, but at moment not known.


=head1 AUTHOR

Gerhard Spitzlsperger
gerhard.spitzlsperger@gs68.de
http://www.gs68.de

=head1 SEE ALSO

The source of a older version of the www.gs68.de site (where you 
probably got this file) is available as example. Please be aware that 
some links will not work, because I removed some bigger data files to 
reduce size.

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2003, Gerhard Spitzlsperger. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the same terms as Perl.

=head1 SCRIPT CATEGORIES

Web

=cut


#!/usr/bin perl -w

use strict;
use Cwd;
use Getopt::Std;
use Net::FTP;
use vars qw( $AUTOLOAD );

#------------------------------------------------------------------------------
# YOU NEED TO SET THESE VARIABLES TO YOUR NEEDS, YOU DON'T NEED
# TO CHANGE SOMETHING BELOW
#
my $g_source_dir = "replace me";
my $g_target_dir = "replace me THIS DIRECTORY IS REASED BY THE -r OPTION ";
my $host = "ftp.....";   # the ftp host for upload
my $login = "......";
my $password = "......";
#------------------------------------------------------------------------------


sub usage
{
  print "Usage $0 [-u] [-r]\n";
  print "  -u to upload the site without local site construction\n";
  print "  -r to remove the target directory\n";
  print "  default is to process the offline html\n\n"; 
}


my $VERSION = 0.05;

my $g_dir_stack = my_directory_stack-&gt;new();
my $g_var_stack = my_variable_stack-&gt;new( pdir_stack =&gt; $g_dir_stack );


print "\n\nnow starting:\n\n\nyahobuilder.pl\n\n\n";

our($opt_r, $opt_u);
getopts('ur');

if( $opt_r )
{
  # erase the target directory
  system("rm -r $g_target_dir/*");
}
elsif( $opt_u )
{
  # upload to ftp host
  &amp;upload();
}
else
{
  chdir($g_source_dir);
  &amp;process_dir($g_source_dir);
}

#------------------------------------------------------------------------------
# subroutine definition
#------------------------------------------------------------------------------
sub process_dir
{
    my $dir = shift;
    chdir($dir);

    $g_dir_stack-&gt;push( $dir );

    my @files = &lt;*&gt;;
    my @vars = ();    

    # make id variable for folder
    my $v = new my_var(name =&gt; 'folder_id', 
		       content =&gt; "$dir",
		       mode =&gt; 'id');
    push(@vars, $v);

    # now putting file variables on the stack
    foreach my $fname (@files)
    {
	
	# this simple regex delivers the filename without extension
        # as no path can exist
	if( -f $fname &amp;&amp; $fname =~ m!(\w+)\.var$!)
	{
	    print "now working on file variable $fname\n";
	    my $v = new my_var(name =&gt; $1, 
			       fname =&gt; &amp;get_act_source_dir() . "/$fname",
			       mode =&gt; 'file');
	    push (@vars, $v);    
	}
	elsif( -f $fname &amp;&amp; $fname =~ /\.my_hp$/)
	{
	    print "now working on $fname\n";
	    my @v = &amp;process_my_hp_file( $fname );
	    push (@vars, @v);  
	}
    }

    $g_var_stack-&gt;push( \@vars );

    print "the actual directory is: ". $g_dir_stack-&gt;get_path(1) . "\n";

    # create corresponding target directory if not existing
    if(! -d  &amp;get_act_target_dir() )
    {
      system("mkdir " . &amp;get_act_target_dir() );
    }

    foreach my $fname (@files)
    { 
        my $ad = getcwd();

	if(-d $fname )
	{
	    print "$fname is a directory\n";

	    print ".. calling print_dir recursively\n";
	    &amp;process_dir($fname);
            $g_dir_stack-&gt;pop();
	    chdir(&amp;get_act_source_dir()); # we cannot simply go up because
	                                  # of symlinks.
	    print ".. now we are back to $fname\n"; 
	}
	elsif( -f $fname &amp;&amp; $fname =~ /\.html$/)
	{
	    &amp;process_html_file($fname);
	}
	elsif( -f $fname &amp;&amp; $fname =~ /\.var$/)
	{
	    # intentionally empty
	}
	elsif( -f $fname &amp;&amp; $fname =~ /^\.my_hp$/)
	{
	    # intentionally empty
	}
	elsif( -f $fname &amp;&amp; $fname =~ /~$/)
	{
	    # intentionally empty
            # we don't copy backup files
	}
	else
	{
	    #print "$fname is copied unchanged to target directory\n";

	    my $asd = &amp;get_act_source_dir();
	    my $atd = &amp;get_act_target_dir();

            # compare which files are unchanged, we decide
            # based on modification time and file size
 
            my $s_size = (stat("$asd/$fname"))[7];
            my $t_size = (stat("$atd/$fname"))[7];

            my $s_mtime = (stat("$asd/$fname"))[9];
            my $t_mtime = (stat("$atd/$fname"))[9];
 
       
            if( ($s_size != $t_size) || ($s_mtime &gt;= $t_mtime) 
                 || (! -e "$atd/$fname"))
            {
              print " updated file: $asd/$fname \n";
	      system("cp $asd/$fname $atd");
	    }
	  
	}
    }
    
    # remove vars of this dir from stack
    $g_var_stack-&gt;pop();
}



sub process_html_file
{
    my $fname = shift;

    my $buf = $g_var_stack-&gt;process_file(&amp;get_act_source_dir() . "/$fname");

    my $out_file = &amp;get_act_target_dir() . "/$fname";

    unless( open(FO, "&gt; $out_file") )
    {
	print STDERR "file $out_file cannot be opened\n\n";
	return;
    }

    print FO $buf;

    close(FO);
}

sub process_my_hp_file
{
    my $fname = shift;
    my @vars = ();

    unless( open(FH, "$fname") )
    {
	print STDERR "file $fname cannot be opened\n\n";
	return;
    }

    while( my $line = &lt;FH&gt; )
    {
        chomp $line;

	if( $line =~ /~~~/ )
	{
	    my $t = $';
	    my $v = new my_var(name =&gt; $`, 
			       content =&gt; $t,
			       mode =&gt; 'folder_title');
            #' just not to confuse sytay highlighting of emacs
	    push(@vars, $v);        
	}
    }


    # clean up and return
    close(FH);
    return @vars;
}


sub get_act_source_dir
{
    return $g_source_dir . $g_dir_stack-&gt;get_path(1);
}

sub get_act_target_dir
{
    return $g_target_dir . $g_dir_stack-&gt;get_path(1);
}

#------------------------------------------------------------------------------
# subroutines for upload
#------------------------------------------------------------------------------
sub upload
{
  my $ftp = Net::FTP-&gt;new($host);
  $ftp-&gt;login($login, $password);

  if( !defined $ftp )
  {
     print "could not login ... exiting\n";
     exit( -1);
  }

  &amp;upload_dir($g_target_dir, $ftp);

  $ftp-&gt;quit;
}

sub upload_dir
{
    my $dir = shift;
    my $ftp = shift;
   
    chdir($dir);

    $g_dir_stack-&gt;push( $dir );

    my @files = &lt;*&gt;;
    
    print "the actual directory is: ". $g_dir_stack-&gt;get_path(1) . "\n";
    
    foreach my $fname (@files)
    { 
        my $ad = getcwd();

	if(-d $fname )
	{
	    print "$fname is a directory\n";

            my $e = $ftp-&gt;cwd( $g_dir_stack-&gt;get_path(1) . "/$fname" );
            print "e $e\n";


            # if the directory doesn't exist make it 
            if( ! $e )  
            {  
	      print "create " . $g_dir_stack-&gt;get_path(1) ."\n";
              $ftp-&gt;mkdir( $g_dir_stack-&gt;get_path(1). "/$fname")    
	    }

	    print ".. calling upload_dir recursively\n";

	    &amp;upload_dir($fname, $ftp);
            $g_dir_stack-&gt;pop();
	    chdir(&amp;get_act_target_dir()); # we cannot simply go up because
	                                  # of symlinks.
	    print ".. now we are back to $fname\n"; 
	}
	else
	{
	    my $asd = &amp;get_act_target_dir();
	  
            # compare which files are unchanged, we decide
            # based on modification time 
            my $s_mtime = (stat("$asd/$fname"))[9];
       
            # this is the required filename on the server 
            my $temp = $g_dir_stack-&gt;get_path(1);
              
            print " $temp/$fname \n";  

            my $f_mtime = $ftp-&gt;mdtm("$temp/$fname");
            
            if( $s_mtime &gt;= $f_mtime )
            {
              print " updated file: $asd/$fname \n";
	      my $t = $ftp-&gt;put( "$asd/$fname",  "$temp/$fname");
              print "  to $t \n\n";
	    }
	}
    }
}

#------------------------------------------------------------------------------
# Package Definition
#------------------------------------------------------------------------------

package my_var;

use strict;
use vars qw( $AUTOLOAD );
use Carp;

BEGIN
{
    my %_attr_data = ( _content   =&gt; [undef, 'read/write'],
		       _fname     =&gt; ['', 'read'] ,
		       _mode      =&gt; ['file', 'read'] ,
		       _name      =&gt; ['', 'read'] ,
		       _type      =&gt; ['scalar', 'read/write']
		       );

    my $_count = 0;

    sub _accessible
    {
	my ($self, $attr, $mode ) = @_;
	return $_attr_data{$attr}[1] =~ /$mode/;
    }
 
    sub _default_for
    {
	my ($self, $attr ) = @_;
	return $_attr_data{$attr}[0];
    }

    sub _standard_keys
    {
	return  keys %_attr_data;
    }

    sub get_count
    {
	return $_count;
    }

    sub _incr_count { ++$_count }
    sub _decr_count { --$_count }

}

sub new
{
    my ($class, %arg ) = @_;
    my $self = bless {}, $class;

    foreach my $attr_name ($self-&gt;_standard_keys() )
    {
	$attr_name =~ /^_(.*)/;
	my $arg_name = $1;

	if( exists $arg{$arg_name} )
	{
	    $self-&gt;{$attr_name} = $arg{$arg_name};
	}
	else
	{
	    $self-&gt;{$attr_name} = $self-&gt;_default_for($attr_name);
	}
    }

    $self-&gt;_incr_count();

    return $self;
}
	    

sub DESTROY
{
    $_[0]-&gt;_decr_count();
}

sub AUTOLOAD
{
    no strict "refs";

    my ($self, $new_value) = @_;

    # was it get....?
    if( $AUTOLOAD =~ /.*::get(_\w+)/  &amp;&amp; $self-&gt;_accessible( $1, 'read'))
    {
	my $attr_name = $1;

	*{$AUTOLOAD} = sub { return $_[0]-&gt;{$attr_name} };
	return $self-&gt;{$attr_name};
    }

    # was it set....?
    if( $AUTOLOAD =~ /.*::set(_\w+)/  &amp;&amp; $self-&gt;_accessible( $1, 'write'))
    {
	my $attr_name = $1;

	*{$AUTOLOAD} = sub { $_[0]-&gt;{$attr_name} = $_[1]; return; };
	$self-&gt;{$attr_name} = $new_value;
	return;
    }  
    
    # seems to be a failure
    croak "no such method: $AUTOLOAD";
}

#------------------------------------------------------------------------------


package my_directory_stack;

use strict;
use vars qw( $AUTOLOAD );
use Carp;

BEGIN
{
    my %_attr_data = ( _stack   =&gt; [undef, 'read']
		     );

    my $_count = 0;

    sub _accessible
    {
	my ($self, $attr, $mode ) = @_;
	return $_attr_data{$attr}[1] =~ /$mode/;
    }
 
    sub _default_for
    {
	my ($self, $attr ) = @_;
	return $_attr_data{$attr}[0];
    }

    sub _standard_keys
    {
	return  keys %_attr_data;
    }

    sub get_count
    {
	return $_count;
    }

    sub _incr_count { ++$_count }
    sub _decr_count { --$_count }

}

sub new
{
    my ($class, %arg ) = @_;
    my $self = bless {}, $class;

    # initialization of attributes
    foreach my $attr_name ($self-&gt;_standard_keys() )
    {
	$attr_name =~ /^_(.*)/;
	my $arg_name = $1;

	if( exists $arg{$arg_name} )
	{
	    $self-&gt;{$attr_name} = $arg{$arg_name};
	}
	else
	{
	    $self-&gt;{$attr_name} = $self-&gt;_default_for($attr_name);
	}
    }

    # count instances
    $self-&gt;_incr_count();

    # initially the stack is an empty list
    $self-&gt;{'_stack'} = [];

    return $self;
}
	

sub get_path
{
    my $self = shift;
    my $level = shift || 1;
    my $max = shift || $#{$self-&gt;{'_stack'}};

    my $r = '';

    for(my $i=abs($level); $i &lt;= $max; $i++)
    {
	$r .= '/' . $self-&gt;{'_stack'}-&gt;[$i];
    }

    $r =~ s!/!! if( $level &lt; 0 );

    return $r;
}

sub push
{
    my $self = shift;
    my $t = shift;

    push(@{$self-&gt;{'_stack'}}, $t);
}

sub pop
{
    my $self = shift;
 
    return pop(@{$self-&gt;{'_stack'}});
}

sub DESTROY
{
    $_[0]-&gt;_decr_count();
}

sub AUTOLOAD
{
    no strict "refs";

    my ($self, $new_value) = @_;

    # was it get....?
    if( $AUTOLOAD =~ /.*::get(_\w+)/  &amp;&amp; $self-&gt;_accessible( $1, 'read'))
    {
	my $attr_name = $1;

	*{$AUTOLOAD} = sub { return $_[0]-&gt;{$attr_name} };
	return $self-&gt;{$attr_name};
    }

    # was it set....?
    if( $AUTOLOAD =~ /.*::set(_\w+)/  &amp;&amp; $self-&gt;_accessible( $1, 'write'))
    {
	my $attr_name = $1;

	*{$AUTOLOAD} = sub { $_[0]-&gt;{$attr_name} = $_[1]; return; };
	$self-&gt;{$attr_name} = $new_value;
	return;
    }  
    
    # seems to be a failure
    croak "no such method: $AUTOLOAD";
}

#------------------------------------------------------------------------------
package my_variable_stack;

use strict;
use vars qw( $AUTOLOAD );
use Carp;

BEGIN
{
    my %_attr_data = ( _stack   =&gt; [undef, 'read'],
		       _pdir_stack   =&gt; [undef, 'read/write'],
		     );

    my $_count = 0;

    sub _accessible
    {
	my ($self, $attr, $mode ) = @_;
	return $_attr_data{$attr}[1] =~ /$mode/;
    }
 
    sub _default_for
    {
	my ($self, $attr ) = @_;
	return $_attr_data{$attr}[0];
    }

    sub _standard_keys
    {
	return  keys %_attr_data;
    }

    sub get_count
    {
	return $_count;
    }

    sub _incr_count { ++$_count }
    sub _decr_count { --$_count }

}

sub new
{
    my ($class, %arg ) = @_;
    my $self = bless {}, $class;

    # initialization of attributes
    foreach my $attr_name ($self-&gt;_standard_keys() )
    {
	$attr_name =~ /^_(.*)/;
	my $arg_name = $1;

	if( exists $arg{$arg_name} )
	{
	    $self-&gt;{$attr_name} = $arg{$arg_name};
	}
	else
	{
	    $self-&gt;{$attr_name} = $self-&gt;_default_for($attr_name);
	}
    }

    # count instances
    $self-&gt;_incr_count();

    # initially the stack is an empty list
    $self-&gt;{'_stack'} = [];

    return $self;
}
	
sub push
{
    my $self = shift;
    my $t = shift;

    push(@{$self-&gt;{'_stack'}}, $t);
}

sub pop
{
    my $self = shift;
 
    return pop(@{$self-&gt;{'_stack'}});
}

sub lookup_variable
{
    my $self = shift;
    my $name = shift;

    my $result; 
    my $i; 

    

    # check some standard variables
    if( $name eq 'navigate_parents' )
    {
	print "navigate ....\n";
	$result = '&lt;a href="/index.html"&gt;Home&lt;/a&gt;';	

	for($i=1; $i &lt;= $#{$self-&gt;{'_stack'}}; $i++)
	{   
	    my @vars = @{$self-&gt;{'_stack'}-&gt;[$i]};

	    my $t;
	    foreach my $j (@vars)
	    {
		if('folder_title' eq $j-&gt;get_name() )
		{
		    $t =  $j-&gt;get_content();
		    last;
		} 
	    }  

	    if( !defined $t )
	    {
		foreach my $j (@vars)
		{
		    if('folder_id' eq $j-&gt;get_name() )
		    {
		       $t =  $j-&gt;get_content();
		    }   
		}
	    }
	    
	    # read the directory stack until the relevant level
	    $result .= ' &amp;gt;  &lt;a href="' . 
                        $self-&gt;{'_pdir_stack'}-&gt;get_path(1, $i) .
			'/index.html"&gt; ' . $t . '&lt;/a&gt;';

	}
	print $result;
    }

    # check the stack
    $i=$#{$self-&gt;{'_stack'}};
    while( $i &gt;= 0 &amp;&amp; !defined $result)
    {
	my @vars = @{$self-&gt;{'_stack'}-&gt;[$i]};

	for( my $j=0; $j &lt;= $#vars; $j++)
	{
	    if($name eq $vars[$j]-&gt;get_name() )
	    {
		if( defined $vars[$j]-&gt;get_content() )
		{
		    $result = $vars[$j]-&gt;get_content();
		} 
		elsif( $vars[$j]-&gt;get_mode() eq 'file' ) 
		{
		    # content of file variables will be never set
                    # because some variables which are called inside
                    # might depend on the directory
		    $result = $self-&gt;process_file( $vars[$j]-&gt;get_fname() );
		}
		else
		{
		    print STDERR "VARIABLE $name not " .
                                 "defined in this context!\n";
		    exit(1);
		}
	    }
	}
	$i--;
    }

    if( !defined $result )
    {
	print STDERR "VARIABLE $name not " .
	    "defined in this context!\n";
	exit(1);
    }
    print "now looking up: $name, ok\n";
    #print "result:\n$result\n\n!!!!continue"; &lt;STDIN&gt;;

    return $result;
}

sub process_file
{
    my $self = shift;
    my $fname = shift;

    my $buf = '';

    unless( open(FH, $fname) )
    {
	print STDERR "file $fname cannot be opened\n\n";
	return;
    }

    my @lines = &lt;FH&gt;;

    while( @lines )
    {
	my $line = shift @lines;
	
	# ? is necessary to force non greedy matching
	if( $line =~ m!&lt;dtml-var\s+(.*?)\s*/&gt;! )
	{
	    my $var = $1;
 
	    my $t = $self-&gt;lookup_variable($var);
	    
	    $line = '';
	    $line .= $` if defined $`;
	    $line .= $t;

	    # must be parsed again
	    unshift(@lines, $') if defined $';
	}

	$buf .= $line;
    }

    close(FH);

    return $buf;
}

sub print_var_stack
{
    my $self;

    my $i=$#{$self-&gt;{'_stack'}};

    print "var stack (len: $i)\n";
    while(  $i &gt;= 0 )
    {
	my @vars = @{$self-&gt;{'_stack'}-&gt;[$i]};

	print "level $i:\n";
	for( my $j=0; $j &lt;= $#vars; $j++)
	{
	    print  '  ' . $vars[$j]-&gt;get_name() . 
		          $vars[$j]-&gt;get_fname() ."\n"; 
	}
	print "\n";
	$i--;
    }
}

sub DESTROY
{
    $_[0]-&gt;_decr_count();
}

sub AUTOLOAD
{
    no strict "refs";

    my ($self, $new_value) = @_;

    # was it get....?
    if( $AUTOLOAD =~ /.*::get(_\w+)/  &amp;&amp; $self-&gt;_accessible( $1, 'read'))
    {
	my $attr_name = $1;

	*{$AUTOLOAD} = sub { return $_[0]-&gt;{$attr_name} };
	return $self-&gt;{$attr_name};
    }

    # was it set....?
    if( $AUTOLOAD =~ /.*::set(_\w+)/  &amp;&amp; $self-&gt;_accessible( $1, 'write'))
    {
	my $attr_name = $1;

	*{$AUTOLOAD} = sub { $_[0]-&gt;{$attr_name} = $_[1]; return; };
	$self-&gt;{$attr_name} = $new_value;
	return;
    }  
    
    # seems to be a failure
    croak "no such method: $AUTOLOAD";
}


1;

</pre></body></html>