#!/usr/bin/perl # -T use strict; use warnings; use Date::Parse; use Encode; use HTML::Entities qw/:DEFAULT encode_entities_numeric /; use LWP; # status/global exec my $debug=1; # file/uri setup $ENV{"HOME"} =~ /(.*)/; my $publichtml="$1/public_html/"; my $filehtml="nblog_real.html"; my $filerss1="nblog.rdf"; #my $dir="mute_psychiatrist"; my $dir="blog"; my $urlhome="http://oelzant.priv.at/~aoe"; my $entryregex="[0-9].*.html"; my $template="$publichtml/$dir/template_nblog.html"; # to contain #UPDATED# and #ITEMLIST# # desc my $author="Alexander Oelzant <alexander\@oelzant.priv.at>"; my $title="aoe: Not a 'blog"; my $desc="various rants and musings"; my $image="http://oelzant.priv.at/~aoe/images/trash/alex3sm_small.jpg"; my $starttime=iso8601date(0); # content"style" my %substs=( "#UPDATED#" => '"
last updated
"', "#AUTHOR#" => '"$author"', "#DESC#" => '"$desc"', "#IMAGE#" => '"$image"', ); # Create an RFC822 compliant date (current time) sub iso8601date { my ($time)=@_; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); my @date = ($year+1900,$mon+1,$mday,$hour,$min,$sec); return sprintf("%d-%02d-%02dT%02d:%02d:%02dZ", @date); } sub html_escape { my ($in)=@_; $in =~ s/>/\>/msg; $in =~ s/\</msg; $in =~ s/&/\&/msg; $in =~ s/"/\"/msg; return $in; } my $iso8601date=iso8601date(time); sub rss_head { my ($entries)=@_; my $list=""; for (@$entries) { my ($url,$itime,$mtime,$head,$lang,$body)=@$_; print "$url\n"; $list .= "]*>.*?
\s*)+)#msi) { my ($head,$body)=($1,$2); if (length($body) > 1100) { # && ($body =~ m#]*>.*?
\s*$#i) ) { $body =~ s#^((]*>.*?
\s*$##i; #$body =~ s#$# #mg; } my $filedate=datefromname($filebase); my $filemod=[stat $file]->[9]; $head =~ s/^\s+//msg; $head =~ s/\s+$//msg; $body =~ s/^\s+//msg; $body =~ s/\s+$//msg; print "$filebase: ($head) $body\n" if $debug; return ([$filebase,$filedate,$filemod,$head,$lang,$body]); } else { warn "no h1 in $filebase"; } return undef; } sub cat { my ($file)=@_; open I,$file || die "open $file";; my $oldifs=$/; $/=undef; my $in=; close $file; $/=$oldifs; return $in; } # converts yyyymmdd.html or ssssssssssss.html to unix timestamp sub datefromname { my $r; my $f=shift; # strip dir if ($f =~ m#/([^/]+)$#) { $f=$1; } print "$f => "; if ($f =~ /^(\d\d\d\d)(\d\d)(\d\d)(?!\d).*\.html$/) { $r=str2time("$1-$2-$3"); } elsif ($f =~ /^(\d+)(?!\d).*\.html/) { $r=$1; } else { $r="0 but should be an error"; } print "$r\n"; return $r; } sub parse_dir { my ($file,$in,@entries,@entry); my ($thisdir)=@_; opendir DIR,$thisdir; while ($file=readdir(DIR)) { if ($file =~ /$entryregex/) { my $tuple = parse_file("$thisdir/$file"); if ($tuple && @$tuple == 6) { push @entries,$tuple; } else { print "$tuple unparseable\n"; } } } return \@entries; } sub format_item_rss1 { my ($url,$itime,$mtime,$head,$lang,$body)=@_; $lang = "un" unless $lang; #$head = encode("utf-8",decode("iso-8859-1", decode_entities($head))); $head = encode_entities_numeric($head); # encode("utf-8",decode("iso-8859-1", decode_entities($head)))); #$body = html_escape(encode_entities(decode("iso-8859-1", decode_entities($body)))); # kludge: absolutize links $body =~ s#="/~aoe#="$urlhome#msg; $body = encode_entities_numeric(decode("iso-8859-1", $body)); my $parsdate=iso8601date($itime); return <<"EOITEM";][^>]*>|>))(.*?)(
)#$1$2$3#sg; #$body =~ s#^(.*)$##ms; #$body =~ s#?p[^>]*>##g; return ""; } sub format_item_html { my ($url,$itime,$mtime,$head,$lang,$body)=@_; print "format_item_html: $url,$itime,$mtime,$head,$lang\n"; $lang = "un" unless $lang; my $parsdate=localtime($itime); my $parsmod=localtime($mtime); #$body =~ s#?p[^>]*>##g; return " "; } sub write_file_html { my ($name,$template,$entriesr)=@_; print "write_file_html: @$entriesr\n"; my $outfile=cat($template); my $items=""; for my $subst (keys(%substs)) { my $repl=$substs{$subst}; $outfile =~ s/$subst/eval $repl/emsg; } for (@$entriesr) { $items .= format_item_html(@$_); } $items .= ""; $outfile =~ s/#ITEMLIST#/$items/g; print "write_file_html: writing $name\n"; open O,">$name" || die "open >$name"; print O $outfile; close O; $outfile=cat($template); $items=""; for my $subst (keys(%substs)) { my $repl=$substs{$subst}; $outfile =~ s/$subst/eval $repl/emsg; } for (@$entriesr) { $items .= format_item_html1(@$_); } $items .= ""; $outfile =~ s/#ITEMLIST#/$items/g; $name =~ s/\.html$/1.html/; print "write_file_html: writing $name\n"; open O,">$name" || die "open >$name"; print O "$outfile"; close O; return 0; } sub write_file_rss1 { my ($name,$entriesr)=@_; my $outfile=rss_head($entriesr); for my $subst (keys(%substs)) { my $repl=$substs{$subst}; $outfile =~ s/$subst/eval $repl/emsg; } for (@$entriesr) { $outfile .= format_item_rss1(@$_); } $outfile .= "