#!/usr/bin/env perl ############################################## # # # doblog: static blogs generation tool # #--------------------------------------------# # @author: pancake # # @version: 0.4 # # @date: 2006-06-03 # # # ############################################## $progname="doblog"; $version="0.4"; $update=0; $prefix="data"; $extension="html"; # could be overrided in doblog/themes/xxx/blog.conf (to be php, pl, etc) $|=1; if ($ARGV[0]) { $prefix=$ARGV[0]; } $prefix="data"; print "Cannot find prefix directory '$prefix'\n" if ( ! -d $prefix ); # {{{ Default blog.conf values $podcast=0; $podcast_limit=5; $blog_limit=0; $cat_limit=0; $read_more_string="[read more]"; $comment_add_string="[add comment]"; $comment_view_string="[view comments]"; $list_of_posts_string="List of posts"; # }}} # {{{ MXML Parser sub parse_mxml { my (@files)=@_; my @data=(); my %item=(); my $line=""; my $base=".*"; my $target=""; my $nocomment=1; $is_comment=0 if ($files[0]=~/comment/); my $target=""; foreach $i (0 .. $#files) { my $file = $files[$i]; open( my $fd, "<","$prefix/$file") or return @data; #print "[r] $file\n"; while (chomp($line=<$fd>)) { $line=~s/^\s*//g if ($target ne "body"); $line=~s/\s*$//; if ($line=~/^<($base)>/) { $base=$1; %item={}; } else { 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); %item->{"folder"} = $file; if ($target eq "body") { %item->{$target}.="\n"; } } } else { if ($target ne $1) { $line = filter_line($line,!$is_comment); } # only content # XXX : optimization: \n makes me go 2x slower!!! WHY!? %item->{$target} .= $line."\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; } # {{{ PodCast 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 " $blog_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; } # }}} # {{{ GetRandomAtDot sub get_random_at_dot() { return (("_at_","ARROBA","_AT_","(at)","(arroba)")[int(rand(5))], ("_dot_","DOT", "_DOT_","(dot)","(.)")[int(rand(5))] ); } # }}} # Read doblog configuration file invalid_doblog_prefix("Cannot open blog.conf") if ( !-e "$prefix/blog.conf" ); require "$prefix/blog.conf"; # Post configuration checks $blog_limit=1 if ( $blog_limit<=0 ); $podcast_limit=1 if ($podcast_limit<=0); $blog_list_limit=1 if ($blog_list_limit<=0); $rss_limit=1 if ($rss_limit<=0); ($at,$dot) = get_random_at_dot(); $emailx=$email; $emailx=~s/\@/$at/; $emailx=~s/\./$dot/; # Read theme specific configuration if ( -e "doblog/themes/$theme/blog.conf" ) { require "doblog/themes/$theme/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++; #OTW : XXX use "filename" maybe not yet processed ? :) print "Unlinking post_".$pn.".$extension\n", unlink "post_$pn.$extension"; $mailbody.="/post_$pn.$extension\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.$extension"; unlink "$prefix/comments_todo"; #OTW :: comments_todo must be handled by the new post filenames. #$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; } # Set default configuration values my $target=""; my $title="doblog blog" if (!$title); my $version_string="$progname $version"; $theme="default" if ($theme eq ""); 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 # # {{{ Filter line sub filter_line { my ($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\]/<\/div>/g; $line=~s/\[console\]/
 Console:<\/div>/g; $line=~s/\[\/console\]/<\/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