Index: trunk/gsdl/bin/script/convert_toc.pl
===================================================================
--- trunk/gsdl/bin/script/convert_toc.pl	(revision 3053)
+++ trunk/gsdl/bin/script/convert_toc.pl	(revision 3081)
@@ -4,59 +4,61 @@
 # <Section> syntax (suitable for processing by HTMLPlug -description_tags
 
-my $line = "";
-my $foundbody = 0;
 my $level = 0;
-while (defined ($line = <STDIN>)) {
 
-    $line =~ s/[\cJ\cM]+$//;
-    if (!$foundbody) {
-	print STDOUT "$line\n";
-	$foundbody = 1 if ($line =~ /<body/i);
-	next;
+sub main {
+  open STDIN, "<$ARGV[0]" or die "$ARGV[0]: $!\n";
+  open STDOUT, ">$ARGV[1]" or die "$ARGV[1]: $!\n";
+
+  my $content = "";
+  
+  while (<STDIN>) {
+    $_ =~ s/[\cJ\cM]+$//;
+    $content .= $_;
+  }
+
+  # fix images
+  $content =~ s/&lt;&lt;I&gt;&gt;\s*(\w+\.(?:png|jpe?g|gif))\s*(.*?)<\/p>/<center><img src=\"$1\"><br>$2<\/center><\/p>\n/igs;
+  
+  # process section title
+  while ($content =~ s/(?:<P[^>]*>|<BR>)(?:\s*<\/?[^>]*>)*?&lt;&lt;TOC(\d+)&gt;&gt;(.*?)(?:\s*<\/?[^>]*>)*?\s*(<\/P[^>]*>|<BR>)/process_toc ($1, $2)/sige) {}
+  
+  # close the remaining sections
+  my $section_ending = "<!--\n";
+  for (my $j = 0; $j < $level; $j++) {
+    $section_ending .= "</Section>\n";
+  }
+  $section_ending .= "-->\n";
+  $content =~ s/(<\/body>)/$section_ending.$1/ie;
+
+  print STDOUT $content;
+  close STDOUT; close STDIN;
+}
+
+sub process_toc {
+  my ($thislevel, $title) = @_;
+  my $toc = '';
+ 
+  $title =~ s/\n/ /g;
+  $title =~ s/<[^>]+>//g;
+  $title =~ s/^\s+//;
+  $title =~ s/\s+$//;
+
+  $toc .= "\n<!--\n";
+
+  if ($thislevel <= $level) {
+    for (my $i = 0; $i < ($level-$thislevel+1); $i++) {
+	$toc .= "</Section>\n";
     }
+  }
+  $toc .= "<Section>\n".
+	"  <Description>\n".
+	"    <Metadata name=\"Title\">$title</Metadata>\n".
+	"  </Description>\n".
+	"-->\n";
 
-    # sort out the <<I>> image tags
-    $line =~ s/&lt;&lt;I&gt;&gt;\s*(\w+\.(?:png|jpe?g|gif))/<center><img src=\"$1\"><\/center><br>\n/igs;
+  $level = $thislevel;
+  
+  return $toc;
+}
 
-    if ($line =~ /&lt;&lt;TOC(\d+)&gt;&gt;(.*)/) {
-	my $thislevel = $1;
-	my $title = $2;
-	$title =~ s/<[^>]+>//g;
-	$title =~ s/^\s+//;
-	$title =~ s/\s+$//;
-
-	# print out any tags that were on the line
-	while ($line =~ s/(<[^>]+>)//) {
-	    print STDOUT $1;
-	}
-
-	print STDOUT "\n<!--\n";
-
-	if ($thislevel <= $level) {
-	    for (my $i = 0; $i < ($level-$thislevel+1); $i++) {
-		print STDOUT "</Section>\n";
-	    }
-	}
-	print STDOUT "<Section>\n";
-	print STDOUT "  <Description>\n";
-	print STDOUT "    <Metadata name=\"Title\">$title</Metadata>\n";
-	print STDOUT "  </Description>\n";
-	print STDOUT "-->\n";
-
-	$level = $thislevel;
-
-    } elsif ($line =~ /<\/body>/i) {
-	$line =~ /^(.*?)(<\/body>.*)$/i;
-	print STDOUT $1 if defined $1;
-	print STDOUT "\n<!--\n";
-	for (my $j = 0; $j < $level; $j++) {
-	    print STDOUT "</Section>\n";
-	}
-	print STDOUT "-->\n";
-	print STDOUT $2 if defined $2;
-	print STDOUT "\n";
-
-    } else {
-	print STDOUT "$line\n";
-    }
-}
+&main;
Index: trunk/gsdl/bin/script/convert_toc_dir.pl
===================================================================
--- trunk/gsdl/bin/script/convert_toc_dir.pl	(revision 3053)
+++ trunk/gsdl/bin/script/convert_toc_dir.pl	(revision 3081)
@@ -3,4 +3,14 @@
 # run convert_toc.pl on any html files found in the directory passed in on
 # the command line (or any subdirs).
+
+die "\$GSDLOS not set !\n" if (!defined $ENV{GSDLOS});
+my ($move, $separator);
+if ($ENV{GSDLOS} eq "windows") {
+  $move = "move";
+  $separator = "\\";
+} else {
+  $move = "mv";
+  $separator = "/";
+}
 
 &recurse($ARGV[0]);
@@ -20,11 +30,11 @@
     foreach my $file (@files) {
 	next if $file =~ /^\.\.?$/;
-	my $fullpath = "$dir/$file";
+	my $fullpath = "$dir$separator$file";
 	if (-d $fullpath) {
 	    &recurse($fullpath);
 	} elsif ($file =~ /\.html?$/i) {
 	    print STDERR "converting $fullpath\n";
-	    `convert_toc.pl < $fullpath > $fullpath.new`;
-	    `mv $fullpath.new $fullpath`;
+	    `convert_toc.pl $fullpath $fullpath.new`;
+	    `$move $fullpath.new $fullpath`;
 	}
     }
