[LON-CAPA-cvs] cvs: modules /gerd/Wiki convert.pl

www www at source.lon-capa.org
Wed Dec 14 11:44:56 EST 2011


www		Wed Dec 14 16:44:56 2011 EDT

  Modified files:              
    /modules/gerd/Wiki	convert.pl 
  Log:
  Copy image files into place
  
  
Index: modules/gerd/Wiki/convert.pl
diff -u modules/gerd/Wiki/convert.pl:1.5 modules/gerd/Wiki/convert.pl:1.6
--- modules/gerd/Wiki/convert.pl:1.5	Wed Dec 14 01:15:56 2011
+++ modules/gerd/Wiki/convert.pl	Wed Dec 14 16:44:56 2011
@@ -1,4 +1,6 @@
 use strict;
+use File::Copy;
+
 my %residx=();
 my %resdir=();
 my %idxres=();
@@ -6,6 +8,8 @@
 my %fileres=();
 my %titleres=();
 my %duplicatecnt=();
+my %filelink=();
+my %filedir=();
 
 # Read the copy/paste of the treemenu source code
 
@@ -102,7 +106,6 @@
 mkdir('htmlwiki');
 foreach my $idx (keys(%idxres)) {
    my $targetfile=$fileres{$idx}.'.html';
-   print $idxres{$idx}." ---> ".$resdir{$idxres{$idx}}."\n";
    my ($dir1,$dir2)=split(/\//,$resdir{$idxres{$idx}});
    my $targetdir='htmlwiki'; 
    unless ($dir1) { $dir1='reference'; }
@@ -112,10 +115,59 @@
       $targetdir.='/'.$dir2;
       mkdir($targetdir);
     }
-#   open(IN,'dump/'.$idx.'.wikitext');
-#   while (my $line=<IN>) {
-#      chomp($line);
-#   }
-#   close(IN);
+    open(IN,'dump/'.$idx.'.wikitext');
+    print "====== Processing $idx: $idxres{$idx}\n";
+    while (my $line=<IN>) {
+       chomp($line);
+       if ($line=~/\[\[([^\]\|\#]+)/) {
+          my $ref=$1;
+          &rellink($targetdir,$ref);
+       }
+    }
+    close(IN);
 }
 
+sub rellink {
+   my ($srcdir,$ref)=@_;
+   my ($dir0,$dir1,$dir2)=split(/\//,$srcdir);
+   $ref=~s/\_/ /g;
+   $ref=~s/^[^\w\?\(]+//gs;
+   $ref=~s/[^\w\?\)]+$//gs;
+   $ref=~s/\%27/\'/g;
+   my $filename;
+   my $filedir;
+   if ($ref=~/^file\:\s*(.+)$/i) {
+      my $link=$1;
+      my $returnref;
+      my $returndir;
+      if ($filelink{$link}) {
+         $returnref=$filelink{$link};
+         $returndir=$filedir{$link};
+      } else {
+         unless (-e 'dump/'.$link) {
+            print "WARNING: [$ref] ($link) does not exist\n";
+            return '';
+         }
+         my $filename=$link;
+         $filename=~s/\s+/\_/g;
+         my ($fileb,$ext)=($filename=~/^(.+)\.(\w+)$/);
+         $ext=lc($ext);
+         $fileb=~s/\W//;
+         $filename=$fileb.'.'.$ext;
+         $filedir=$srcdir.'/files';
+         mkdir($filedir);
+         copy('dump/'.$link,$filedir.'/'.$filename);
+     }
+   } elsif ($ref=~/^wikipedia\:(.+)$/i) {
+      $ref=~s/wikipedia\:/http\:en.wikipedia.org\//;
+      return $ref;
+   } elsif ($ref=~/^http\:(.+)$/i) {
+      return $ref;
+   } else {
+       my $idx=$residx{$ref};
+       unless ($idx) {
+          print "WARNING: Unknown index for [$ref]\n";
+          return '';
+       } 
+   }
+}




More information about the LON-CAPA-cvs mailing list