blob: 97e3d556042df3ffd342e223a5446c11a7ff1f93 [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]) {
21 $base->path->trailing_slash(1);
22 };
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
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 Diewalda898dac2015-05-06 21:04:16 +000039 # TODO: Support opener mechanism, so the link will open the embedded
40 # documentation in case it's not there.
Nils Diewald7148c6f2015-05-04 15:07:53 +000041 $mojo->helper(
42 doc_link_to => sub {
43 my $c = shift;
44 my $title = shift;
45 my $page = pop;
46 my $scope = shift;
Nils Diewald61e6ff52015-05-07 17:26:50 +000047
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 Diewald7148c6f2015-05-04 15:07:53 +000058 return $c->link_to(
59 $title,
Nils Diewald61e6ff52015-05-07 17:26:50 +000060 $url,
61 class => 'doc-link'
Nils Diewald7148c6f2015-05-04 15:07:53 +000062 );
63 }
64 );
65
Nils Diewald61e6ff52015-05-07 17:26:50 +000066 $mojo->helper(
67 doc_ext_link_to => sub {
68 my $c = shift;
69 return $c->link_to(@_, target => '_top');
70 }
71 );
72
Nils Diewald7148c6f2015-05-04 15:07:53 +000073
74 # Documentation alert - Under Construction!
75 $mojo->helper(
76 doc_uc => sub {
Nils Diewald023c6712015-05-21 20:12:30 +000077 my $c = shift;
78 return $c->tag('p', $c->loc('underConstruction'));
Nils Diewald7148c6f2015-05-04 15:07:53 +000079 }
80 );
81
Nils Diewalda898dac2015-05-06 21:04:16 +000082 $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 Diewald7148c6f2015-05-04 15:07:53 +0000100
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 Diewald61e6ff52015-05-07 17:26:50 +0000187 doc_query => sub {
Nils Diewald7148c6f2015-05-04 15:07:53 +0000188 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 Diewaldfccfbcb2015-04-29 20:48:19 +0000206 # Check for test port
Nils Diewald2fe12e12015-03-06 16:47:06 +0000207 $mojo->helper(
208 kalamar_test_port => sub {
209 my $c = shift;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000210
211 # Test port is defined in the stash
Nils Diewald2fe12e12015-03-06 16:47:06 +0000212 if (defined $c->stash('kalamar.test_port')) {
213 return $c->stash('kalamar.test_port');
214 };
215
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000216 # Check the port
Nils Diewald2fe12e12015-03-06 16:47:06 +0000217 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 Diewaldfccfbcb2015-04-29 20:48:19 +0000223 # No test port
Nils Diewald2fe12e12015-03-06 16:47:06 +0000224 $c->stash('kalamar.test_port' => 0);
225 return 0;
226 });
227};
228
Nils Diewald7148c6f2015-05-04 15:07:53 +0000229
Nils Diewald2fe12e12015-03-06 16:47:06 +00002301;
Nils Diewald7148c6f2015-05-04 15:07:53 +0000231
232
233__END__
234
Nils Diewald023c6712015-05-21 20:12:30 +0000235=pod
236
237=encoding utf8
238
239=head1 NAME
240
241Kalamar::Plugin::KalamarHelpers
242
243
244=head1 DESCRIPTION
245
246L<Kalamar::Plugin::KalamarHelpers> makes Kalamar specific
247helpers 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
257Create 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
265Creates a link to an external page, that will be opened in the top frame,
266in case it's in an embedded frame (used in the tutorial).
267
268=head2 doc_uc
269
270 %# In templates
271 %= doc_uc
272
273Generates an C<Under Construction> notification.
274
275
276=head2 doc_opener
277
278Currently not used.
279
280
281=head2 doc_navi
282
283Returns an HTML representation of the documentation navigation,
284based on active navigation items.
285
286
287=head2 doc_query
288
289 %# In templates
290 %= doc_query poliqarp => 'Baum'
291
292Creates 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
302Returns 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
310Returns a modified and relatified overview svg image to be embedded
311as an object in templates.
312
313
314=head1 COPYRIGHT AND LICENSE
315
316Copyright (C) 2015, L<IDS Mannheim|http://www.ids-mannheim.de/>
317Author: L<Nils Diewald|http://nils-diewald.de/>
318
319Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
320Corpus Analysis Platform at the
321L<Institute for the German Language (IDS)|http://ids-mannheim.de/>,
322member of the
323L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
324and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
325funded by the
326L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
327
328Kalamar is free software published under the
Akron456abd92015-06-02 15:07:21 +0200329L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
Nils Diewald023c6712015-05-21 20:12:30 +0000330
331=cut