| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 1 | package Kalamar::Plugin::KalamarPages; |
| 2 | use Mojo::Base 'Mojolicious::Plugin'; |
| 3 | use Mojo::JSON qw/decode_json true false/; |
| 4 | use Mojo::ByteStream 'b'; |
| 5 | use Mojo::Util qw/xml_escape deprecated/; |
| 6 | |
| Akron | f7ec444 | 2019-10-27 20:01:05 +0100 | [diff] [blame] | 7 | our $navi = {}; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 8 | |
| 9 | # TODO: |
| 10 | # Add documentation plugin to programmatically |
| 11 | # create documentation navigation as a content_block |
| 12 | # so custom routes to custom templates can easily |
| 13 | # be configured |
| 14 | sub register { |
| 15 | my ($plugin, $mojo) = @_; |
| 16 | |
| 17 | # Documentation link |
| 18 | # TODO: Support opener mechanism, so the link will open the embedded |
| 19 | # documentation in case it's not there. |
| 20 | $mojo->helper( |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 21 | embedded_link_to => sub { |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 22 | my $c = shift; |
| Akron | 3cfa26d | 2019-10-24 15:17:34 +0200 | [diff] [blame] | 23 | |
| 24 | # The embedded link now expects at least 3 parameters: |
| 25 | # - The realm, which is identical to the named route |
| 26 | # - The title of the link |
| 27 | # - An optional scope, which is a first level path for navigation |
| 28 | # - The page to link to (accepting a fragment) |
| 29 | my $realm = shift; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 30 | my $title = shift; |
| 31 | my $page = pop; |
| 32 | my $scope = shift; |
| 33 | |
| 34 | ($page, my $fragment) = split '#', $page; |
| 35 | |
| Akron | 3cfa26d | 2019-10-24 15:17:34 +0200 | [diff] [blame] | 36 | my $url = $c->url_with($realm, page => $page, scope => $scope); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 37 | $url->fragment($fragment) if $fragment; |
| Akron | 254fe21 | 2019-10-24 14:33:28 +0200 | [diff] [blame] | 38 | $url->path->canonicalize; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 39 | |
| 40 | return $c->link_to( |
| 41 | $title, |
| 42 | $url, |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 43 | class => 'embedded-link' |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 44 | ); |
| 45 | } |
| 46 | ); |
| 47 | |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 48 | # DEPRECATED: 2019-10-17 |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 49 | $mojo->helper( |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 50 | doc_link_to => sub { |
| 51 | my $c = shift; |
| 52 | deprecated 'Deprecated "doc_link_to" in favor of "embedded_link_to"'; |
| Akron | 3cfa26d | 2019-10-24 15:17:34 +0200 | [diff] [blame] | 53 | return $c->embedded_link_to('doc', @_) |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 54 | } |
| 55 | ); |
| 56 | |
| 57 | # Link to an external page |
| 58 | $mojo->helper( |
| 59 | ext_link_to => sub { |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 60 | my $c = shift; |
| 61 | return $c->link_to(@_, target => '_top'); |
| 62 | } |
| 63 | ); |
| 64 | |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 65 | # DEPRECATED: 2019-10-17 |
| 66 | $mojo->helper( |
| 67 | doc_ext_link_to => sub { |
| 68 | my $c = shift; |
| 69 | deprecated 'Deprecated "doc_ext_link_to" in favor of "ext_link_to"'; |
| 70 | return $c->ext_link_to(@_); |
| 71 | } |
| 72 | ); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 73 | |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 74 | # Page alert - Under Construction! |
| 75 | $mojo->helper( |
| 76 | under_construction => sub { |
| 77 | my $c = shift; |
| 78 | return $c->tag('p', $c->loc('underConstruction', 'Under Construction!')); |
| 79 | } |
| 80 | ); |
| 81 | |
| 82 | # Page alert - Under Construction! |
| 83 | # DEPRECATED: 2019-10-17 |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 84 | $mojo->helper( |
| 85 | doc_uc => sub { |
| 86 | my $c = shift; |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 87 | deprecated 'Deprecated "doc_uc" in favor of "under_construction"'; |
| 88 | return $c->under_construction |
| 89 | } |
| 90 | ); |
| 91 | |
| 92 | # Page title helper |
| 93 | $mojo->helper( |
| 94 | page_title => sub { |
| 95 | my $c = shift; |
| 96 | return $c->tag('h2' => (id => 'page-top') => $c->stash('title')) |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 97 | } |
| 98 | ); |
| 99 | |
| 100 | $mojo->helper( |
| 101 | doc_opener => sub { |
| 102 | my $c = shift; |
| 103 | my $cb = pop; |
| 104 | my $page = pop; |
| 105 | my $scope = shift; |
| 106 | my $url; |
| 107 | if ($page) { |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 108 | $url = $c->url_with('doc', scope => $scope, page => $page); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 109 | $url->path->canonicalize; |
| 110 | } |
| 111 | else { |
| 112 | $url = $c->url_for('doc_start'); |
| 113 | }; |
| 114 | return $c->link_to($cb->($c), $url); |
| 115 | } |
| 116 | ); |
| 117 | |
| 118 | |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 119 | # DEPRECATED: 2019-10-24 |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 120 | $mojo->helper( |
| 121 | 'doc.url' => sub { |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 122 | deprecated 'Deprecated "doc->url" in favor of direct usage with "url_with"'; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 123 | my $c = shift; |
| 124 | my $page = pop; |
| 125 | my $scope = shift; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 126 | return $c->url_with( |
| Akron | 254fe21 | 2019-10-24 14:33:28 +0200 | [diff] [blame] | 127 | 'doc', |
| 128 | page => $page, |
| 129 | scope => $scope |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 130 | ); |
| 131 | } |
| 132 | ); |
| 133 | |
| 134 | # Documentation navigation helper |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 135 | # DEPRECATED: 2019-10-24 |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 136 | $mojo->helper( |
| 137 | doc_navi => sub { |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 138 | deprecated 'Deprecated "docnavi" in favor of "navigation"'; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 139 | my $c = shift; |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 140 | return $c->navigation('doc', @_) |
| 141 | } |
| 142 | ); |
| 143 | |
| 144 | # Navigation helper |
| 145 | $mojo->helper( |
| 146 | 'navigation' => sub { |
| 147 | my $c = shift; |
| 148 | my $realm = shift; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 149 | my $items = pop; |
| 150 | my $scope = shift; |
| 151 | |
| Akron | f7ec444 | 2019-10-27 20:01:05 +0100 | [diff] [blame] | 152 | # Take items from central list |
| 153 | unless ($items) { |
| 154 | $items = $navi->{$realm}; |
| Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame^] | 155 | |
| 156 | # Realm has no entries |
| 157 | return '' unless $items; |
| 158 | } |
| 159 | |
| 160 | # Set realm |
| 161 | else { |
| 162 | $navi->{$realm} = $items; |
| Akron | f7ec444 | 2019-10-27 20:01:05 +0100 | [diff] [blame] | 163 | }; |
| 164 | |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 165 | # Create unordered list |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 166 | my $html = '<ul class="nav nav-'.$realm.'">'."\n"; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 167 | |
| 168 | # Embed all link tags |
| 169 | foreach (@$items) { |
| 170 | |
| 171 | my ($active, $url) = 0; |
| 172 | |
| 173 | # There is a fragment! |
| 174 | if (index($_->{id}, '#') == 0) { |
| 175 | |
| 176 | my $part_scope = scalar($scope); |
| 177 | $part_scope =~ s!\/([^\/]+)$!!; |
| 178 | my $page = $1; |
| 179 | my $id = $_->{id}; |
| 180 | $id =~ s/^#//; |
| 181 | |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 182 | $url = $c->url_with($realm, scope => $part_scope, page => $page); |
| 183 | |
| 184 | # Canonicalize (for empty scopes) |
| 185 | $url->path->canonicalize; |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 186 | $url->fragment($id); |
| 187 | } |
| 188 | |
| 189 | # There is no fragment |
| 190 | else { |
| 191 | |
| 192 | # The item is active |
| 193 | if ($c->stash('page') && $c->stash('page') eq $_->{id}) { |
| 194 | $active = 1; |
| 195 | }; |
| 196 | |
| 197 | # Generate url with query parameter inheritance |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 198 | $url = $c->url_with($realm, scope => $scope, page => $_->{id}); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 199 | |
| 200 | # Canonicalize (for empty scopes) |
| 201 | $url->path->canonicalize; |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 202 | $url->fragment('page-top'); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | my @classes; |
| 206 | push(@classes, $_->{'class'}) if $_->{'class'}; |
| 207 | push(@classes, 'active') if $active; |
| 208 | |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 209 | # New list item |
| 210 | $html .= '<li'; |
| 211 | if (@classes) { |
| 212 | $html .= ' class="' . join(' ', @classes) . '"'; |
| 213 | }; |
| 214 | $html .= '>'; |
| 215 | |
| 216 | # Translate title |
| 217 | my $title = $c->loc('Nav_' . $_->{id}, $_->{title}); |
| 218 | |
| 219 | # Generate link |
| 220 | $html .= $c->link_to($title, $url); |
| 221 | |
| 222 | # Set sub entries |
| 223 | if ($_->{items} && ref($_->{items}) eq 'ARRAY') { |
| 224 | $html .= "\n"; |
| 225 | my $subscope = $scope ? scalar($scope) . '/' . $_->{id} : $_->{id}; |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 226 | $html .= $c->navigation($realm, $subscope, $_->{items}); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 227 | $html .= "</li>\n"; |
| 228 | } |
| 229 | else { |
| 230 | $html .= "</li>\n"; |
| 231 | }; |
| 232 | }; |
| 233 | return $html . "</ul>\n"; |
| 234 | } |
| 235 | ); |
| 236 | |
| 237 | |
| 238 | # Create helper for queries in the tutorial |
| 239 | $mojo->helper( |
| 240 | doc_query => sub { |
| 241 | my ($c, $ql, $q, %param) = @_; |
| 242 | |
| 243 | # Query is not supported in the corpus |
| 244 | if ($q =~ s/^\*\*\s*//) { |
| 245 | # Escape query for html embedding |
| 246 | $q = xml_escape $q; |
| 247 | |
| 248 | return b( |
| 249 | '<pre class="query tutorial unsupported">' . |
| 250 | "<code>$q</code>" . |
| 251 | '<span title="' . $c->loc('notAvailInCorpus') . '">*</span>' . |
| 252 | '</pre>'); |
| 253 | }; |
| 254 | |
| 255 | # Escape query for html embedding |
| 256 | $q = xml_escape $q; |
| 257 | |
| 258 | # Return tag |
| 259 | b('<pre class="query tutorial" ' . |
| 260 | qq!data-query="$q" data-query-cutoff="! . |
| 261 | ($param{cutoff} ? 1 : 0) . |
| 262 | '"' . |
| 263 | qq! data-query-language="$ql">! . |
| 264 | '<code>' . $q . '</code>' . |
| 265 | '</pre>' |
| 266 | ); |
| 267 | } |
| 268 | ); |
| 269 | |
| Akron | f7ec444 | 2019-10-27 20:01:05 +0100 | [diff] [blame] | 270 | # Set a navigation list to a realm |
| 271 | $mojo->helper( |
| 272 | 'navi.set' => sub { |
| 273 | my $c = shift; |
| 274 | my $realm = shift; |
| 275 | my $list = shift; |
| 276 | |
| 277 | $navi->{$realm} = $list; |
| 278 | } |
| 279 | ); |
| 280 | |
| Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame^] | 281 | # Add an item to the realm |
| Akron | f7ec444 | 2019-10-27 20:01:05 +0100 | [diff] [blame] | 282 | $mojo->helper( |
| 283 | 'navi.add' => sub { |
| 284 | my $c = shift; |
| 285 | my $realm = shift; |
| 286 | my $navi_realm = ($navi->{$realm} //= []); |
| 287 | my $title = shift; |
| 288 | my $id = shift; |
| 289 | |
| 290 | push @$navi_realm, { |
| 291 | title => $title, |
| 292 | id => $id |
| 293 | } |
| 294 | } |
| 295 | ); |
| Akron | 5999212 | 2019-10-29 11:28:45 +0100 | [diff] [blame^] | 296 | |
| 297 | # Check for existence |
| 298 | $mojo->helper( |
| 299 | 'navi.exists' => sub { |
| 300 | my $c = shift; |
| 301 | my $realm = shift; |
| 302 | unless (exists $navi->{$realm}) { |
| 303 | return 0 ; |
| 304 | }; |
| 305 | return 0 unless ref $navi->{$realm} && @{$navi->{$realm}} > 0; |
| 306 | return 1; |
| 307 | } |
| 308 | ); |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | 1; |
| 312 | |
| 313 | |
| 314 | |
| 315 | __END__ |
| 316 | |
| 317 | =pod |
| 318 | |
| 319 | =encoding utf8 |
| 320 | |
| 321 | =head1 NAME |
| 322 | |
| 323 | Kalamar::Plugin::KalamarHelpers |
| 324 | |
| 325 | |
| 326 | =head1 DESCRIPTION |
| 327 | |
| 328 | L<Kalamar::Plugin::KalamarHelpers> makes Kalamar specific |
| 329 | helpers for Mojolicious available. |
| 330 | |
| 331 | |
| 332 | =head1 HELPERS |
| 333 | |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 334 | =head2 embedded_link_to |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 335 | |
| 336 | %# In templates |
| Akron | 3cfa26d | 2019-10-24 15:17:34 +0200 | [diff] [blame] | 337 | %= embedded_link_to 'doc','Kalamar', 'korap', 'kalamar' |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 338 | |
| Akron | 3cfa26d | 2019-10-24 15:17:34 +0200 | [diff] [blame] | 339 | Create a link to the documentation. Accepts a realm, a title, a scope, and a page. |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 340 | |
| 341 | |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 342 | =head2 ext_link_to |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 343 | |
| 344 | %# In templates |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 345 | %= ext_link_to 'GitHub', "https://github.com/KorAP/Koral" |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 346 | |
| 347 | Creates a link to an external page, that will be opened in the top frame, |
| Akron | 9490e3b | 2019-10-17 12:26:29 +0200 | [diff] [blame] | 348 | in case it's in an embedded frame. |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 349 | |
| 350 | =head2 doc_uc |
| 351 | |
| 352 | %# In templates |
| 353 | %= doc_uc |
| 354 | |
| 355 | Generates an C<Under Construction> notification. |
| 356 | |
| 357 | |
| 358 | =head2 doc_opener |
| 359 | |
| 360 | Currently not used. |
| 361 | |
| 362 | |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 363 | =head2 navigation |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 364 | |
| Akron | d512ea6 | 2019-10-24 15:50:04 +0200 | [diff] [blame] | 365 | Returns an HTML representation of a navigation structure |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 366 | based on active navigation items. |
| 367 | |
| 368 | |
| 369 | =head2 doc_query |
| 370 | |
| 371 | %# In templates |
| 372 | %= doc_query poliqarp => 'Baum' |
| 373 | |
| 374 | Creates an interactive query view for documentation purposes. |
| 375 | |
| 376 | |
| 377 | =head1 COPYRIGHT AND LICENSE |
| 378 | |
| 379 | Copyright (C) 2015-2019, L<IDS Mannheim|http://www.ids-mannheim.de/> |
| 380 | Author: L<Nils Diewald|http://nils-diewald.de/> |
| 381 | |
| 382 | Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/> |
| 383 | Corpus Analysis Platform at the |
| 384 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 385 | member of the |
| hebasta | 21b7baf | 2019-12-16 10:32:43 +0100 | [diff] [blame] | 386 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de> |
| Akron | 41a190a | 2019-10-16 18:01:02 +0200 | [diff] [blame] | 387 | and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project, |
| 388 | funded by the |
| 389 | L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>. |
| 390 | |
| 391 | Kalamar is free software published under the |
| 392 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>. |
| 393 | |
| 394 | =cut |