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