| 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 | |
| Nils Diewald | b8cd4d6 | 2015-05-08 00:37:46 +0000 | [diff] [blame] | 19 | my $base = $c->url_for('index'); |
| 20 | if ($base->path->parts->[0]) { |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 21 | $base->path->trailing_slash(1); |
| Nils Diewald | b8cd4d6 | 2015-05-08 00:37:46 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 24 | # If there is a different base - append this as a base |
| Nils Diewald | b541d71 | 2015-05-08 00:41:14 +0000 | [diff] [blame] | 25 | $url->query([base => $base // '/']); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 26 | |
| 27 | $url->fragment($scope); |
| 28 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 29 | return $c->tag( |
| 30 | 'object', |
| 31 | data => $url, |
| 32 | type => 'image/svg+xml', |
| 33 | alt => $c->loc('korap_overview'), |
| 34 | id => 'overview' |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 35 | ); |
| 36 | } |
| 37 | ); |
| 38 | |
| 39 | # Documentation link |
| Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 40 | # TODO: Support opener mechanism, so the link will open the embedded |
| 41 | # documentation in case it's not there. |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 42 | $mojo->helper( |
| 43 | doc_link_to => sub { |
| 44 | my $c = shift; |
| 45 | my $title = shift; |
| 46 | my $page = pop; |
| 47 | my $scope = shift; |
| Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 48 | |
| 49 | ($page, my $fragment) = split '#', $page; |
| 50 | |
| 51 | my $url = $c->url_with( |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 52 | 'doc', |
| 53 | scope => $scope, |
| 54 | page => $page |
| Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 55 | ); |
| 56 | |
| 57 | $url->fragment($fragment) if $fragment; |
| 58 | |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 59 | return $c->link_to( |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 60 | $title, |
| 61 | $url, |
| 62 | class => 'doc-link' |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 63 | ); |
| 64 | } |
| 65 | ); |
| 66 | |
| Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 67 | $mojo->helper( |
| 68 | doc_ext_link_to => sub { |
| 69 | my $c = shift; |
| 70 | return $c->link_to(@_, target => '_top'); |
| 71 | } |
| 72 | ); |
| 73 | |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 74 | |
| 75 | # Documentation alert - Under Construction! |
| 76 | $mojo->helper( |
| 77 | doc_uc => sub { |
| Nils Diewald | 023c671 | 2015-05-21 20:12:30 +0000 | [diff] [blame] | 78 | my $c = shift; |
| 79 | return $c->tag('p', $c->loc('underConstruction')); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 80 | } |
| 81 | ); |
| 82 | |
| Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 83 | $mojo->helper( |
| 84 | doc_opener => sub { |
| 85 | my $c = shift; |
| 86 | my $cb = pop; |
| 87 | my $page = pop; |
| 88 | my $scope = shift; |
| 89 | my $url; |
| 90 | if ($page) { |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 91 | $url = $c->url_for('doc', page => $page, scope => $scope); |
| 92 | $url->path->canonicalize; |
| Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 93 | } |
| 94 | else { |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 95 | $url = $c->url_for('doc_start'); |
| Nils Diewald | a898dac | 2015-05-06 21:04:16 +0000 | [diff] [blame] | 96 | }; |
| 97 | return $c->link_to($cb->($c), $url); |
| 98 | } |
| 99 | ); |
| 100 | |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 101 | |
| 102 | # Documentation navigation helper |
| 103 | $mojo->helper( |
| 104 | doc_navi => sub { |
| 105 | my $c = shift; |
| 106 | my $items = pop; |
| 107 | my $scope = shift; |
| 108 | |
| 109 | # Create unordered list |
| Akron | e8235be | 2016-06-27 11:02:18 +0200 | [diff] [blame] | 110 | my $html = '<ul class="nav">'."\n"; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 111 | |
| 112 | # Embed all link tags |
| 113 | foreach (@$items) { |
| 114 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 115 | my ($active, $url) = 0; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 116 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 117 | # There is a fragment! |
| 118 | if (index($_->{id}, '#') == 0) { |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 119 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 120 | my $part_scope = scalar($scope); |
| 121 | $part_scope =~ s!\/([^\/]+)$!!; |
| 122 | my $page = $1; |
| 123 | my $id = $_->{id}; |
| 124 | $id =~ s/^#//; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 125 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 126 | $url = $c->url_with( |
| 127 | 'doc', |
| 128 | 'scope' => $part_scope, |
| 129 | 'page' => $page |
| 130 | ); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 131 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 132 | $url->fragment($id); |
| 133 | } |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 134 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 135 | # There is no fragment |
| 136 | else { |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 137 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 138 | # The item is active |
| 139 | if ($c->stash('page') && $c->stash('page') eq $_->{id}) { |
| 140 | $active = 1; |
| 141 | }; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 142 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 143 | # Generate url with query parameter inheritance |
| 144 | $url = $c->url_with( |
| 145 | 'doc', |
| 146 | 'scope' => $scope, |
| 147 | 'page' => $_->{id} |
| 148 | ); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 149 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 150 | # Canonicalize (for empty scopes) |
| 151 | $url->path->canonicalize; |
| Akron | 1120a58 | 2017-10-17 12:29:16 +0200 | [diff] [blame] | 152 | $url->fragment('tutorial-top'); |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 153 | }; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 154 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 155 | my @classes; |
| 156 | push(@classes, $_->{'class'}) if $_->{'class'}; |
| 157 | push(@classes, 'active') if $active; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 158 | |
| 159 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 160 | # New list item |
| 161 | $html .= '<li'; |
| 162 | if (@classes) { |
| 163 | $html .= ' class="' . join(' ', @classes) . '"'; |
| 164 | }; |
| 165 | $html .= '>'; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 166 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 167 | # Translate title |
| 168 | my $title = $c->loc('Nav_' . $_->{id}, $_->{title}); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 169 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 170 | # Generate link |
| 171 | $html .= $c->link_to($title, $url); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 172 | |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 173 | # Set sub entries |
| 174 | if ($_->{items} && ref($_->{items}) eq 'ARRAY') { |
| 175 | $html .= "\n"; |
| 176 | my $subscope = $scope ? scalar($scope) . '/' . $_->{id} : $_->{id}; |
| 177 | $html .= $c->doc_navi($subscope, $_->{items}); |
| 178 | $html .= "</li>\n"; |
| 179 | } |
| 180 | else { |
| 181 | $html .= "</li>\n"; |
| 182 | }; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 183 | }; |
| 184 | return $html . "</ul>\n"; |
| 185 | } |
| 186 | ); |
| 187 | |
| 188 | |
| 189 | # Create helper for queries in the tutorial |
| 190 | $mojo->helper( |
| Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 191 | doc_query => sub { |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 192 | my ($c, $ql, $q, %param) = @_; |
| 193 | |
| 194 | # Escape query for html embedding |
| 195 | $q = xml_escape $q; |
| 196 | |
| 197 | # Return tag |
| 198 | b('<pre class="query tutorial" ' . |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 199 | qq!data-query="$q" data-query-cutoff="! . |
| 200 | ($param{cutoff} ? 1 : 0) . |
| 201 | '"' . |
| 202 | qq! data-query-language="$ql">! . |
| 203 | '<code>' . $q . '</code>' . |
| 204 | '</pre>' |
| 205 | ); |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 206 | } |
| 207 | ); |
| 208 | |
| 209 | |
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 210 | # Check for test port |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 211 | $mojo->helper( |
| 212 | kalamar_test_port => sub { |
| 213 | my $c = shift; |
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 214 | |
| 215 | # Test port is defined in the stash |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 216 | if (defined $c->stash('kalamar.test_port')) { |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 217 | return $c->stash('kalamar.test_port'); |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 220 | # Check the port |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 221 | if ($c->req->url->to_abs->port == 6666 || |
| Akron | 0dc1090 | 2017-09-01 18:00:16 +0200 | [diff] [blame] | 222 | $c->app->mode =~ m/^development|test$/) { |
| 223 | $c->stash('kalamar.test_port' => 1); |
| 224 | return 1; |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 227 | # No test port |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 228 | $c->stash('kalamar.test_port' => 0); |
| 229 | return 0; |
| 230 | }); |
| 231 | }; |
| 232 | |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 233 | |
| Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 234 | 1; |
| Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 235 | |
| 236 | |
| 237 | __END__ |
| 238 | |
| Nils Diewald | 023c671 | 2015-05-21 20:12:30 +0000 | [diff] [blame] | 239 | =pod |
| 240 | |
| 241 | =encoding utf8 |
| 242 | |
| 243 | =head1 NAME |
| 244 | |
| 245 | Kalamar::Plugin::KalamarHelpers |
| 246 | |
| 247 | |
| 248 | =head1 DESCRIPTION |
| 249 | |
| 250 | L<Kalamar::Plugin::KalamarHelpers> makes Kalamar specific |
| 251 | helpers for Mojolicious available. |
| 252 | |
| 253 | |
| 254 | =head1 HELPERS |
| 255 | |
| 256 | =head2 doc_link_to |
| 257 | |
| 258 | %# In templates |
| 259 | %= doc_link_to 'Kalamar', 'korap', 'kalamar' |
| 260 | |
| 261 | Create a link to the documentation. Accepts a name, a scope, and a page. |
| 262 | |
| 263 | |
| 264 | =head2 doc_ext_link_to |
| 265 | |
| 266 | %# In templates |
| 267 | %= doc_ext_link_to 'GitHub', "https://github.com/KorAP/Koral" |
| 268 | |
| 269 | Creates a link to an external page, that will be opened in the top frame, |
| 270 | in case it's in an embedded frame (used in the tutorial). |
| 271 | |
| 272 | =head2 doc_uc |
| 273 | |
| 274 | %# In templates |
| 275 | %= doc_uc |
| 276 | |
| 277 | Generates an C<Under Construction> notification. |
| 278 | |
| 279 | |
| 280 | =head2 doc_opener |
| 281 | |
| 282 | Currently not used. |
| 283 | |
| 284 | |
| 285 | =head2 doc_navi |
| 286 | |
| 287 | Returns an HTML representation of the documentation navigation, |
| 288 | based on active navigation items. |
| 289 | |
| 290 | |
| 291 | =head2 doc_query |
| 292 | |
| 293 | %# In templates |
| 294 | %= doc_query poliqarp => 'Baum' |
| 295 | |
| 296 | Creates an interactive query view for documentation purposes. |
| 297 | |
| 298 | |
| 299 | =head2 kalamar_test_port |
| 300 | |
| 301 | # In controllers |
| 302 | if ($c->kalamar_test_port) { |
| 303 | $c->app->log->debug('Kalamar runs on test port'); |
| 304 | }; |
| 305 | |
| 306 | Returns a C<true> value in case Kalamar runs on test port C<6666>. |
| 307 | |
| 308 | |
| 309 | =head2 korap_overview |
| 310 | |
| 311 | %# In templates |
| 312 | %= korap_overview 'kalamar' |
| 313 | |
| 314 | Returns a modified and relatified overview svg image to be embedded |
| 315 | as an object in templates. |
| 316 | |
| 317 | |
| 318 | =head1 COPYRIGHT AND LICENSE |
| 319 | |
| 320 | Copyright (C) 2015, L<IDS Mannheim|http://www.ids-mannheim.de/> |
| 321 | Author: L<Nils Diewald|http://nils-diewald.de/> |
| 322 | |
| 323 | Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/> |
| 324 | Corpus Analysis Platform at the |
| 325 | L<Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 326 | member of the |
| 327 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/> |
| 328 | and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project, |
| 329 | funded by the |
| 330 | L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>. |
| 331 | |
| 332 | Kalamar is free software published under the |
| Akron | 456abd9 | 2015-06-02 15:07:21 +0200 | [diff] [blame] | 333 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>. |
| Nils Diewald | 023c671 | 2015-05-21 20:12:30 +0000 | [diff] [blame] | 334 | |
| 335 | =cut |