| 1 | package Yukki::Web::Plugin::Spreadsheet; | ||
| 2 | $Yukki::Web::Plugin::Spreadsheet::VERSION = '0.991_002'; # TRIAL | ||
| 3 | |||
| 4 | $Yukki::Web::Plugin::Spreadsheet::VERSION = '0.991002';use v5.24; | ||
| 5 | use utf8; | ||
| 6 | use Moo; | ||
| 7 | |||
| 8 | use Types::Standard qw( HashRef Str ); | ||
| 9 | |||
| 10 | use namespace::clean; | ||
| 11 | |||
| 12 | extends 'Yukki::Web::Plugin'; | ||
| 13 | |||
| 14 | # ABSTRACT: add spreadsheet functionality to wiki pages | ||
| 15 | |||
| 16 | use Scalar::Util qw( blessed ); | ||
| 17 | use Try::Tiny; | ||
| 18 | use Yukki::Error qw( http_throw ); | ||
| 19 | |||
| 20 | |||
| 21 | has format_helpers => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => HashRef[Str], | ||
| 24 | required => 1, | ||
| 25 | default => sub { +{ | ||
| 26 | '=' => 'spreadsheet_eval', | ||
| 27 | } }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | with 'Yukki::Web::Plugin::Role::FormatHelper'; | ||
| 31 | |||
| 32 | |||
| 33 | sub initialize_context { | ||
| 34 | my ($self, $ctx) = @_; | ||
| 35 | |||
| 36 | $ctx->stash->{'Spreadsheet.sheet'} //= Spreadsheet::Engine->new; | ||
| 37 | $ctx->stash->{'Spreadsheet.map'} //= {}; | ||
| 38 | $ctx->stash->{'Spreadsheet.rowmap'} //= {}; | ||
| 39 | $ctx->stash->{'Spreadsheet.nextrow'} //= 'A'; | ||
| 40 | $ctx->stash->{'Spreadsheet.nextcol'} //= {}; | ||
| 41 | |||
| 42 | return $ctx->stash->{'Spreadsheet.sheet'}; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub setup_spreadsheet { | ||
| 47 | my ($self, $params) = @_; | ||
| 48 | |||
| 49 | my $ctx = $params->{context}; | ||
| 50 | my $file = $params->{file}; | ||
| 51 | my $arg = $params->{arg}; | ||
| 52 | |||
| 53 | my $sheet = $ctx->stash->{'Spreadsheet.sheet'}; | ||
| 54 | my $row = $self->row($ctx, $file); | ||
| 55 | |||
| 56 | my ($name, $formula) = $arg =~ /^(?:([\w -]+):)?(.*)/; | ||
| 57 | |||
| 58 | my $new_cell = $row . ($sheet->raw->{sheetattribs}{lastrow} + 1); | ||
| 59 | |||
| 60 | $self->cell($ctx, $file, $name, $new_cell) if $name; | ||
| 61 | |||
| 62 | return ($new_cell, $name, $formula); | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub row { | ||
| 67 | my ($self, $ctx, $file) = @_; | ||
| 68 | |||
| 69 | my $rowmap = $ctx->stash->{'Spreadsheet.rowmap'}; | ||
| 70 | |||
| 71 | my $row = $rowmap->{ $file->repository_name }{ $file->full_path } | ||
| 72 | // $ctx->stash->{'Spreadsheet.nextrow'}++; | ||
| 73 | |||
| 74 | return $rowmap->{ $file->repository_name }{ $file->full_path } = $row; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub cell { | ||
| 79 | my ($self, $ctx, $file, $name, $new_cell) = @_; | ||
| 80 | my $map = $ctx->stash->{'Spreadsheet.map'}; | ||
| 81 | $map->{ $file->repository_name }{ $file->full_path }{ $name } = $new_cell | ||
| 82 | if defined $new_cell; | ||
| 83 | return $map->{ $file->repository_name }{ $file->full_path }{ $name }; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub lookup_name { | ||
| 88 | my ($self, $params) = @_; | ||
| 89 | |||
| 90 | my $ctx = $params->{context}; | ||
| 91 | my $file = $params->{file}; | ||
| 92 | my $name = $params->{name}; | ||
| 93 | |||
| 94 | if ($name =~ /!/) { | ||
| 95 | my ($path, $name) = split /!/, $name, 2; | ||
| 96 | |||
| 97 | my $repository_name; | ||
| 98 | if ($path =~ /^(\w+):/) { | ||
| 99 | ($repository_name, $path) = split /:/, $path, 2; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $repository_name = $file->repository_name; | ||
| 103 | } | ||
| 104 | |||
| 105 | $path = $self->app->munge_label($path); | ||
| 106 | |||
| 107 | my $other_repo = $self->model('Repository', { | ||
| 108 | name => $repository_name, | ||
| 109 | }); | ||
| 110 | |||
| 111 | my $other_file = $other_repo->file({ | ||
| 112 | full_path => $path, | ||
| 113 | }); | ||
| 114 | |||
| 115 | $self->load_spreadsheet($ctx, $other_file) | ||
| 116 | unless $other_file->repository_name eq $file->repository_name | ||
| 117 | and $other_file->full_path eq $file->full_path;; | ||
| 118 | |||
| 119 | return $self->cell($ctx, $other_file, $name); | ||
| 120 | } | ||
| 121 | |||
| 122 | my $cell = $self->cell($ctx, $file, $name); | ||
| 123 | |||
| 124 | http_throw('unknown name') if not defined $cell; | ||
| 125 | |||
| 126 | return $cell; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | sub spreadsheet_eval { | ||
| 131 | my ($self, $params) = @_; | ||
| 132 | |||
| 133 | my $ctx = $params->{context}; | ||
| 134 | my $plugin_name = $params->{plugin_name}; | ||
| 135 | my $file = $params->{file}; | ||
| 136 | my $arg = $params->{arg}; | ||
| 137 | |||
| 138 | my $sheet = $self->initialize_context($ctx); | ||
| 139 | |||
| 140 | my ($new_cell, $name, $formula) = $self->setup_spreadsheet($params); | ||
| 141 | |||
| 142 | my $error = 0; | ||
| 143 | |||
| 144 | try { | ||
| 145 | $formula =~ s/ \[ ([^\]]+) \] / | ||
| 146 | $self->lookup_name({ | ||
| 147 | %$params, | ||
| 148 | name => $1, | ||
| 149 | }) | ||
| 150 | /gex; | ||
| 151 | } | ||
| 152 | |||
| 153 | catch { | ||
| 154 | $error++; | ||
| 155 | if (blessed $_ and $_->isa('Yukki::Error')) { | ||
| 156 | my $msg = $_->message; | ||
| 157 | $sheet->execute("set $new_cell constant e#NAME? $msg"); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | die $_; | ||
| 161 | } | ||
| 162 | }; | ||
| 163 | |||
| 164 | $sheet->execute("set $new_cell formula $formula") unless $error; | ||
| 165 | $sheet->recalc; | ||
| 166 | |||
| 167 | my $raw = $sheet->raw; | ||
| 168 | my $attrs = defined $name ? qq[ id="spreadsheet-$name"] : ''; | ||
| 169 | my $value; | ||
| 170 | if ($raw->{cellerrors}{ $new_cell }) { | ||
| 171 | $attrs .= qq[ title="$arg (ERROR: $raw->{formulas}{ $new_cell })"] | ||
| 172 | . qq[ class="spreadsheet-cell error" ]; | ||
| 173 | $value = $raw->{cellerrors}{ $new_cell }; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | $attrs .= qq[ title="$arg" class="spreadsheet-cell error" ]; | ||
| 177 | $value = $raw->{datavalues}{ $new_cell }; | ||
| 178 | } | ||
| 179 | |||
| 180 | return qq[<span$attrs>$value</span>]; | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | sub load_spreadsheet { | ||
| 185 | my ($self, $ctx, $file) = @_; | ||
| 186 | http_throw('no such spreadsheet exists') unless $file->exists; | ||
| 187 | $file->fetch_formatted($ctx); | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ | ||
| 193 | |||
| 194 | =pod | ||
| 195 | |||
| 196 | =encoding UTF-8 | ||
| 197 | |||
| 198 | =head1 NAME | ||
| 199 | |||
| 200 | Yukki::Web::Plugin::Spreadsheet - add spreadsheet functionality to wiki pages | ||
| 201 | |||
| 202 | =head1 VERSION | ||
| 203 | |||
| 204 | version 0.991_002 | ||
| 205 | |||
| 206 | =head1 SYNOPSIS | ||
| 207 | |||
| 208 | {{=:a:5} | ||
| 209 | {{=:b:4}} | ||
| 210 | {{=:SUM([a],[b],[main:Other Page!c])}} | ||
| 211 | |||
| 212 | =head1 DESCRIPTION | ||
| 213 | |||
| 214 | Provides a quick format helper to give you spreadsheet variables in your page. This is based upon L<Spreadsheet::Engine>, so all the features and functions there are available here. | ||
| 215 | |||
| 216 | In addition, this provides a variable mapping. The variables are mapped using square brackets. You can link between variables on different pages using an exclamation mark ("!") as a separated between page name and variable name. | ||
| 217 | |||
| 218 | =head1 ATTRIBUTES | ||
| 219 | |||
| 220 | =head2 format_helpers | ||
| 221 | |||
| 222 | This sets up the "=" format helper mapped to the L</spreadsheet_eval> method. | ||
| 223 | |||
| 224 | =head1 METHODS | ||
| 225 | |||
| 226 | =head2 initialize_context | ||
| 227 | |||
| 228 | Used to setup the spreadsheet information for the current context. Do not use. | ||
| 229 | |||
| 230 | =head2 setup_spreadsheet | ||
| 231 | |||
| 232 | Sets up spreadsheet for the current request context. Do not use. | ||
| 233 | |||
| 234 | =head2 row | ||
| 235 | |||
| 236 | Used to lookup the current row letter for a file. Do not use. | ||
| 237 | |||
| 238 | =head2 cell | ||
| 239 | |||
| 240 | Used to lookup the cell for a variable. Do not use. | ||
| 241 | |||
| 242 | =head2 lookup_name | ||
| 243 | |||
| 244 | Used to convert the square bracket names to cell names. Do not use. | ||
| 245 | |||
| 246 | =head2 spreadsheet_eval | ||
| 247 | |||
| 248 | This is used to format the double-curly brace C< {{=:...}} >. Do not use. | ||
| 249 | |||
| 250 | =head2 load_spreadsheet | ||
| 251 | |||
| 252 | Used to load spreadsheet variables from an externally referenced wiki page. Do not use. | ||
| 253 | |||
| 254 | =head1 AUTHOR | ||
| 255 | |||
| 256 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 257 | |||
| 258 | =head1 COPYRIGHT AND LICENSE | ||
| 259 | |||
| 260 | This software is copyright (c) 2017 by Qubling Software LLC. | ||
| 261 | |||
| 262 | This is free software; you can redistribute it and/or modify it under | ||
| 263 | the same terms as the Perl 5 programming language system itself. | ||
| 264 | |||
| 265 | =cut |