rendered paste body#!/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'''<br>
[[trójkąt prostokątny]] • [[trójkąt równoboczny]] • [[trójkąt równoramienny]]
'''[[czworokąt]]y'''<br>
[[trapez]] • [[trapez prostokątny]] • [[trapez równoramienny]] • [[deltoid]] • [[równoległobok]] • [[romb]] • [[prostokąt]] • [[kwadrat]]
'''pozostałe'''<br>
[[pięciokąt]] • [[sześciokąt]] • [[ośmiokąt]] • [[wielokąt gwiaździsty]]
'''[[wielokąt foremny|wielokąty foremne]]'''<br>
[[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;
}
my $i = 0;
$text =~ s/'''(.+?)'''\s*<br ?\/?>\s+(.+?)\s+/ $i++; "|grupa$i = $1\n|lista$i = $2\n"/ge;
my @templates = extract_templates($text);
print Dumper @templates;
sub rebuild_templates {
my $result = '';
foreach my $item (@_) {
$result .= "{{$item->{name}\n";
foreach my $field (keys %{ $item->{fields} }) {
$result .= "| $field = $item->{fields}->{$field}\n";
}
my $i = 1;
foreach my $arg (@{ $item->{args} }) {
$result .= "| $i = $arg\n";
$i++;
}
}
return $result;
}
print rebuild_templates(@templates);