| 1 | package Yukki::Web::Plugin::Spreadsheet; | ||
| 2 | { | ||
| 3 | $Yukki::Web::Plugin::Spreadsheet::VERSION = '0.140290'; | ||
| 4 | } | ||
| 5 | use 5.12.1; | ||
| 6 | use Moose; | ||
| 7 | |||
| 8 | extends 'Yukki::Web::Plugin'; | ||
| 9 | |||
| 10 | # ABSTRACT: add spreadsheet functionality to wiki pages | ||
| 11 | |||
| 12 | use Scalar::Util qw( blessed ); | ||
| 13 | use Try::Tiny; | ||
| 14 | use Yukki::Error qw( http_throw ); | ||
| 15 | |||
| 16 | |||
| 17 | has format_helpers => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'HashRef[Str]', | ||
| 20 | required => 1, | ||
| 21 | default => sub { +{ | ||
| 22 | '=' => 'spreadsheet_eval', | ||
| 23 | } }, | ||
| 24 | ); | ||
| 25 | |||
| 26 | with 'Yukki::Web::Plugin::Role::FormatHelper'; | ||
| 27 | |||
| 28 | |||
| 29 | sub initialize_context { | ||
| 30 | my ($self, $ctx) = @_; | ||
| 31 | |||
| 32 | $ctx->stash->{'Spreadsheet.sheet'} //= Spreadsheet::Engine->new; | ||
| 33 | $ctx->stash->{'Spreadsheet.map'} //= {}; | ||
| 34 | $ctx->stash->{'Spreadsheet.rowmap'} //= {}; | ||
| 35 | $ctx->stash->{'Spreadsheet.nextrow'} //= 'A'; | ||
| 36 | $ctx->stash->{'Spreadsheet.nextcol'} //= {}; | ||
| 37 | |||
| 38 | return $ctx->stash->{'Spreadsheet.sheet'}; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub setup_spreadsheet { | ||
| 43 | my ($self, $params) = @_; | ||
| 44 | |||
| 45 | my $ctx = $params->{context}; | ||
| 46 | my $file = $params->{file}; | ||
| 47 | my $arg = $params->{arg}; | ||
| 48 | |||
| 49 | my $sheet = $ctx->stash->{'Spreadsheet.sheet'}; | ||
| 50 | my $row = $self->row($ctx, $file); | ||
| 51 | |||
| 52 | my ($name, $formula) = $arg =~ /^(?:([\w -]+):)?(.*)/; | ||
| 53 | |||
| 54 | my $new_cell = $row . ($sheet->raw->{sheetattribs}{lastrow} + 1); | ||
| 55 | |||
| 56 | $self->cell($ctx, $file, $name, $new_cell) if $name; | ||
| 57 | |||
| 58 | return ($new_cell, $name, $formula); | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub row { | ||
| 63 | my ($self, $ctx, $file) = @_; | ||
| 64 | |||
| 65 | my $rowmap = $ctx->stash->{'Spreadsheet.rowmap'}; | ||
| 66 | |||
| 67 | my $row = $rowmap->{ $file->repository_name }{ $file->full_path } | ||
| 68 | // $ctx->stash->{'Spreadsheet.nextrow'}++; | ||
| 69 | |||
| 70 | return $rowmap->{ $file->repository_name }{ $file->full_path } = $row; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub cell { | ||
| 75 | my ($self, $ctx, $file, $name, $new_cell) = @_; | ||
| 76 | my $map = $ctx->stash->{'Spreadsheet.map'}; | ||
| 77 | $map->{ $file->repository_name }{ $file->full_path }{ $name } = $new_cell | ||
| 78 | if defined $new_cell; | ||
| 79 | return $map->{ $file->repository_name }{ $file->full_path }{ $name }; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub lookup_name { | ||
| 84 | my ($self, $params) = @_; | ||
| 85 | |||
| 86 | my $ctx = $params->{context}; | ||
| 87 | my $file = $params->{file}; | ||
| 88 | my $name = $params->{name}; | ||
| 89 | |||
| 90 | if ($name =~ /!/) { | ||
| 91 | my ($path, $name) = split /!/, $name, 2; | ||
| 92 | |||
| 93 | my $repository_name; | ||
| 94 | if ($path =~ /^(\w+):/) { | ||
| 95 | ($repository_name, $path) = split /:/, $path, 2; | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | $repository_name = $file->repository_name; | ||
| 99 | } | ||
| 100 | |||
| 101 | $path = $self->app->munge_label($path); | ||
| 102 | |||
| 103 | my $other_repo = $self->model('Repository', { | ||
| 104 | name => $repository_name, | ||
| 105 | }); | ||
| 106 | |||
| 107 | my $other_file = $other_repo->file({ | ||
| 108 | full_path => $path, | ||
| 109 | }); | ||
| 110 | |||
| 111 | $self->load_spreadsheet($ctx, $other_file) | ||
| 112 | unless $other_file->repository_name eq $file->repository_name | ||
| 113 | and $other_file->full_path eq $file->full_path;; | ||
| 114 | |||
| 115 | return $self->cell($ctx, $other_file, $name); | ||
| 116 | } | ||
| 117 | |||
| 118 | my $cell = $self->cell($ctx, $file, $name); | ||
| 119 | |||
| 120 | http_throw('unknown name') if not defined $cell; | ||
| 121 | |||
| 122 | return $cell; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub spreadsheet_eval { | ||
| 127 | my ($self, $params) = @_; | ||
| 128 | |||
| 129 | my $ctx = $params->{context}; | ||
| 130 | my $plugin_name = $params->{plugin_name}; | ||
| 131 | my $file = $params->{file}; | ||
| 132 | my $arg = $params->{arg}; | ||
| 133 | |||
| 134 | my $sheet = $self->initialize_context($ctx); | ||
| 135 | |||
| 136 | my ($new_cell, $name, $formula) = $self->setup_spreadsheet($params); | ||
| 137 | |||
| 138 | my $error = 0; | ||
| 139 | |||
| 140 | try { | ||
| 141 | $formula =~ s/ \[ ([^\]]+) \] / | ||
| 142 | $self->lookup_name({ | ||
| 143 | %$params, | ||
| 144 | name => $1, | ||
| 145 | }) | ||
| 146 | /gex; | ||
| 147 | } | ||
| 148 | |||
| 149 | catch { | ||
| 150 | $error++; | ||
| 151 | if (blessed $_ and $_->isa('Yukki::Error')) { | ||
| 152 | my $msg = $_->message; | ||
| 153 | $sheet->execute("set $new_cell constant e#NAME? $msg"); | ||
| 154 | } | ||
| 155 | else { | ||
| 156 | die $_; | ||
| 157 | } | ||
| 158 | }; | ||
| 159 | |||
| 160 | $sheet->execute("set $new_cell formula $formula") unless $error; | ||
| 161 | $sheet->recalc; | ||
| 162 | |||
| 163 | my $raw = $sheet->raw; | ||
| 164 | my $attrs = defined $name ? qq[ id="spreadsheet-$name"] : ''; | ||
| 165 | my $value; | ||
| 166 | if ($raw->{cellerrors}{ $new_cell }) { | ||
| 167 | $attrs .= qq[ title="$arg (ERROR: $raw->{formulas}{ $new_cell })"] | ||
| 168 | . qq[ class="spreadsheet-cell error" ]; | ||
| 169 | $value = $raw->{cellerrors}{ $new_cell }; | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | $attrs .= qq[ title="$arg" class="spreadsheet-cell error" ]; | ||
| 173 | $value = $raw->{datavalues}{ $new_cell }; | ||
| 174 | } | ||
| 175 | |||
| 176 | return qq[<span$attrs>$value</span>]; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | sub load_spreadsheet { | ||
| 181 | my ($self, $ctx, $file) = @_; | ||
| 182 | http_throw('no such spreadsheet exists') unless $file->exists; | ||
| 183 | $file->fetch_formatted($ctx); | ||
| 184 | } | ||
| 185 | |||
| 186 | 1; | ||
| 187 | |||
| 188 | __END__ |