User:Ilmari Karonen/ahnentafel.pl

From Wikipedia, the free encyclopedia

This is the perl script I used to make the unoptimized version of the Ahnentafel templates ({{Ahnentafel2}}, {{Ahnentafel3}}, {{Ahnentafel4}}, {{Ahnentafel5}}, {{Ahnentafel6}}). See User:Ilmari Karonen/ahnentafel2.pl for a version that produces much more efficient (but much less readable) output.


#!/usr/bin/perl -w
use strict;
use integer;

my $depth = shift or die "Usage: $0 <depth>\n";

print "{{familytree/start|style={{{style|}}}}}\n";

for my $row ( 1 .. (1<<$depth)-1 ) {
    my $level = $depth;
    my $n = $row + (1<<$depth);
    $n >>= 1, $level-- until $n & 1;
    $n >>= 1;

    my $gray1 = ($row ^ ($row<<1));
    my $gray2 = ($row-1) ^ (($row-1)<<1) ^ 1;

    my @line;
    my $boxn = sprintf "BX%03d", $n;
    my $seen = 0;
    for my $i ( 0 .. $depth-1 ) {
        my $quad = (($gray1>>$i)&1) + ((($gray2>>$i)&1)<<1);
        push @line, (["| | | | ", "| | | |,", "| | | |`"]->[$quad] ||
                     ($seen++   ? "| | | |!": "|$boxn|("));
    }
    chop $line[0];
    print "{{familytree", reverse(@line), " border={{{border_$level|{{{border|2}}}}}} | boxstyle={{{boxstyle|}}};{{{boxstyle_$level|}}} | $boxn={{{$n}}} }}\n";
}

print "{{familytree/end}}\n";

__END__