Index: trunk/gsdl/perllib/plugins/HTMLPlug.pm
===================================================================
--- trunk/gsdl/perllib/plugins/HTMLPlug.pm	(revision 10118)
+++ trunk/gsdl/perllib/plugins/HTMLPlug.pm	(revision 10121)
@@ -96,5 +96,8 @@
 	'type' => "string",
 	'deft' => "",
-	'reqd' => "no"}
+	'reqd' => "no"},
+      { 'name' => "sectionalise_using_h_tags",
+	'desc' => "{HTMLPlug.sectionalise_using_h_tags}",
+	'type' => "flag" }
       ];
 
@@ -126,4 +129,5 @@
 			 q^description_tags^, \$self->{'description_tags'},
 			 q^no_strip_metadata_html/.*/^, \$self->{'no_strip_metadata_html'},
+			 q^sectionalise_using_h_tags^, \$self->{'sectionalise_using_h_tags'},
 			 "allow_extra_options")) {
 
@@ -228,4 +232,27 @@
     $self->{'dir_num'} = 0;
     $self->{'file_num'} = 0;
+
+    # process an HTML file where sections are divided by headings tags (H1, H2 ...)
+    # you can also include metadata in the format (X can be any number)
+    # <hX>Title<!--gsdl-metadata
+    #	<Metadata name="name1">value1</Metadata>
+    #	...
+    #	<Metadata name="nameN">valueN</Metadata>
+    #--></hX>
+    if ($self->{'sectionalise_using_h_tags'}) {
+	# description_tags should allways be activated because we convert headings to description tags
+	$self->{'description_tags'} = 1;
+
+	my $arrSections = [];
+	$$textref =~ s/<h([0-9]+)[^>]*>(.*?)<\/h[0-9]+>/$self->process_heading($1, $2, $arrSections, $file)/isge;
+
+	if (scalar(@$arrSections)) {
+	    my $strMetadata = $self->update_section_data($arrSections, -1);
+	    if (length($strMetadata)) {
+		$strMetadata = '<!--' . $strMetadata . "\n-->\n</body>";
+		$$textref =~ s/<\/body>/$strMetadata/ig;
+	    }
+	}
+    }
 
     my $cursection = $doc_obj->get_top_section();
@@ -389,4 +416,60 @@
 }
 
+
+sub process_heading
+{
+    my ($self, $nHeadNo, $strHeadingText, $arrSections, $file) = @_;
+    $strHeadingText = '' if (!defined($strHeadingText));
+
+    my $strMetadata = $self->update_section_data($arrSections, int($nHeadNo));
+
+    my $strSecMetadata = '';
+    while ($strHeadingText =~ s/<!--gsdl-metadata(.*?)-->//is)
+    {
+	$strSecMetadata .= $1;
+    }
+
+    $strHeadingText =~ s/^\s+//g;
+    $strHeadingText =~ s/\s+$//g;
+    $strSecMetadata =~ s/^\s+//g;
+    $strSecMetadata =~ s/\s+$//g;
+
+    $strMetadata .= "\n<Section>\n\t<Description>\n\t\t<Metadata name=\"Title\">" . $strHeadingText . "</Metadata>\n";
+
+    if (length($strSecMetadata)) {
+	$strMetadata .= "\t\t" . $strSecMetadata . "\n";
+    }
+
+    $strMetadata .= "\t</Description>\n";
+
+    return "<!--" . $strMetadata . "-->";
+}
+
+
+sub update_section_data
+{
+    my ($self, $arrSections, $nCurTocNo) = @_;
+    my ($strBuffer, $nLast, $nSections) = ('', 0, scalar(@$arrSections));
+
+    if ($nSections == 0) {
+	push @$arrSections, $nCurTocNo;
+	return $strBuffer;
+    }
+    $nLast = $arrSections->[$nSections - 1];
+    if ($nCurTocNo > $nLast) {
+	push @$arrSections, $nCurTocNo;
+	return $strBuffer;
+    }
+    for(my $i = $nSections - 1; $i >= 0; $i--) {
+	if ($nCurTocNo <= $arrSections->[$i]) {
+	    $strBuffer .= "\n</Section>";
+	    pop @$arrSections;
+	}
+    }
+    push @$arrSections, $nCurTocNo;
+    return $strBuffer;
+}
+
+
 # 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
Index: trunk/gsdl/perllib/strings.rb
===================================================================
--- trunk/gsdl/perllib/strings.rb	(revision 10118)
+++ trunk/gsdl/perllib/strings.rb	(revision 10121)
@@ -647,4 +647,6 @@
 HTMLPlug.rename_assoc_files:Renames files associated with documents (e.g. images). Also creates much shallower directory structure (useful when creating collections to go on cd-rom).
 
+HTMLPlug.sectionalise_using_h_tags:Automatically create a sectioned document using h1, h2, ... hX tags.
+
 HTMLPlug.title_sub:Substitution expression to modify string stored as Title. Used by, for example, PDFPlug to remove "Page 1", etc from text used as the title.
 
