Headers

Michel Fortin michel.fortin at michelf.com
Thu Feb 3 13:32:19 EST 2005


>     $text =~ s/(<?\/h)([1-4])(>)/$1($2+$headaddition)$3/g;
>
> Does that look like it will work?

It seems to me that it will work. May I suggest the ShiftHeaders 
function I made a while ago for exactly this purpose. It takes two 
arguments: the HTML text and the new maximum header level.

In Perl:

	sub ShiftHeaders {
		my $text = shift;
		my $level = shift;
		$level--;
	
		if ($level > 0) {
			$text =~ s{(</?h)([1-6])(>|\\s)}{
				$1 . ($2+$level >= 6 ? 6 : $2+$level) . $3
			}egi;
		}
		$text;
	}

In PHP:

	function ShiftHeaders($text, $level) {
		$level--;
	
		if ($level > 0) {
			$text = preg_replace(
					"!(</?h)([1-6])(>|\\s)!ie",
					"'\\1'.(\\2+$level >= 6 ? 6 : \\2+$level).'\\3'",
					$text);
		}
		return $text;
	}

Use like this (in both languages):

	$text = ShiftHeaders($text, 3); # H1 => H3, H2 => H4, etc.


Michel Fortin
michel.fortin at michelf.com
http://www.michelf.com/



More information about the Markdown-Discuss mailing list