#!/usr/bin/env perl ############################################## # # # doblog: static blogs generation tool # #--------------------------------------------# # @author: pancake # # @version: 0.3 # # @date: 2005-12-22 # # # ############################################## $progname="doblog"; $version="0.3"; $update=0; $prefix="data"; if ($ARGV[0]) { $prefix=$ARGV[0]; } $prefix="data"; if ( ! -d $prefix ) { print "Cannot find prefix directory '$prefix'\n"; } # {{{ Default blog.conf values $podcast=0; $podcast_limit=5; # }}} # {{{ MXML Parser sub parse_mxml { my (@files)=@_; my @data=(); my %item=(); my $line=""; my $base=".*"; my $target=""; my $nocomment=1; if ($files[0]=~/comment/) { $is_comment=0; } foreach $i (0 .. $#files) { my $file = $files[$i]; open( my $fd, "<","$prefix/$file") or return @data; #print "[r] $file\n"; while (chomp($line=<$fd>)) { if ($target ne "body") { $line=~s/^\s*//g; } $line=~s/\s*$//; # THIS IS BUGGY #if ($line=~/<\!--/) #{ # while(! $line=~/-->/ ) # { # chomp($line=); # } # $line=; #} if ($line=~/^<($base)>/) { $base=$1; %item={}; } else { # TODO: only filter_line when tag changes (optimization) if ($line=~/^<(.*)>\s*(.*)/) { # tag content if ($target ne "") { $target = $1; #filter_line($1,1); # XXX } if ($1 eq "/$base") { push @data, {%item}; $target=""; } else { $target=$1; $line=$2; %item->{$target} = filter_line($line,!$is_comment); if ($target eq "body") { %item->{$target}.="\n"; } } } else { # only content %item->{$target} .= filter_line($line,!$is_comment)."\n"; } } } close $fd; } return @data; } # }}} sub invalid_doblog_prefix { my ($str)=@_; if (!($prefix=~/\/$/)) { $prefix=$prefix."/"; } if (!($prefix=~/^\//)) {$prefix="./".$prefix; } print "doblog\@$prefix : $str\n"; exit 1; } sub podcast_get_filesize { my ($file)=@_; if ( $file=~/(files\/.*)/ ) { $filename=$1; if (( $filename ne "" ) || ( -e "$filename" )) { return ((stat($filename))[7]); } } else { # check if this file is here. $file=~s/$url//; $file="./".$file; if ( -f "$file") { return ((stat($file))[7]); } } return -1; } sub makePodcast { use POSIX qw(strftime); $now=(strftime "%a, %e %b %Y %H:%M:%S %z", gmtime); my @podcasts=parse_mxml(("podcast")); @podcasts=reverse @podcasts if ($inverse==1); open pod,">podcast.xml" or die "Cannot create podcast.xml"; print pod "\n"; print pod "\n"; print pod " $title\n"; print pod " $url\n"; print pod " $description\n"; print pod " $now\n"; print pod " es\n"; foreach $i (0 .. $#podcasts) { last if ($i>=$podcast_limit); $pn=$#podcasts-$i; $filesize=podcast_get_filesize($podcasts[$pn]{"link"}); print pod " \n"; print pod " ".$podcasts[$pn]{"title"}."\n"; print pod " ".$podcasts[$pn]{"date"}."\n"; print pod " ".$author."\n"; print pod " ".$podcasts[$pn]{"link"}."\n"; print pod " ".$podcasts[$pn]{"category"}."\n"; print pod " ".$podcasts[$pn]{"body"}."\n"; print pod " \n"; print pod " \n"; } print pod "\n"; print pod "\n"; close pod; # The same in PLS format open pod, ">podcast.pls"; print pod "[playlist]\n"; foreach $i (0 .. $#podcasts) { last if ($i>=$podcast_limit); $pn=$#podcasts-$i; $filesize = podcast_get_filesize($podcasts[$pn]{"link"}); print pod "File$i=".$podcasts[$pn]{"link"}."\n"; print pod "Title$i=".$podcasts[$pn]{"title"}."\n"; print pod "Length$i=$filesize\n"; } print pod "NumberOfEntries=".$#podcasts."\n"; print pod "Version=2\n"; close pod; } # Read doblog configuration file if (! -e "$prefix/blog.conf" ) { invalid_doblog_prefix("Cannot open blog.conf"); } require "$prefix/blog.conf"; # Clean config.php #if ( -e "admin/config.php" ) #{ # `cat admin/config.php | sort | uniq > admin/config.php.tmp`; # `mv admin/config.php.tmp admin/config.php`; #} if ($ARGV[0] eq "-date") { use POSIX qw(strftime); $str=(strftime "%a, %e %b %Y %H:%M:%S %z", gmtime); print "$str\n"; exit 0; } elsif ($ARGV[0] eq "-mkconf") { print <0); $update=1; } elsif ($ARGV[0] eq "-podcast") { $prefix=$ARGV[1] if ($#ARGV>0); makePodcast(); exit(0); } elsif ($ARGV[0] eq "-comments") { $prefix=$ARGV[1] if ($#ARGV>0); my $n=0; open PC,"$prefix/comments_todo" or die("No new comments"); $mailbody="You have new comments on these posts:\n\n"; while () { $pn=$_; chomp($pn); $n++; print "Unlinking post_".$pn.".html\n", unlink "post_$pn.html"; $mailbody.="/post_$pn.html\n"; } close PC; $mailbody.="\nHave a nice day!"; if ($n==0) { print "No comments todo.\n"; exit 0 } else { if ($mail_on_new_comments==1) { if ($email eq "") { print "\$email variable not defined.\n"; } else { system("echo '$mailbody' |mail $email"); } } } unlink "index.html"; unlink "$prefix/comments_todo"; $update=1; } elsif ($ARGV[0] eq "-last") { $prefix=$ARGV[1] if ($#ARGV>0); $update=1; $last=1; } elsif ($ARGV[0] eq "-hidden") { $prefix=$ARGV[1] if ($#ARGV>0); $update=1; $hidden=1; } elsif ($ARGV[0] eq "" || $ARGV[0] eq "-help") { print "usage: doblog.pl [-flags|prefix]\n"; print " -update: only update.Just create new files(do not overwrite)\n"; print " -hidden: update only hidden posts\n"; print " -last: update only the last post\n"; print " -version: shows version information\n"; print " -comments: only update posts that have queued comments to write\n"; print " -mkconf: prints a default configuration file to the stdout\n"; print " -help: show this message\n"; print " -date: shows date in RFC-2822 format\n"; exit 0; } $read_more_string="[read more]"; $comment_add_string="[add comment]"; $comment_view_string="[view comments]"; # Set default configuration values my $target=""; $title="doblog blog" if (!$title); $version_string="$progname $version"; if ($theme eq "") { $theme="default"; } if (-d "$theme" ) { $theme="$theme"; } elsif (-d "doblog/themes/$theme") { $theme="doblog/themes/$theme"; } else { print "Invalid theme '$theme'.\n"; exit 1; } print "Using theme '$theme'.\n"; # # Auxiliar subroutines # sub filter_line { ($line,$html)=@_; $st=$smileys_theme; # TODO: N space levels #$line=~s/\s/ /; $line=~s/>/>/g; $line=~s//g; $line=~s/\[:\(\]//g; $line=~s/\[;\)\]//g; $line=~s/\[:D\]//g; $line=~s/\[8\)\]//g; $line=~s/\[:P\]//g; $line=~s/\[:O\]//g; $line=~s/\[:\/\]//g; $line=~s/\[:\|\]//g; $line=~s/\[:~\]//g; } else { $line=~s/\[:\)\]/:\)/g; $line=~s/\[:\(\]/:\(/g; $line=~s/\[;\)\]/;\)/g; $line=~s/\[8\)\]/8\)/g; $line=~s/\[:D\]/:D/g; $line=~s/\[:P\]/:P/g; $line=~s/\[:O\]/:O/g; $line=~s/\[:\/\]/:\//g; $line=~s/\[:|\]/:|/g; $line=~s/\[:~\]/:'(/g; } $line=~s/\[right\]/

