# pretty-print PmWiki page file
# 2009-08-31 Oliver Betz http://oliverbetz.de/
# Based on code from Eemeli Aro

# you don't need a full perl installation to run this script, since it
# doesn't need any modules. Activestate's Perl.exe and perl58.dll are sufficient

my $text;

if($ARGV[0] eq ''){
  print STDERR "Script to pretty-print PmWiki page files\n";
  print STDERR "Usage: $0 infile [outfile]\n";
  exit;
}

if((-f($ARGV[0])) == 0){
  print STDERR "couldn\'t open $ARGV[0]\n";
  exit;
}

if($ARGV[1] ne ''){
  open(OUTFILE, ">$ARGV[1]") or die "couldn\'t open $ARGV[1]";
}
else{
  open(OUTFILE, ">-") or die "couldn\'t write to STDOUT";
};

open( FILE, $ARGV[0] ) or die "Can't open $ARGV[0]";

while(<FILE>) {
  next if/^[^=]+:.*/;  # skip history lines (no '=' before ':')

  chomp;
  if( /^text=(.*)/ ) {
    $text = $1; # remember page text
  }
  else{
    /^([^=]+)=(.*)/;
    printf OUTFILE "%10s | %s\n", $1, $2;
  }
}  # while
close( FILE );

$text =~ s/%0a/\n/g;
$text =~ s/%3c/</g;
$text =~ s/%25/%/g;
print OUTFILE "$text\n";

close OUTFILE;

exit;

__END__