<?xml version="1.0" encoding="windows-1252"?>
<node id="604786" title="Re: nested level section closing" created="2007-03-14 07:50:52" updated="2007-03-14 03:50:52">
<type id="11">
note</type>
<author id="460657">
Samy_rio</author>
<data>
<field name="doctext">
&lt;p&gt;Hi [Anonymous Monk], try like this using Regular Expression,&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

my $input = do{local $/; &lt;DATA&gt;};

$input =~ s/(&lt;(h\d+&gt;)[^\n]+)/$1&lt;\/$2/gsi;
$input =~ s/(&lt;h(\d+)[^&gt;]*&gt;)(.*?)(?=(&lt;h(\d+)[^&gt;]*&gt;))/"$1".&amp;section_close($2,$3,$5)/egsi;
$input =~ s/(&lt;(h\d+&gt;)){2,}/$1/gsi;

######Last level
if ($input =~/&lt;h./si)
{
	if ($input =~/(&lt;h\d+[^&gt;]*&gt;)(.*)(&lt;h(\d+)[^&gt;]*&gt;)(.*)$/si)
	{
		$input =~s/(&lt;h\d+[^&gt;]*&gt;)(.*)(&lt;h(\d+)[^&gt;]*&gt;)(.*)$/"$1$2$3".&amp;section_close($4,$5,1)/egsi;		
	}
	else 
	{	
		$input =~s/(.*)(&lt;h(\d+)[^&gt;]*&gt;)(.*)$/"$1$2".&amp;section_close($3,$4,1)/egsi;		
	}
}

############Heading replacement
$input =~ s/(&lt;h(\d+)&gt;)((?:(?!&lt;\/h\2&gt;).)*)&lt;\/h\2&gt;/$1&lt;head&gt;$3&lt;\/head&gt;/gsi;
$input =~ s/(&lt;\/?)h(\d+&gt;)/$1section$2/gsi;
	
print $input;

sub section_close
{
	my ($csect_no,$aft_txt,$asect_no)=@_;
	
	my $tag_close;
		
	if ($csect_no == $asect_no)
	{
		$tag_close="$aft_txt&lt;\/h$csect_no&gt;\n" 
	}	
	if ($csect_no &lt; $asect_no)
	{
		my $j = $asect_no - $csect_no;
		my $i = $csect_no;
		my $temp = "";
		while ($j &gt; 1)
		{
			my $k = $i + 1;
			$temp = $temp."&lt;h$k&gt;\n";			
			$i++;
			$j--;
		}
		
		$tag_close = "&lt;h$csect_no&gt;".$aft_txt.$temp;		
	}

	if ($csect_no &gt; $asect_no) #head separation
	{
		my $temp = "";
		my $i = $asect_no;
		for ($i = $asect_no; $i &lt;= $csect_no; $i++)
		{
			$temp = "&lt;\/h$i&gt;\n".$temp;
		}
		$tag_close = $aft_txt.$temp;
		
		
	}	
	
	return $tag_close;
	
}

__DATA__
&lt;h1&gt;Heading level 1
&lt;h2&gt;Heading level 2
&lt;h3&gt;Heading level 3
&lt;h2&gt;Heading level 2
&lt;h1&gt;Heading level 1
paragraph here
paragraph here

Output:
-------
&lt;section1&gt;&lt;head&gt;Heading level 1&lt;/head&gt;
&lt;section2&gt;&lt;head&gt;Heading level 2&lt;/head&gt;
&lt;section3&gt;&lt;head&gt;Heading level 3&lt;/head&gt;
&lt;/section3&gt;
&lt;/section2&gt;
&lt;section2&gt;&lt;head&gt;Heading level 2&lt;/head&gt;
&lt;/section2&gt;
&lt;/section1&gt;
&lt;section1&gt;&lt;head&gt;Heading level 1&lt;/head&gt;
paragraph here
paragraph here
&lt;/section1&gt;
&lt;/code&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-460657"&gt;
&lt;p&gt;Regards,&lt;br&gt;Velusamy R.&lt;hr&gt;&lt;c&gt;eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';&lt;/c&gt;&lt;/p&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
604767</field>
<field name="parent_node">
604767</field>
</data>
</node>
