############################################################################## # # Settings - please edit! # ############################################################################## # Palm pilot data directory (e.g. "C:\\Palm\\JaunceD" ) WITHOUT trailing slash. # There should exist memopad/memopad.dat inside this directory my $palm_data_dir = "C:\\Palm\\JaunceD"; my $output_dir = ".\\output"; my $stylesheet_link = "file:///C:/progra~1/memo2html/memo2html.css"; # If FTP disabled #my $stylesheet_link = "/memo2html/memopad.css"; # If FTP enabled my $ftp_enabled = 0; # change to 1 to enable, 0 to disable. my $ftp_server = "ftp.btinternet.com"; my $ftp_dir = "/pub/memo2html"; my $ftp_username = "my.username"; ############################################################################## # # You should not edit below this line. # ############################################################################## my $version = "1.0.0"; ############################################################################## # Main ############################################################################## use strict; use Pilot::MemoPad; use File::Path; use Net::FTP; #$| = 1; # uncomment to force immediate file flush. clearDirectory( $output_dir ); ensureDirectoryExists( $output_dir ); my $memodata = new Pilot::MemoPad $palm_data_dir; my @memos = @{$memodata->{'memos'}}; my $memo; my $pagecounter = 0; foreach $memo (@memos) { $pagecounter += processMemo($memo); } print "Converted $pagecounter pages to HTML.\n"; ensureIndexPagesExist( $output_dir, "" ); if( $ftp_enabled ) { ftp(); } exit; ############################################################################## # Procedures ############################################################################## # Process a single memo. This is where the main work is done. sub processMemo { my ($memo) = @_; my $line; my @lines = @{$memo->{'text'}}; chomp @lines; if( !isMarkedForConversion( @lines ) ) { return 0; } # Definitely dealing with a memo that needs conversion. my $file = getFilename( @lines ); my $cat = findCategory( @lines ); my $title = findTitle( @lines ); my $catpath = $cat; # Prepend output directory $catpath = $output_dir . "\\" . $catpath; # Append .html if necessary if( ( substr $file, -5, 5 ) ne ".html" ) { $file = $file . ".html"; } ensureDirectoryExists( $catpath ); $file = $catpath . "\\" . $file; @lines = removeTagsFromLines( @lines ); writeHtml( $file, $cat, $title, @lines ); return 1; } # Returns 1 (true) if the file has @file... on the first line. sub isMarkedForConversion { my( @lines ) = @_; my $firstLine = $lines[ 0 ]; #print "First line = |$firstLine|\n"; if( ( index $firstLine, "\@file " ) != 0 ) # there is no '@file ...' on the first line { return 0; } else { return 1; } } # Return the string following the @file tag. sub getFilename { my( @lines ) = @_; my $firstLine = $lines[ 0 ]; return substr $firstLine, 6; } # Find category definition, if there is one. sub findCategory { my( @lines ) = @_; my $line; foreach $line ( @lines ) { if( ( index $line, "\@cat " ) == 0 ) { my $cat = substr $line, 5; #print "Category = $cat\n"; return $cat; } } return ""; } # Find title (first non-tagged line) sub findTitle { my( @lines ) = @_; my $line; foreach $line ( @lines ) { if( ( substr $line, 0, 1 ) ne '@' ) { return $line; } } return "Untitled"; } # Makes sure that a directory exists. sub ensureDirectoryExists { my ($fullpath) = @_; my $dirhandle; if( !( opendir $dirhandle, $fullpath ) ) { #print "Creating directory: $fullpath ...\n"; mkdir $fullpath; if( !( opendir $dirhandle, $fullpath ) ) { die "STILL can't create directory: $fullpath\n"; } } close $dirhandle; } # Just dump the raw text of a memo to stdout (for testing) sub printMemo { my ($memo) = @_; my $line; foreach $line ( @{$memo->{'text'}} ) { print $line . "\n"; } } # Clear out all files and directories in the specified directory. sub clearDirectory { my ($dir) = @_; rmtree( $dir, 0, 0 ); # from the core module File::Path } # Write a HTML file sub writeHtml { my( $file, $cat, $title, @lines ) = @_; #print "File = $file Category = $cat Title = $title\n"; my $handle; if( !open( $handle, ">$file" ) ) { die "Could not write file: $file\n"; } print $handle ""; print $handle $title; print $handle ""; my $html; my $line; foreach $line (@lines) { #print $handle ( . "\n" ); $html .= formatLine( $line ) . "\n"; } print $handle ( formatBody( $file, $cat, $title, $html ) ); print $handle ""; close $handle; } # Removes all lines with @file, @cat, and the title line. sub removeTagsFromLines { my( @lines ) = @_; my $line; my @newlines; my $title = ""; foreach $line (@lines) { if( ( index $line, "\@file " ) == 0 ) { next; } if( ( index $line, "\@cat " ) == 0 ) { next; } if( ( $title eq "" ) && ( ( substr $line, 0, 1 ) ne '@' ) ) { $title = $line; next; } push @newlines, $line; } return @newlines; } # Format a line sub formatLine { my( $line ) = @_; # @private if( ( index $line, "\@private" ) == 0 ) { $line = ""; } ############################################# # HTML formatting tags ############################################# #

