#!/usr/bin/perl -w # eozl's alml2html convertor and consolidator. ask him about alml. # used in my online journals. proto-megamesh. # written aug 13-18, 2004 # ignore copyrights. use warnings; use CGI; use Time::HiRes qw(gettimeofday tv_interval ); use POSIX; $time0 = [gettimeofday]; #set up initial time print "Content-type: text/html\n\n"; # these ENV vars are useful: #[DOCUMENT_URI] -> [/html/evilhaxx0ring/perl.html] #[DOCUMENT_ROOT] -> [/var/www/localhost/htdocs] #find the directory of the html $entrydir = $ENV{DOCUMENT_ROOT} . substr($ENV{DOCUMENT_URI}, 0, rindex($ENV{DOCUMENT_URI}, '/')) . '/entries'; #read all the files there, push into @entryfiles opendir(DIR, $entrydir) or die "died trying to open $entrydir. $!"; @entryfiles=(); while (defined ($_ = $entry = readdir(DIR))) { push(@entryfiles,"$entrydir/$entry") if (-T "$entrydir/$entry" && /\.entry$/); } @entryfiles=(reverse sort @entryfiles); # process all the files # set up each entry header, css, and alml2html it. foreach $entry (@entryfiles) { open (DATA, $entry) or die "died while reading $file; $!"; $str = do { local $/, }; close DATA; $str =~ s/\s+$//; #clear whitespace at the end of the entry alml2html($str); #convert the entry to alml my ($dir, $f) = $entry =~ m/(.*\/)(.*)$/ ; ($title) = $f =~ m/(.*).entry/; #find the last modified timestamp my $mtime=(stat $entry)[9]; $mtimestr = lc(strftime("%a %b %d %H:%M:%S %Y", localtime($mtime))); print < \n$f | $mtimestr
EOHTML print "$str
\n\n\n"; } $elapsed = tv_interval ($time0); print ""; exit; ### # # ALML-2-HTML stuff below ### sub almlital { $str =~ s/\.\\(.*?)\.\\/$1<\/i>/g; } sub almllink { # NOTE: ## convertor for <> is not here. put this after <> is done. ## $str =~ s/<\/link>/<\/a>/g; # to take care of used in <> ### ### ### $str =~ s///g; # /local ./local $str =~ s/]*?tp:\/\/.*?)\1 ?>//g; #xtp://extern $str =~ s///g; #extern.com } sub almlparen { #use temporary string for escaped \(s ... #if there's a better way, tell me. $str =~ s/\\\(/_\(_/g; $str =~ s/\\\)/_\)_/g; $str =~ s/\(/\(/g; $str =~ s/\)/\)<\/font>/g; $str =~ s/_\(_/\(/g; $str =~ s/_\)_/\)/g; #$str =~ s/\\\(/\(/g; #screws up (()) #$str =~ s/^\(/\(/; #for initial (s #$str =~ s/([^\\])\)/${1}\)<\/font>/g; #screws up (()) #$str =~ s/\\\)/\)/g;screws up (()) } sub alml2html { ### remaining scutwork #initial PRE if (0) { @precontents = ($str =~ m/
(.*?)<\/pre>/gs);
    $prei=0;
    while ( $str =~ s/
(.*?)<\/pre>/__pre${prei}__/s) {
	$prei++;
    }}


    # deal with annoying .\italics.\
    almlital($str);
 
    # link: note  convertor is at the end here
    almllink($str);
   
    # deal with ()s parens
    almlparen($str);


    #deal with old  shortform of 
  • ... $str =~ s//
  • /g; #deal with \>-) $str =~ s/\\/>/g; ### #####deal with "quotes": set up array of " and positions ### #$str =~ s/\"(\w*)\"/~${1}~/g; $str =~ s/\\\"/"/g; ## make quotes list @quots=(); $n = -1; while (1) { $_=index($str, "\"",$n); if ($_ == -1) { last;} push(@quots, $_); $n=1+$_; } ## make tags list @tags=(); $n = -1; while (1) { $lt = index($str, "<", $n); $gt = index($str, ">", $n); #print "lt gt: $lt $gt\n"; if (($lt == -1) && ($gt == -1)) {last;} push(@tags, $lt, $gt ); #if we ever expect this to be needed... ##if ($lt < $gt) { $n = $gt +1 ; } else {$n = $lt +1 ;} $n = $gt + 1; } ## purge quotes inside tags $t = 0; $q = 0; @tagclose=(); while ($t <= $#tags) { #not foreached because we need to increm by 2 #set up @tagclose for <> if ($tags[$t] +1 == $tags[$t+1]) { $prevtag=substr($str, $tags[$t-2], 1+$tags[$t-1]-$tags[$t-2]); #print "phoobe: $t, $tags[$t-1], $prevtag \n
    "; $prevtag =~ s/<(.*?)(?:( .*?)>|(?)>)/<\/${1}>/g; push(@tagclose, $prevtag); $t+=2; next; } foreach $q (0..$#quots) { #while ($q <= $#quots) { if (($quots[$q] > $tags[$t]) && ($quots[$q] < $tags[$t+1])){ #delete $quots[$q]; $quots[$q]=-1; } } $t+=2; } ## rebuild the string $i=0; $qflag = 0; $offsets=0; foreach $i (0..$#quots) { if ($quots[$i] == -1) { next; } if ($qflag == 0) { substr($str, $quots[$i]+$offsets, 1) = "\""; $offsets+=4; $qflag = 1; } else { substr($str, $quots[$i]+$offsets, 1) = "\""; $offsets+=5; $qflag = 0; } } # take care of <>'s $n = -1; $t=0; while (1) { $_=index($str, "<>",$n); if ($_ == -1) { last;} substr($str, $_, 2) = $tagclose[$t]; $t++; $n=1+$_; } # #####FINISH QUOTES STUFF # # to take care of used in <> $str =~ s/<\/link>/<\/a>/g; #deal with \n\n->

    s parags, \n->
    breaks $str =~ s/\n\n/

    /g; $str =~ s/\n([^ ])/\n
    $1/g; # Remove
    and

    from

     things
        while (($breaker) = $str =~ m/
    .*?(
    |

    ).*?<\/pre>/gs) { $str =~ s/

    (.*?)${breaker}(.*?)<\/pre>/
    ${1}${2}<\/pre>/gs;
        }
    
        # NOT USED
        #another method of removing all tags from PREs...
        if (0) {
        #final PRE
        foreach $prei (0..$#precontents) {
    	#print "prei: $prei->$stf[$prei]\n";
    	$prestr="__pre${prei}__";
    	$str =~ s/$prestr/
    @precontents[$prei]<\/pre>/gs;
        }}
        # i r teh jeo!!!
    
    }