#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $text = << 'EOF';
{{Uniwersalny szablon nawigacyjny|SZABLON=Wielokąty
|TYTUŁ ='''[[Wielokąt]]y'''
|OBRAZEK =
|KOLOR_TYTUŁ_TŁO = #eeeeee
|KOLOR_TŁO = #ffffff
|ZAWARTOŚĆ=
'''[[trójkąt]]y'''
[[trójkąt prostokątny]] • [[trójkąt równoboczny]] • [[trójkąt równoramienny]]
'''[[czworokąt]]y'''
[[trapez]] • [[trapez prostokątny]] • [[trapez równoramienny]] • [[deltoid]] • [[równoległobok]] • [[romb]] • [[prostokąt]] • [[kwadrat]]
'''pozostałe'''
[[pięciokąt]] • [[sześciokąt]] • [[ośmiokąt]] • [[wielokąt gwiaździsty]]
'''[[wielokąt foremny|wielokąty foremne]]'''
[[trójkąt równoboczny]] • [[kwadrat (geometria)|kwadrat]] • [[pięciokąt foremny]] • [[sześciokąt foremny]] • [[siedmiokąt foremny]] • [[ośmiokąt foremny]] • [[dziewięciokąt foremny]] • [[dziesięciokąt foremny]]
}}
EOF
sub extract_templates($)
{
my $data = shift @_;
my $open = '{{';
my $close = '}}';
my @templates = $data =~ /$open((?:[^\{\}]*(?:$open.*?$close)?)*)$close/og;
my @results;
while (scalar(@templates))
{
my $template = shift @templates;
my %infobox;
my ($name, $content) = $template =~ /^\s*(.+?)\s*(?:\|(.*?)|)$/s;
$infobox{'name'} = $name;
$infobox{'fields'} = ();
$infobox{'args'} = ();
# my @items = $content =~ /((?:[^\|\{\}\[\]]+(?:\{\{[^\{\}]*\}\}|\[{1,2}[^\[\]]*\]{1,2}|))+)(?:\||$)/sg;
my @items = $content =~ /((?:[^\|\{\}\[\]]*(?:\{\{[^\{\}]*\}\}|\[{1,2}[^\[\]]*\]{1,2}|))+)(?:\||$)/sg;
while (scalar(@items))
{
my $item = shift @items;
# if ($item =~ /^\s*([^= ]+)\s*=[ \t]*(.*?)\s*$/s)
if ($item =~ /^\s*([^=]+?)\s*=[ \t]*(.*?)\s*$/s)
{
$infobox{'fields'}{$1} = $2;
}
else
{
push @{ $infobox{'args'} }, $item;
}
}
push @results, \%infobox;
}
return @results;
}
print Dumper extract_templates($text);
my $i = 0;
$text =~ s/'''(.+?)'''\s*
\s+(.+?)\s+/ $i++; "|grupa$i = $1\n|lista$i = $2\n"/ge;
print $text;