summaryrefslogtreecommitdiff
path: root/incantesimi/excel_to_incantesimi.pl
blob: 95e0c95fe2dcba4f3f4eb9791e568e88972ea8fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl 
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use LaTeX::Encode;
use Template;
use Data::Printer;
 
# excel => latex 
my %fieldmap = (
    spellname => 'name',
    level => 'level',
    school => 'scuola',
    comp => 'components',
    ct => 'tempo',
    range => 'range',
    target => 'target',
    duration => 'durata',
    st => 'st',
    sr => 'sr',
    spell => 'descrizione',
    special => 'specialcomp',
    page => 'ref',
    notes => 'note',
);
 
my ($input_filename,$output_filename)=@ARGV;
 
die "$0 \$input \$output\n"
    unless $input_filename && $output_filename;
 
my $parser = Spreadsheet::ParseExcel->new();
my $wb = $parser->parse($input_filename)
    or die $parser->error;
 
my $spells = parse_table($wb->worksheet('Incantesimi')
                             // $wb->worksheet(0));
 
munge($spells);
$spells;
my $template = Template->new();
$template->process(
    \*DATA,
    {
        spells => $spells,
        fieldmap => \%fieldmap,
        lt => \&latexise,
    },
    $output_filename,
)
    or die $template->error;
 
sub parse_table {
    my ($ws) = @_;
 
    my ($min_col,$max_col) = $ws->col_range();
    my ($min_row,$max_row) = $ws->row_range();
    my @headings = map { eval { $ws->get_cell($min_row,$_)->value } }
        $min_col .. $max_col;
 
    for my $h (@headings) {
        $h =~ s{\W.*$}{};
        $h = lc($h);
    }
 
    my @ret = ();
 
    for my $row ((1+$min_row) .. $max_row) {
        # allow and skip empty rows 
        next unless eval { $ws->get_cell($row,$min_col)->value };
        my %record;
 
        keys @headings;
        while (my ($idx,$heading) = each @headings) {
            # allow and skip empty cols 
            next unless $heading;
            $record{$heading} =
                eval { $ws->get_cell($row,$idx+$min_col)->value } // '';
        }
        push @ret,\%record;
    }
    return \@ret;
}
 
sub munge {
    my ($spells) = @_;
 
    for my $spell (@$spells) {
        if ($spell->{page} =~ m{^\d+$}) {
            $spell->{page} = 'PHB'.$spell->{page};
        }
    }
    return;
}
 
sub latexise {
    my ($str) = @_;
 
    $str = latex_encode($str);
    $str =~ s{(?<!\\)\. }{.\\ }g;
 
    return $str;
}
 
__DATA__
\input{incantesimi.cmd}
\usepackage{textcomp}
 
\begin{document}
%
\begin{tikzpicture}
\matrix {
 
[% FOREACH spell IN spells %]
\incantesimo{type=spell,
[%- FOREACH field IN fieldmap; IF spell.item(field.key) != '' %]
    [% field.value %]={[% lt(spell.item(field.key)) %]},
[%- END; END %]
} [% IF (loop.count() % 6) %]& [% ELSE %] \\ [% END -%]
[% IF (loop.count() % 18) == 0 %]};
\end{tikzpicture}
 
\pagebreak
 
\begin{tikzpicture}
\matrix {
 
[% END; END;
   rest_in_row = spells.size % 6;
   FOREACH skip IN [ 1 .. rest_in_row ]; ' & '; END; ' \\\\ '
%]};
\end{tikzpicture}
\vspace{-10pt}
\end{document}