/g; $line=~s/\[\/right\]/<\/p>/g; $line=~s/\[console\]/

  console:<\/div>/g; $line=~s/\[\/console\]/<\/tt><\/div>/g; #$line=~s/\[feed\]/
feed<\/td><\/tr><\/table>
/g; #$line=~s/\[\/feed\]/<\/td><\/tr><\/table>/g; # TODO: This only works on local images!! #$line=~s/\[img src=/\[img\ \ src=$url\//; # characters $line=~s/\[,c\]/ç/g; $line=~s/\[,C\]/Ç/g; $line=~s/\[a'\]/á/g; $line=~s/\[A'\]/Á;/g; $line=~s/\['a\]/à/g; $line=~s/\[e'\]/é/g; $line=~s/\['e\]/è/g; $line=~s/\[E'\]/É/g; $line=~s/\[i'\]/í/g; $line=~s/\[I'\]/Í/g; $line=~s/\[:i\]/ï/g; $line=~s/\[:I\]/Ï/g; $line=~s/\['o\]/ò/g; $line=~s/\[o'\]/ó/g; $line=~s/\[O'\]/Ó/g; $line=~s/\[u'\]/ú/g; #ú $line=~s/\[U'\]/Ú/g; $line=~s/\[:o\]/ï/g; $line=~s/\[:O\]/Ð/g; $line=~s/\[:u\]/ü/g; $line=~s/\[:U\]/Ü/g; $line=~s/\[~n\]/ñ/g; $line=~s/\[~N\]/Ñ/g; $line=~s/\(C\)/©/g; $line=~s/\(R\)/®/g; $line=~s/&&/&/g; if ($html == 1) { $line=~s/\[//g; #$line=~s/
\n