summaryrefslogtreecommitdiff
path: root/incantesimi/excel_to_incantesimi.pl
blob: 674d0b1e26ac39d01d111c83c6b3a781af7e0d2b (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl 
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use LaTeX::Encode;
use Template;
use Data::Printer;
 
# excel => latex 
my %fieldmap = (
    spellname => 'name',
    name => 'name',
    level => 'level',
    school => 'scuola',
    comp => 'components',
    ct => 'tempo',
    range => 'range',
    target => 'target',
    duration => 'durata',
    st => 'st',
    sr => 'sr',
    description => '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;
 
sheet_2_latex(
    sheet_name => 'Incantesimi',
    output_filename => "${output_filename}.incantesimi.tex",
);
sheet_2_latex(
    sheet_name => 'Domini',
    sheet_idx => 1,
    output_filename => "${output_filename}.domini.tex",
    skip_col => 1,
);
 
{
my $template;
sub template {
    $template //do {local $/;<DATA>};
    return $template;
}
}
 
sub sheet_2_latex {
    my %opts = (
        sheet_idx => 0,
        fieldmap => \%fieldmap,
        @_,
    );
 
    my $spells = parse_table(
        $wb->worksheet($opts{sheet_name})
            // $wb->worksheet($opts{sheet_idx}),
        $opts{skip_col},
    );
 
    munge($spells);
 
    my $template = Template->new();
    $template->process(
        \template(),
        {
            spells => $spells,
            fieldmap => $opts{fieldmap},
            lt => \&latexise,
        },
        $opts{output_filename},
    )
    or die $template->error;
}
 
sub parse_table {
    my ($ws,$skip_col) = @_;
    $skip_col //= 0;
 
    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{^spell\W}{}i;
        $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+$skip_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}