blob: 855a92b47d7a5efbaa03aea14a81ad94bcc0f24b [file] [log] [blame]
Nils Diewald2fe12e12015-03-06 16:47:06 +00001package Kalamar::Plugin::KalamarHelpers;
2use Mojo::Base 'Mojolicious::Plugin';
Nils Diewald7148c6f2015-05-04 15:07:53 +00003use Mojo::JSON 'decode_json';
4use Mojo::JSON::Pointer;
5use Mojo::ByteStream 'b';
6use Mojo::Util qw/xml_escape/;
Nils Diewald2fe12e12015-03-06 16:47:06 +00007
8sub register {
9 my ($plugin, $mojo) = @_;
10
Nils Diewald7148c6f2015-05-04 15:07:53 +000011 # 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 Diewaldb8cd4d62015-05-08 00:37:46 +000019 my $base = $c->url_for('index');
20 if ($base->path->parts->[0]) {
Akron0dc10902017-09-01 18:00:16 +020021 $base->path->trailing_slash(1);
Nils Diewaldb8cd4d62015-05-08 00:37:46 +000022 };
23
Nils Diewald7148c6f2015-05-04 15:07:53 +000024 # If there is a different base - append this as a base
Nils Diewaldb541d712015-05-08 00:41:14 +000025 $url->query([base => $base // '/']);
Nils Diewald7148c6f2015-05-04 15:07:53 +000026
27 $url->fragment($scope);
28
Akron0dc10902017-09-01 18:00:16 +020029 return $c->tag(
30 'object',
31 data => $url,
32 type => 'image/svg+xml',
33 alt => $c->loc('korap_overview'),
34 id => 'overview'
Nils Diewald7148c6f2015-05-04 15:07:53 +000035 );
36 }
37 );
38
39 # Documentation link
Nils Diewalda898dac2015-05-06 21:04:16 +000040 # TODO: Support opener mechanism, so the link will open the embedded
41 # documentation in case it's not there.
Nils Diewald7148c6f2015-05-04 15:07:53 +000042 $mojo->helper(
43 doc_link_to => sub {
44 my $c = shift;
45 my $title = shift;
46 my $page = pop;
47 my $scope = shift;
Nils Diewald61e6ff52015-05-07 17:26:50 +000048
49 ($page, my $fragment) = split '#', $page;
50
51 my $url = $c->url_with(
Akron0dc10902017-09-01 18:00:16 +020052 'doc',
53 scope => $scope,
54 page => $page
Nils Diewald61e6ff52015-05-07 17:26:50 +000055 );
56
57 $url->fragment($fragment) if $fragment;
58
Nils Diewald7148c6f2015-05-04 15:07:53 +000059 return $c->link_to(
Akron0dc10902017-09-01 18:00:16 +020060 $title,
61 $url,
62 class => 'doc-link'
Nils Diewald7148c6f2015-05-04 15:07:53 +000063 );
64 }
65 );
66
Nils Diewald61e6ff52015-05-07 17:26:50 +000067 $mojo->helper(
68 doc_ext_link_to => sub {
69 my $c = shift;
70 return $c->link_to(@_, target => '_top');
71 }
72 );
73
Nils Diewald7148c6f2015-05-04 15:07:53 +000074
75 # Documentation alert - Under Construction!
76 $mojo->helper(
77 doc_uc => sub {
Nils Diewald023c6712015-05-21 20:12:30 +000078 my $c = shift;
79 return $c->tag('p', $c->loc('underConstruction'));
Nils Diewald7148c6f2015-05-04 15:07:53 +000080 }
81 );
82
Nils Diewalda898dac2015-05-06 21:04:16 +000083 $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) {
Akron0dc10902017-09-01 18:00:16 +020091 $url = $c->url_for('doc', page => $page, scope => $scope);
92 $url->path->canonicalize;
Nils Diewalda898dac2015-05-06 21:04:16 +000093 }
94 else {
Akron0dc10902017-09-01 18:00:16 +020095 $url = $c->url_for('doc_start');
Nils Diewalda898dac2015-05-06 21:04:16 +000096 };
97 return $c->link_to($cb->($c), $url);
98 }
99 );
100
Nils Diewald7148c6f2015-05-04 15:07:53 +0000101
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
Akrone8235be2016-06-27 11:02:18 +0200110 my $html = '<ul class="nav">'."\n";
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111
112 # Embed all link tags
113 foreach (@$items) {
114
Akron0dc10902017-09-01 18:00:16 +0200115 my ($active, $url) = 0;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000116
Akron0dc10902017-09-01 18:00:16 +0200117 # There is a fragment!
118 if (index($_->{id}, '#') == 0) {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000119
Akron0dc10902017-09-01 18:00:16 +0200120 my $part_scope = scalar($scope);
121 $part_scope =~ s!\/([^\/]+)$!!;
122 my $page = $1;
123 my $id = $_->{id};
124 $id =~ s/^#//;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000125
Akron0dc10902017-09-01 18:00:16 +0200126 $url = $c->url_with(
127 'doc',
128 'scope' => $part_scope,
129 'page' => $page
130 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000131
Akron0dc10902017-09-01 18:00:16 +0200132 $url->fragment($id);
133 }
Nils Diewald7148c6f2015-05-04 15:07:53 +0000134
Akron0dc10902017-09-01 18:00:16 +0200135 # There is no fragment
136 else {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000137
Akron0dc10902017-09-01 18:00:16 +0200138 # The item is active
139 if ($c->stash('page') && $c->stash('page') eq $_->{id}) {
140 $active = 1;
141 };
Nils Diewald7148c6f2015-05-04 15:07:53 +0000142
Akron0dc10902017-09-01 18:00:16 +0200143 # Generate url with query parameter inheritance
144 $url = $c->url_with(
145 'doc',
146 'scope' => $scope,
147 'page' => $_->{id}
148 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000149
Akron0dc10902017-09-01 18:00:16 +0200150 # Canonicalize (for empty scopes)
151 $url->path->canonicalize;
Akron1120a582017-10-17 12:29:16 +0200152 $url->fragment('tutorial-top');
Akron0dc10902017-09-01 18:00:16 +0200153 };
Nils Diewald7148c6f2015-05-04 15:07:53 +0000154
Akron0dc10902017-09-01 18:00:16 +0200155 my @classes;
156 push(@classes, $_->{'class'}) if $_->{'class'};
157 push(@classes, 'active') if $active;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000158
159
Akron0dc10902017-09-01 18:00:16 +0200160 # New list item
161 $html .= '<li';
162 if (@classes) {
163 $html .= ' class="' . join(' ', @classes) . '"';
164 };
165 $html .= '>';
Nils Diewald7148c6f2015-05-04 15:07:53 +0000166
Akron0dc10902017-09-01 18:00:16 +0200167 # Translate title
168 my $title = $c->loc('Nav_' . $_->{id}, $_->{title});
Nils Diewald7148c6f2015-05-04 15:07:53 +0000169
Akron0dc10902017-09-01 18:00:16 +0200170 # Generate link
171 $html .= $c->link_to($title, $url);
Nils Diewald7148c6f2015-05-04 15:07:53 +0000172
Akron0dc10902017-09-01 18:00:16 +0200173 # 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 Diewald7148c6f2015-05-04 15:07:53 +0000183 };
184 return $html . "</ul>\n";
185 }
186 );
187
188
189 # Create helper for queries in the tutorial
190 $mojo->helper(
Nils Diewald61e6ff52015-05-07 17:26:50 +0000191 doc_query => sub {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000192 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" ' .
Akron0dc10902017-09-01 18:00:16 +0200199 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 Diewald7148c6f2015-05-04 15:07:53 +0000206 }
207 );
208
209
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000210 # Check for test port
Nils Diewald2fe12e12015-03-06 16:47:06 +0000211 $mojo->helper(
212 kalamar_test_port => sub {
213 my $c = shift;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000214
215 # Test port is defined in the stash
Nils Diewald2fe12e12015-03-06 16:47:06 +0000216 if (defined $c->stash('kalamar.test_port')) {
Akron0dc10902017-09-01 18:00:16 +0200217 return $c->stash('kalamar.test_port');
Nils Diewald2fe12e12015-03-06 16:47:06 +0000218 };
219
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000220 # Check the port
Nils Diewald2fe12e12015-03-06 16:47:06 +0000221 if ($c->req->url->to_abs->port == 6666 ||
Akron0dc10902017-09-01 18:00:16 +0200222 $c->app->mode =~ m/^development|test$/) {
223 $c->stash('kalamar.test_port' => 1);
224 return 1;
Nils Diewald2fe12e12015-03-06 16:47:06 +0000225 };
226
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000227 # No test port
Nils Diewald2fe12e12015-03-06 16:47:06 +0000228 $c->stash('kalamar.test_port' => 0);
229 return 0;
230 });
231};
232
Nils Diewald7148c6f2015-05-04 15:07:53 +0000233
Nils Diewald2fe12e12015-03-06 16:47:06 +00002341;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000235
236
237__END__
238
Nils Diewald023c6712015-05-21 20:12:30 +0000239=pod
240
241=encoding utf8
242
243=head1 NAME
244
245Kalamar::Plugin::KalamarHelpers
246
247
248=head1 DESCRIPTION
249
250L<Kalamar::Plugin::KalamarHelpers> makes Kalamar specific
251helpers 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
261Create 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
269Creates a link to an external page, that will be opened in the top frame,
270in case it's in an embedded frame (used in the tutorial).
271
272=head2 doc_uc
273
274 %# In templates
275 %= doc_uc
276
277Generates an C<Under Construction> notification.
278
279
280=head2 doc_opener
281
282Currently not used.
283
284
285=head2 doc_navi
286
287Returns an HTML representation of the documentation navigation,
288based on active navigation items.
289
290
291=head2 doc_query
292
293 %# In templates
294 %= doc_query poliqarp => 'Baum'
295
296Creates 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
306Returns 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
314Returns a modified and relatified overview svg image to be embedded
315as an object in templates.
316
317
318=head1 COPYRIGHT AND LICENSE
319
320Copyright (C) 2015, L<IDS Mannheim|http://www.ids-mannheim.de/>
321Author: L<Nils Diewald|http://nils-diewald.de/>
322
323Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
324Corpus Analysis Platform at the
325L<Institute for the German Language (IDS)|http://ids-mannheim.de/>,
326member of the
327L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
328and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
329funded by the
330L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
331
332Kalamar is free software published under the
Akron456abd92015-06-02 15:07:21 +0200333L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
Nils Diewald023c6712015-05-21 20:12:30 +0000334
335=cut