Index: /trunk/gsdl/perllib/plugins/HTMLPlug.pm
===================================================================
--- /trunk/gsdl/perllib/plugins/HTMLPlug.pm	(revision 2816)
+++ /trunk/gsdl/perllib/plugins/HTMLPlug.pm	(revision 2817)
@@ -75,4 +75,8 @@
     print STDERR "                          Used by, for example, PDFHtml to remove Page 1 etc from text\n";
     print STDERR "                          chosen to be used as the title.\n";
+    print STDERR "   -description_tags      Split document into sub-sections where <Section> tags occur.\n";
+    print STDERR "                          Note that by setting this option you implicitly set -no_metadata\n";
+    print STDERR "                          as all metadata should be included within the <Section> tags.\n";
+    print STDERR "                          Also, -keep_head will have no effect when this option is set.\n";
 }
 
@@ -92,4 +96,5 @@
 			 q^rename_assoc_files^, \$self->{'rename_assoc_files'},
 			 q^title_sub/.*/^, \$self->{'title_sub'},
+			 q^description_tags^, \$self->{'description_tags'},
 			 "allow_extra_options")) {
 
@@ -139,5 +144,5 @@
 
     $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection) 
-	unless $self->{'no_metadata'};
+	unless $self->{'no_metadata'} || $self->{'description_tags'};
 
     # Store URL for page as metadata - this can be used for an
@@ -149,6 +154,71 @@
     $doc_obj->add_utf8_metadata($cursection, "URL", $web_url);
 
+    if ($self->{'description_tags'}) {
+
+	my $found_something = 0; my $top = 1;
+	while ($$textref =~ s/^(.*?)<!--(.*?)-->//s) {
+	    my $text = $1;
+	    my $comment = $2;
+	    if (defined $text) {
+		$self->process_section(\$text, $base_dir, $file, $doc_obj, $cursection);
+	    }
+	    while ($comment =~ s/<([^>]+)>//s) {
+		my $tag = $1;
+		if ($tag eq "Section") {
+		    $found_something = 1;
+		    $cursection = $doc_obj->insert_section($doc_obj->get_end_child($cursection)) unless $top;
+		    $top = 0;
+		} elsif ($tag eq "/Section") {
+		    $found_something = 1;
+		    $cursection = $doc_obj->get_parent_section ($cursection);
+		} elsif ($tag =~ /^Metadata name=\"([^\"]+)\"/s) {
+		    my $metaname = $1;
+		    $comment =~ s/^(.*?)<\/Metadata>//s;
+		    my $metavalue = $1;
+		    $metavalue =~ s/^\s+//;
+		    $metavalue =~ s/\s+$//;
+		    $doc_obj->set_utf8_metadata_element($cursection, $metaname, $metavalue);
+		}
+	    }
+	}
+	if ($cursection ne "") {
+	    print $outhandle "HTMLPlug: WARNING: $file contains unmatched <Section></Section> tags\n";
+	}
+
+	$$textref =~ s/^.*?<body[^>]*>//is;
+	$$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
+	if ($$textref =~ /\S/) {
+	    if (!$found_something) {
+		print $outhandle "HTMLPlug: WARNING: $file appears to contain no Section tags so\n";
+		print $outhandle "          will be processed as a single section document\n";
+		$self->process_section($$textref, $base_dir, $file, $doc_obj, $cursection);
+	    } else {
+		print $outhandle "HTMLPlug: WARNING: $file contains the following text outside\n";
+		print $outhandle "          of the final closing </Section> tag. This text will\n";
+		print $outhandle "          be ignored.";
+		if (length($$textref) > 30) {
+		    $text = substr($$textref, 0, 30) . "...";
+		}
+		$text =~ s/\n/ /isg;
+		print $outhandle " ($text)\n";
+	    }
+	}
+	
+    } else {
+	# single section document
+	$self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
+    }
+    return 1;
+}
+
+# note that process_section may be called multiple times for a single
+# section (relying on the fact that add_utf8_text appends the text to any
+# that may exist already).
+sub process_section {
+    my $self = shift (@_);
+    my ($textref, $base_dir, $file, $doc_obj, $cursection) = @_;
+
     # remove header and footer
-    if (!$self->{'keep_head'}) {
+    if (!$self->{'keep_head'} || $self->{'description_tags'}) {
 	$$textref =~ s/^.*?<body[^>]*>//is;
 	$$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
@@ -174,10 +244,5 @@
     # add text to document object
     $doc_obj->add_utf8_text($cursection, $$textref);
-
-    return 1;
-}
-
-
-
+}
 
 sub replace_images {
