All pastes #911208 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-02-20 15:56 UTC
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]] &bull; [[trójkąt równoboczny]] &bull; [[trójkąt równoramienny]]

'''[[czworokąt]]y'''<br>
[[trapez]] &bull; [[trapez prostokątny]] &bull; [[trapez równoramienny]] &bull; [[deltoid]] &bull; [[równoległobok]] &bull; [[romb]] &bull; [[prostokąt]] &bull; [[kwadrat]]

'''pozostałe'''<br>
[[pięciokąt]] &bull; [[sześciokąt]] &bull; [[ośmiokąt]] &bull; [[wielokąt gwiaździsty]]

'''[[wielokąt foremny|wielokąty foremne]]'''<br>
[[trójkąt równoboczny]] &bull; [[kwadrat (geometria)|kwadrat]] &bull; [[pięciokąt foremny]] &bull; [[sześciokąt foremny]] &bull; [[siedmiokąt foremny]] &bull; [[ośmiokąt foremny]] &bull; [[dziewięciokąt foremny]] &bull; [[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);