Index: /main/trunk/greenstone2/perllib/plugins/StructuredHTMLPlugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/StructuredHTMLPlugin.pm	(revision 33298)
+++ /main/trunk/greenstone2/perllib/plugins/StructuredHTMLPlugin.pm	(revision 33299)
@@ -159,5 +159,5 @@
     $body_text =~ s/(<p[^>]*><o:p>&nbsp;<\/o:p><\/p>)//isg;
     
-    # what was the following line for. effectively unused. do we need it??
+	# what was the following line for. effectively unused. do we need it??
     #$section_text .= "<!--\n<Section>\n-->\n";
     #my $top_section_tag = "<!--\n<Section>\n-->\n";
@@ -179,72 +179,84 @@
     foreach $hc ( @h_split )
     {
-	if ($hc =~ m/^([1-3])\s*.*?>(.*)$/s)
-	{
-	    my $new_hnum = $1;
-	    my $hc_after = $2;
-	    
-	    if ($hc_after =~ m/^(.*?)<\/h$new_hnum>/is)
-	    {
-		my $h_text = $1;
-		$hc =~ s/^(\&nbsp\;)+/\&nbsp\;/g;
-		# boil HTML down to some interesting text
-		$h_text =~ s/^[1-3]>//;
-		$h_text =~ s/<\/?.*?>//sg;
-		$h_text =~ s/\s+/ /sg;
-		$h_text =~ s/^\s$//s;
-		$h_text =~ s/(&nbsp;)+\W*/&nbsp;/sg;
-		
-		if ($h_text =~ m/\w+/)
+		if ($hc =~ m/^([1-3])\s*.*?>(.*)$/s)
 		{
-		    if ($new_hnum > $hnum)
-		    {
-			# increase section nesting
-			$hnum++;
-			while ($hnum < $new_hnum)
+			my $new_hnum = $1;
+			my $hc_after = $2;
+			my $filtered_hc = undef;
+			
+			if ($hc_after =~ m/^(.*?)<\/h$new_hnum>(.*)$/is)
 			{
-			    my $spacing = "  " x $hnum;
-			    $section_text .= "<!--\n";
-			    $section_text .= $spacing."<Section>\n";
-			    $section_text .= "-->\n";
-			    $hnum++;
+				my $h_text = $1;
+				$filtered_hc = $2; # This represents the remainder of $hc, after the (e.g.) <h2>xxxx</h2> element
+				
+				$hc =~ s/^(\&nbsp\;)+/\&nbsp\;/g;
+				# boil HTML down to some interesting text
+				$h_text =~ s/^[1-3]>//;
+				$h_text =~ s/<\/?.*?>//sg;
+				$h_text =~ s/\s+/ /sg;
+				$h_text =~ s/^\s$//s;
+				$h_text =~ s/(&nbsp;)+\W*/&nbsp;/sg;
+				
+				if ($h_text =~ m/\w+/)
+				{
+					if ($new_hnum > $hnum)
+					{
+					# increase section nesting
+					$hnum++;
+					while ($hnum < $new_hnum)
+					{
+						my $spacing = "  " x $hnum;
+						$section_text .= "<!--\n";
+						$section_text .= $spacing."<Section>\n";
+						$section_text .= "-->\n";
+						$hnum++;
+					}
+					}
+					else # ($new_hnum <= $hnum)
+					{
+					# descrease section nesting
+					while ($hnum >= $new_hnum)
+					{
+						my $spacing = "  " x $hnum;
+						$section_text .= "<!--\n";
+						$section_text .= $spacing."</Section>\n";
+						$section_text .= "-->\n";
+						$hnum--;
+					}
+					$hnum++;
+					}
+
+					my $spacing = "  " x $hnum;
+					$section_text .= "<!--\n";
+					$section_text .= $spacing."<Section>\n";
+					$section_text .= $spacing."  <Description>\n";
+					$section_text .= $spacing."    <Metadata name=\"Title\">$h_text</Metadata>";
+					$section_text .= $spacing."  </Description>\n";
+					$section_text .= "-->\n";
+					
+					#print $outhandle $spacing."$h_text\n"
+					#	if $self->{'verbosity'} > 2;
+					
+					$sectionh1++ if ($hnum==1);
+				}
 			}
-		    }
-		    else # ($new_hnum <= $hnum)
-		    {
-			# descrease section nesting
-			while ($hnum >= $new_hnum)
-			{
-			    my $spacing = "  " x $hnum;
-			    $section_text .= "<!--\n";
-			    $section_text .= $spacing."</Section>\n";
-			    $section_text .= "-->\n";
-			    $hnum--;
+			else {
+###			print STDERR "***** hc = <h$hc\n\n";
 			}
-			$hnum++;
-		    }
-
-		    my $spacing = "  " x $hnum;
-		    $section_text .= "<!--\n";
-		    $section_text .= $spacing."<Section>\n";
-		    $section_text .= $spacing."  <Description>\n";
-		    $section_text .= $spacing."    <Metadata name=\"Title\">$h_text</Metadata>";
-		    $section_text .= $spacing."  </Description>\n";
-		    $section_text .= "-->\n";
-		    
-		    #print $outhandle $spacing."$h_text\n"
-		    #	if $self->{'verbosity'} > 2;
-		    
-		    $sectionh1++ if ($hnum==1);
+			
+			# This can probably be replaced by the first statement in the if-statement, because
+			# $filtered_hc should always be defined when it is assigned above (even if it resolves
+			# to be an empty string)
+			if (defined $filtered_hc) {
+				$section_text .= $filtered_hc;
+			}
+			else {
+				$section_text .= "<h$hc";
+			}
 		}
-	    }
-	    else {
-###		print STDERR "***** hc = <h$hc\n\n";
-	    }
-	    $section_text .= "<h$hc";
-	}
-	else
-	{
-	    $section_text .= "<h$hc";
-	}
+		else
+		{
+			$section_text .= "<h$hc";
+		}
     }
 
Index: /main/trunk/greenstone2/perllib/unicode.pm
===================================================================
--- /main/trunk/greenstone2/perllib/unicode.pm	(revision 33298)
+++ /main/trunk/greenstone2/perllib/unicode.pm	(revision 33299)
@@ -765,4 +765,5 @@
 }
 
+# perl version of stringToHex
 # Useful method to print UTF8 (or other unicode) for debugging.
 # Characters that are easily displayed (that is, printable ASCII) 
