Created
November 22, 2008 12:41
-
-
Save marcusramberg/27814 to your computer and use it in GitHub Desktop.
Revisions
-
dandv created this gist
Nov 22, 2008 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ #! perl -w use strict; my $content = '=toc<br /> =toc 2-4<br /> <p>=toc 2</p> <div id="stff">=toc -3<br /> <p>=toc 2-</p> '; my ($toc_h_min, $toc_h_max); my $toc_params_RE = qr/\s+ (\d*)(?{$toc_h_min = $^N}) \s* - \s* (\d*)(?{$toc_h_max = $^N})/x; while ( # replace the =toc markup tag if it's on a line of its own (<p>), or not (followed by <br />) $content =~ s{ <p>=toc(?:$toc_params_RE)? \s* </p> | (?:<=>)?=toc(?:$toc_params_RE)? \s* (?=<br) }{<!--mojomojoTOCwillgohere-->}x) { $toc_h_min ||= 1, $toc_h_max ||= 12; # FIXME: Perl 5.10 has a regexp branch reset operator which simplifies this #if ($1) { # # matched the <p> branch # $toc_h_min = $2 || 1; # $toc_h_max = $4 || 12; #} else { # # matched the <br branch # $toc_h_min = $4 || 1; # $toc_h_max = $5 || 12; # 6 is the HTML max, but hey, knock yourself out #} print "$toc_h_min - $toc_h_max\n"; }