if( ( index $line, "\@h1 " ) == 0 ) { $line = "

" . substr( $line, 4 ) . "

"; } #

if( ( index $line, "\@h2 " ) == 0 ) { $line = "

" . substr( $line, 4 ) . "

"; } #

if( ( index $line, "\@h3 " ) == 0 ) { $line = "

" . substr( $line, 4 ) . "

"; } #

if( ( index $line, "\@h4 " ) == 0 ) { $line = "

" . substr( $line, 4 ) . "

"; } # hyperlink if( ( index $line, "\@link " ) == 0 ) { my $pos = rindex( $line, ' ' ); my $linkname = substr( $line, 6, ($pos-6) ); my $linkpage = substr( $line, $pos ); $line = "$linkname"; } $line = $line . "
"; return $line; } # Formats the body of the page, surrounding it with a nice table, etc. sub formatBody { my( $file, $cat, $title, $body ) = @_; my $html = ""; $html .= ""; $html .= ""; $html .= ""; return $html; } # Creates index files if necessary for specified dir and all subdirs sub ensureIndexPagesExist { my ( $rootdir, $cat ) = @_; #print "Checking ".$rootdir."\n"; ensureIndex( $rootdir, $cat ); # get all files in directory my @dirs = getFilesInDir( $rootdir ); my $dir; foreach $dir (@dirs) { if( $dir ne "." && $dir ne ".." ) { my $newdir = $rootdir . "/" . $dir; if( -d $newdir ) { #print "-- Recursing into $newdir..\n"; ensureIndexPagesExist( $newdir, $dir ); } else { #print "-- NOT Recursing into $newdir - it's a file!\n"; } } } } # ensures that an index file exists for the specified directory. sub ensureIndex { my ( $dir, $cat ) = @_; my $indexfilename = $dir . "/index.html"; if( -e "$indexfilename" ) { #print "$indexfilename EXISTS - DON'T need to create $indexfilename\n"; } else { #print "Need to create $indexfilename ...\n"; my $file = $indexfilename; my $title = "Index of category '$cat'"; if( $cat eq "" ) { $title = "Top-level category index"; } my @lines; my @subfiles = getFilesInDir( $dir ); my $subfile; foreach $subfile ( @subfiles ) { #print "scanning subfile: $subfile\n"; if( $subfile ne "." && $subfile ne ".." && $subfile ne "index.html" ) { if( -f $dir."/".$subfile ) { my $subtitle = getTitleFromHtmlFile( $dir . "/" . $subfile ); if( !isPrivateFile( $dir."/".$subfile ) ) { push @lines, "\@link $subtitle $subfile"; #print "added link to $subfile\n"; } } if( -d $dir."/".$subfile ) { my $subtitle = "Category: $subfile"; $subfile = $subfile . "/index.html"; push @lines, "\@link $subtitle $subfile"; #print "added link to $subfile\n"; } } } writeHtml( $file, $cat, $title, @lines ); } } sub getFilesInDir { my ( $dir ) = @_; opendir( DIR, $dir) || die "can't opendir $dir: $!"; my @files = readdir( DIR ); closedir DIR; return @files; } sub getTitleFromHtmlFile { my ( $filename ) = @_; open( FILE, $filename ) || die "can't open file $filename\n"; my @lines = ; my $line; my $concatenated; foreach $line ( @lines ) { chomp $line; $concatenated .= $line; } $concatenated =~ /(.+)<\/title>/; my $title = $1; #print "I think the title of $filename is '$title'.\n"; return $title; } # FTP the files to the internet sub ftp { $| = 1; # forces flushing of text output after every write to stdout print "Connecting to $ftp_server... "; my $ftp = Net::FTP->new( $ftp_server, Debug => 0 ) or die $@; print "connected."; print "\nPassword (will echo to screen): "; my $password = <STDIN>; chomp( $password ); $ftp->login( $ftp_username,$password ) or die $ftp->message; print "Logged in..\n"; $ftp->type( "I" ); # set to BINARY print "Uploading files"; uploadDir( $ftp, "", $ftp_dir ); print "\n"; print "Closing connection...\n"; $ftp->quit; print "Finished.\n"; $| = 0; # automatic flushing of text output } sub uploadDir { my ( $ftp, $dir, $fullpathdir ) = @_; if( $dir eq "." || $dir eq ".." ) { return; } cwmkdir( $ftp, $fullpathdir ); # scan for files, upload if -f, recurse if -d my @files = getFilesInDir( $output_dir . "/" . $dir ); my $file; foreach $file (@files) { my $sourcefile = $output_dir."/".$dir."/".$file; if( -f $sourcefile ) # is a file { #print " - Uploading $sourcefile to $file\n"; if( !isPrivateFile( $sourcefile ) ) { $ftp->put( $sourcefile, $file ); print "."; } } if( -d $sourcefile ) # is a directory { uploadDir( $ftp, $file, $fullpathdir ."/".$file ); } #print "."; } #print "Uploaded everything from ". $output_dir . "/ to "; if( $ftp->pwd ne $ftp_dir ) { $ftp->cdup(); } } sub cwmkdir { my ( $ftp, $fullpathdir ) = @_; if( !$ftp->cwd( $fullpathdir ) ) { $ftp->mkdir( $fullpathdir ); $ftp->cwd( $fullpathdir ) or die "Could not create remote directory.\n"; } } sub ppwd { my ( $ftp ) = @_; print "pwd = " . $ftp->pwd . "\n"; } sub isPrivateFile { my ( $filename ) = @_; open( FILE, $filename ) || die "can't open file $filename\n"; my @lines = <FILE>; my $line; my $concatenated; foreach $line ( @lines ) { chomp $line; $concatenated .= $line; } if( ( index $concatenated, "<!-- Private, not for FTP to website -->" ) != -1 ) { return 1; } else { return 0; } }
"; if( $cat ne "" ) { $html .= "
Category: $cat
"; } $html .= "

"; $html .= "
$title



"; if( $cat ne "" ) { $html .= "Navigation:    "; if( ( rindex $file, "index.html" ) == -1 ) { $html .= "Index      "; } $html .= "Top-Level Index"; } $html .= "
 
$body
 
Generated: ". localtime( time() ) ."