Click to See Complete Forum and Search --> : [PERL] String substitutions


alboot
March 27th, 2008, 10:13 AM
Hi,
I'd like to use a perl script to convert a web page coming from tiddlywiki to a text file that can be imported into MediaWiki.

Here is the code :


#!/usr/local/bin/perl

use POSIX qw(strftime);
#use POSIX::strptime;
#system "rm -rf tiddly2twiki.files/";
print "Starting\n";
mkdir "tiddly2twiki.files";
chdir "tiddly2twiki.files" || die "Could not chdir";
open TIDDLY, '<', '../notes.html' || die "Could not open input file";
print "Reading input file\n";
while(<TIDDLY>){
# m is the match operator
next unless m#^<div title="(.*?)".*modified="(\d+)".*?>(.*)#;
my $title=$1;
my $tiddate=$2;

my $tiddler;
my $line = <TIDDLY>;
while (!($line =~ /div/))
{
$tiddler = $tiddler.$line;
$line = <TIDDLY>;
}
#print $tiddler."\n";
#print $1.$2.$3."\n";

#my $twikidate=strftime('%s',POSIX::strptime($tiddate,'%Y%m%d%H%M'));
#my $filetitle=`grep -m1 "[^:]*:$title\$" ../tiddly2twiki.titlemap`;
my $filetitle=$title;
chomp($filetitle); # removes \n

$filetitle=~s/:.*//;
$filetitle.=".txt";
print "$filetitle\n";
open OUT, '>', $filetitle;
print OUT "=$title=\n";
# substitutions (To make a global substitution the last slash is followed by a g)
$tiddler=~s/<(.*)pre>//g;
$tiddler=~s#\\n#\n#g;
$tiddler=~s#\[\[(.*?)\|(.*?)\]\]#[[\2][\1]]#g;
$tiddler=~s#^!!!#---++++#mg;
$tiddler=~s#^!!#---+++#mg;
#$tiddler=~s#^!#---++#mg;
$tiddler=~s#^!.*#==.*==#mg;
$tiddler=~s#^\*\* # * #mg;
$tiddler=~s#^[*-] # * #mg;
$tiddler=~s/^# / 1. /mg;
$tiddler=~s#\{\{\{#<verbatim>#g;
$tiddler=~s#\}\}\}#</verbatim>#g;
$tiddler=~s#\|\|#|.|#g;
$tiddler=~s#(?<=\|)\&gt;(?=\|)##g;
$tiddler=~s#(?<=\|)!([^|]+)(?=\|)#*$1*#g;
print OUT $tiddler."\n";
}
print "Done\n";


I got this code from http://www.penlug.org/twiki/bin/view/Main/TiddlyWiki

Here are my questions :

1. I want to convert this script to use it on Windows but it seems that the strptime function is not available on Windows. Do you know if there is an alternate function that can be used ?
strptime returns a string according to the specified format :
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = POSIX::strptime("string", "Format");

2. Also, I'd like to know how to replace the string "!TEXT" by "==TEXT==" ?
I tried $tiddler=~s#^!.*#==.*==#mg; but it doesn't work (sorry, I'm still a begineer for PERL scripts).

Thanks