Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 1 | package Kalamar::Plugin::KalamarHelpers; |
| 2 | use Mojo::Base 'Mojolicious::Plugin'; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 3 | use Mojo::JSON 'decode_json'; |
| 4 | use Mojo::JSON::Pointer; |
| 5 | use Mojo::ByteStream 'b'; |
| 6 | use Mojo::Util qw/xml_escape/; |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 7 | |
| 8 | sub register { |
| 9 | my ($plugin, $mojo) = @_; |
| 10 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 11 | # Embed the korap architecture image |
| 12 | $mojo->helper( |
| 13 | korap_overview => sub { |
| 14 | my $c = shift; |
| 15 | my $scope = shift; |
| 16 | |
| 17 | my $url = $c->url_with('/img/korap-overview.svg'); |
| 18 | |
| 19 | # If there is a different base - append this as a base |
| 20 | # $url->query([base => $c->stash('doc_base') // '/']); |
| 21 | $url->query([base => $c->url_for('doc_start')->to_abs // '/']); |
| 22 | |
| 23 | $url->fragment($scope); |
| 24 | |
| 25 | return $c->tag('object', |
| 26 | data => $url, |
| 27 | type => 'image/svg+xml', |
| 28 | alt => $c->loc('korap_overview'), |
| 29 | id => 'overview' |
| 30 | ); |
| 31 | } |
| 32 | ); |
| 33 | |
| 34 | # Documentation link |
Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame^] | 35 | # TODO: Support opener mechanism, so the link will open the embedded |
| 36 | # documentation in case it's not there. |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 37 | $mojo->helper( |
| 38 | doc_link_to => sub { |
| 39 | my $c = shift; |
| 40 | my $title = shift; |
| 41 | my $page = pop; |
| 42 | my $scope = shift; |
| 43 | return $c->link_to( |
| 44 | $title, |
| 45 | $c->url_with('doc', scope => $scope, page => $page) |
| 46 | ); |
| 47 | } |
| 48 | ); |
| 49 | |
| 50 | |
| 51 | # Documentation alert - Under Construction! |
| 52 | $mojo->helper( |
| 53 | doc_uc => sub { |
| 54 | return shift->tag('p', 'Under Construction!') |
| 55 | } |
| 56 | ); |
| 57 | |
Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame^] | 58 | $mojo->helper( |
| 59 | doc_opener => sub { |
| 60 | my $c = shift; |
| 61 | my $cb = pop; |
| 62 | my $page = pop; |
| 63 | my $scope = shift; |
| 64 | my $url; |
| 65 | if ($page) { |
| 66 | $url = $c->url_for('doc', page => $page, scope => $scope); |
| 67 | $url->path->canonicalize; |
| 68 | } |
| 69 | else { |
| 70 | $url = $c->url_for('doc_start'); |
| 71 | }; |
| 72 | return $c->link_to($cb->($c), $url); |
| 73 | } |
| 74 | ); |
| 75 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 76 | |
| 77 | # Documentation navigation helper |
| 78 | $mojo->helper( |
| 79 | doc_navi => sub { |
| 80 | my $c = shift; |
| 81 | my $items = pop; |
| 82 | my $scope = shift; |
| 83 | |
| 84 | # Create unordered list |
| 85 | my $html = "<ul>\n"; |
| 86 | |
| 87 | # Embed all link tags |
| 88 | foreach (@$items) { |
| 89 | |
| 90 | my ($active, $url) = 0; |
| 91 | |
| 92 | # There is a fragment! |
| 93 | if (index($_->{id}, '#') == 0) { |
| 94 | |
| 95 | my $part_scope = scalar($scope); |
| 96 | $part_scope =~ s!\/([^\/]+)$!!; |
| 97 | my $page = $1; |
| 98 | my $id = $_->{id}; |
| 99 | $id =~ s/^#//; |
| 100 | |
| 101 | $url = $c->url_with( |
| 102 | 'doc', |
| 103 | 'scope' => $part_scope, |
| 104 | 'page' => $page |
| 105 | ); |
| 106 | |
| 107 | $url->fragment($id); |
| 108 | } |
| 109 | |
| 110 | # There is no fragment |
| 111 | else { |
| 112 | |
| 113 | # The item is active |
| 114 | if ($c->stash('page') && $c->stash('page') eq $_->{id}) { |
| 115 | $active = 1; |
| 116 | }; |
| 117 | |
| 118 | # Generate url with query parameter inheritance |
| 119 | $url = $c->url_with( |
| 120 | 'doc', |
| 121 | 'scope' => $scope, |
| 122 | 'page' => $_->{id} |
| 123 | ); |
| 124 | |
| 125 | # Canonicalize (for empty scopes) |
| 126 | $url->path->canonicalize; |
| 127 | }; |
| 128 | |
| 129 | my @classes; |
| 130 | push(@classes, $_->{'class'}) if $_->{'class'}; |
| 131 | push(@classes, 'active') if $active; |
| 132 | |
| 133 | |
| 134 | # New list item |
| 135 | $html .= '<li'; |
| 136 | if (@classes) { |
| 137 | $html .= ' class="' . join(' ', @classes) . '"'; |
| 138 | }; |
| 139 | $html .= '>'; |
| 140 | |
| 141 | |
| 142 | # Generate link |
| 143 | $html .= $c->link_to($_->{title}, $url); |
| 144 | |
| 145 | # Set sub entries |
| 146 | if ($_->{items} && ref($_->{items}) eq 'ARRAY') { |
| 147 | $html .= "\n"; |
| 148 | my $subscope = $scope ? scalar($scope) . '/' . $_->{id} : $_->{id}; |
| 149 | $html .= $c->doc_navi($subscope, $_->{items}); |
| 150 | $html .= "</li>\n"; |
| 151 | } |
| 152 | else { |
| 153 | $html .= "</li>\n"; |
| 154 | }; |
| 155 | }; |
| 156 | return $html . "</ul>\n"; |
| 157 | } |
| 158 | ); |
| 159 | |
| 160 | |
| 161 | # Create helper for queries in the tutorial |
| 162 | $mojo->helper( |
| 163 | kalamar_tut_query => sub { |
| 164 | my ($c, $ql, $q, %param) = @_; |
| 165 | |
| 166 | # Escape query for html embedding |
| 167 | $q = xml_escape $q; |
| 168 | |
| 169 | # Return tag |
| 170 | b('<pre class="query tutorial" ' . |
| 171 | qq!data-query="$q" data-query-cutoff="! . |
| 172 | ($param{cutoff} ? 1 : 0) . |
| 173 | '"' . |
| 174 | qq! data-query-language="$ql">! . |
| 175 | '<code>' . $q . '</code>' . |
| 176 | '</pre>' |
| 177 | ); |
| 178 | } |
| 179 | ); |
| 180 | |
| 181 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 182 | # Check for test port |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 183 | $mojo->helper( |
| 184 | kalamar_test_port => sub { |
| 185 | my $c = shift; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 186 | |
| 187 | # Test port is defined in the stash |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 188 | if (defined $c->stash('kalamar.test_port')) { |
| 189 | return $c->stash('kalamar.test_port'); |
| 190 | }; |
| 191 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 192 | # Check the port |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 193 | if ($c->req->url->to_abs->port == 6666 || |
| 194 | $c->app->mode =~ m/^development|test$/) { |
| 195 | $c->stash('kalamar.test_port' => 1); |
| 196 | return 1; |
| 197 | }; |
| 198 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 199 | # No test port |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 200 | $c->stash('kalamar.test_port' => 0); |
| 201 | return 0; |
| 202 | }); |
Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame^] | 203 | |
| 204 | # Create links in the tutorial that make sure the current position is preserved, |
| 205 | # in case the tutorial was opened embedded |
| 206 | $mojo->helper( |
| 207 | kalamar_tut_link_to => sub { |
| 208 | return '[TODO]'; |
| 209 | } |
| 210 | ); |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 213 | |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 214 | 1; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | __END__ |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | # Create links in the tutorial that make sure the current position is preserved, |
| 224 | # in case the tutorial was opened embedded |
| 225 | $mojo->helper( |
| 226 | kalamar_tut_link_to => sub { |
| 227 | my $c = shift; |
| 228 | my $title = shift; |
| 229 | my $link = shift; |
| 230 | my $host = $c->req->headers->header('X-Forwarded-Host'); |
| 231 | my $url = $c->url_for($link); |
| 232 | |
| 233 | # Link is part of the embedded tutorial |
| 234 | if ($c->param('embedded')) { |
| 235 | $url->query({ embedded => 1 }); |
| 236 | return $c->link_to( |
| 237 | $title, |
| 238 | $url, |
| 239 | onclick => qq!setTutorialPage("$url")! |
| 240 | ); |
| 241 | }; |
| 242 | |
| 243 | # Build link |
| 244 | return $c->link_to($title, $url); |
| 245 | } |
| 246 | ); |
| 247 | |
| 248 | |
| 249 | |
| 250 | # Create helper for queries in the tutorial |
| 251 | $mojo->helper( |
| 252 | kalamar_tut_query => sub { |
| 253 | my ($c, $ql, $q, %param) = @_; |
| 254 | my $onclick = 'top.useQuery(this)'; |
| 255 | |
| 256 | my ($fail, $pass, @report) = (0,0); |
| 257 | |
| 258 | if ($c->param('testing')) { |
| 259 | $c->res->headers->cache_control('max-age=1, no-cache'); |
| 260 | }; |
| 261 | |
| 262 | # Store current tutorial position |
| 263 | if ($c->param('embedded')) { |
| 264 | $onclick = 'setTutorialPage(this);' . $onclick; |
| 265 | } |
| 266 | |
| 267 | # Tutorial wasn't embedded - but opened for testing |
| 268 | # elsif ($c->param('testing') && |
| 269 | # $c->kalamar_test_port && |
| 270 | # $param{tests}) { |
| 271 | # |
| 272 | # Currently disabled |
| 273 | |
| 274 | # my $tests = $param{tests} // []; |
| 275 | # my $index = $c->search( |
| 276 | # query => $q, |
| 277 | # ql => $ql, |
| 278 | # cutoff => 'true', |
| 279 | # no_cache => 1 |
| 280 | # ); |
| 281 | # |
| 282 | # # Get the raw results |
| 283 | # my $json = decode_json($index->api_response); |
| 284 | # |
| 285 | # # There is a response |
| 286 | # if ($json) { |
| 287 | # my $json_pointer = Mojo::JSON::Pointer->new($json); |
| 288 | # foreach my $test (@$tests) { |
| 289 | # my ($type, $path, @rest) = @$test; |
| 290 | # |
| 291 | # # Check for equality |
| 292 | # if ($type eq 'is') { |
| 293 | # my $found = $json_pointer->get($path); |
| 294 | # if ($found && $found eq $rest[0]) { |
| 295 | # $pass++; |
| 296 | # } |
| 297 | # else { |
| 298 | # my $result = $path . q! isn't ! . shift @rest; |
| 299 | # $result .= ' but was ' . $found if $found; |
| 300 | # $result .= '; ' . join('; ', @rest); |
| 301 | # push(@report, $result); |
| 302 | # $fail++; |
| 303 | # }; |
| 304 | # } |
| 305 | # |
| 306 | # # Check for inequality |
| 307 | # elsif ($type eq 'isnt') { |
| 308 | # if ($json_pointer->get($path) ne $rest[0]) { |
| 309 | # $pass++; |
| 310 | # } |
| 311 | # else { |
| 312 | # push(@report, $path . q! is ! . join('; ', @rest)); |
| 313 | # $fail++; |
| 314 | # }; |
| 315 | # } |
| 316 | # |
| 317 | # # Check for existence |
| 318 | # elsif ($type eq 'ok') { |
| 319 | # if ($json_pointer->contains($path)) { |
| 320 | # $pass++; |
| 321 | # } |
| 322 | # else { |
| 323 | # push(@report, $path . q! doesn't exist; ! . join('; ', @rest)); |
| 324 | # $fail++; |
| 325 | # }; |
| 326 | # } |
| 327 | # |
| 328 | # # Check for inexistence |
| 329 | # elsif ($type eq 'not_ok') { |
| 330 | # unless ($json_pointer->contains($path)) { |
| 331 | # $pass++; |
| 332 | # } |
| 333 | # else { |
| 334 | # push(@report, $path . q! doesn't exist; ! . join('; ', @rest)); |
| 335 | # $fail++; |
| 336 | # }; |
| 337 | # }; |
| 338 | # }; |
| 339 | # } |
| 340 | # else { |
| 341 | # # There may be notifications here! |
| 342 | # $fail++ foreach @$tests; |
| 343 | # }; |
| 344 | # |
| 345 | # # Emit hook to possible subscribers |
| 346 | # # This is used for self-testing |
| 347 | # # $plugin->emit_hook(kalamar_tut_query => ( |
| 348 | # # query_language => $ql, |
| 349 | # # query => $q, |
| 350 | # # %param |
| 351 | # # )); |
| 352 | # }; |
| 353 | |
| 354 | # Escape query for html embedding |
| 355 | $q = xml_escape $q; |
| 356 | |
| 357 | # There is something to talk about |
| 358 | my $msg = ''; |
| 359 | if (($pass + $fail) > 0) { |
| 360 | $msg .= '<div class="test">'; |
| 361 | $msg .= qq!<p class="pass">Pass: $pass</p>!; |
| 362 | $msg .= qq!<p class="fail">Fail: $fail</p>! if $fail; |
| 363 | foreach (@report) { |
| 364 | $msg .= qq!<p class="fail">${_}</p>!; |
| 365 | }; |
| 366 | $msg .= '</div>'; |
| 367 | }; |
| 368 | |
| 369 | # Return tag |
| 370 | b('<pre class="query tutorial" onclick="' . $onclick . '" ' . |
| 371 | qq!data-query="$q" data-query-cutoff="! . |
| 372 | ($param{cutoff} ? 1 : 0) . |
| 373 | '"' . |
| 374 | qq! data-query-language="$ql"><code>! . |
| 375 | $q . |
| 376 | '</code></pre>' . $msg); |
| 377 | } |
| 378 | ); |