Personal tools




ExamineBraces.pl

From OrganicDesign Wiki

Jump to: navigation, search
#!/usr/bin/perl
#
# Does the same as examineBraces() in MediaWiki Code Snippets
#
 
@braces = examineBraces('foo{{#bar:baz|biz
 
Performed by anyone. A need for an article is assessed, and collaboration on overall purpose and goals etc are established. Essentially a section zero summary is created.

}}[[:Template:R2:]][[:Template:Moo]]baz');   print "$_->{NAME}: $_->{OFFSET}, $_->{LENGTH}, $_->{DEPTH}\n" for @braces;   use Data::Dumper; print Dumper(@braces);   sub examineBraces {

       my $content = shift;
       my @braces  = ();
       my @depths  = ();
       my $depth   = 0;
       while ($content =~ m/\G.*?(\{\{\s*([#a-z0-9_]*:?)|\}\})/sig) {
               my $offset = pos($content)-length($2)-2;
               if ($1 eq '}}') {
                       $brace = $braces[$depths[$depth-1]];
                       $$brace{LENGTH} = $offset-$$brace{OFFSET}+2;
                       $$brace{DEPTH}  = $depth--;
               } else {
                       push @braces, { NAME => $2, OFFSET => $offset };
                       $depths[$depth++] = $#braces;
               }
       }
       return @braces;
}

The GNU Project Debian Linux Ubuntu Linux Wikipedia Affiliate Button MediaWiki