Cleanup helper plugins to separate page helpers from functional helpers
Change-Id: Ic9a925c416f89de9dd6493c29d4caeb2a3663312
diff --git a/Changes b/Changes
index 440cf1e..1d3d800 100755
--- a/Changes
+++ b/Changes
@@ -1,4 +1,6 @@
-0.37 2019-09-25
+0.37 2019-10-16
+ - Removed deprecated 'kalamar_test_port' helper.
+ - Separated KalamarHelpers and KalamarPages.
0.36 2019-09-19
- Rename all cookies to be independent
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index f1a01b0..cc41eed 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -146,6 +146,7 @@
foreach (
'TagHelpers::MailToChiffre', # Obfuscate email addresses
'KalamarHelpers', # Specific Helpers for Kalamar
+ 'KalamarPages', # Page Helpers for Kalamar
'KalamarErrors', # Specific Errors for Kalamar
'KalamarUser', # Specific Helpers for Kalamar Users
'ClientIP', # Get client IP from X-Forwarded-For
diff --git a/lib/Kalamar/Plugin/KalamarHelpers.pm b/lib/Kalamar/Plugin/KalamarHelpers.pm
index 4731e4d..4745860 100644
--- a/lib/Kalamar/Plugin/KalamarHelpers.pm
+++ b/lib/Kalamar/Plugin/KalamarHelpers.pm
@@ -1,14 +1,8 @@
package Kalamar::Plugin::KalamarHelpers;
-use Mojo::Base 'Mojolicious::Plugin';
-use Mojo::JSON qw/decode_json true false/;
use Mojo::ByteStream 'b';
-use Mojo::Util qw/xml_escape deprecated/;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::Util qw/deprecated/;
-# TODO:
-# Add documentation plugin to programmatically
-# create documentation navigation as a content_block
-# so custom routes to custom templates can easily
-# be configured
sub register {
my ($plugin, $mojo) = @_;
@@ -49,220 +43,6 @@
}
);
- # Documentation link
- # TODO: Support opener mechanism, so the link will open the embedded
- # documentation in case it's not there.
- $mojo->helper(
- doc_link_to => sub {
- my $c = shift;
- my $title = shift;
- my $page = pop;
- my $scope = shift;
-
- ($page, my $fragment) = split '#', $page;
-
- my $url = $c->doc->url($scope, $page);
- $url->fragment($fragment) if $fragment;
-
- return $c->link_to(
- $title,
- $url,
- class => 'doc-link'
- );
- }
- );
-
- $mojo->helper(
- doc_ext_link_to => sub {
- my $c = shift;
- return $c->link_to(@_, target => '_top');
- }
- );
-
-
- # Documentation alert - Under Construction!
- $mojo->helper(
- doc_uc => sub {
- my $c = shift;
- return $c->tag('p', $c->loc('underConstruction'));
- }
- );
-
- $mojo->helper(
- doc_opener => sub {
- my $c = shift;
- my $cb = pop;
- my $page = pop;
- my $scope = shift;
- my $url;
- if ($page) {
- $url = $c->doc->url($scope, $page);
- $url->path->canonicalize;
- }
- else {
- $url = $c->url_for('doc_start');
- };
- return $c->link_to($cb->($c), $url);
- }
- );
-
-
- $mojo->helper(
- 'doc.url' => sub {
- my $c = shift;
- my $page = pop;
- my $scope = shift;
- if ($scope) {
- return $c->url_with(
- 'doc2',
- page => $page,
- scope => $scope
- );
- };
-
- return $c->url_with(
- 'doc1',
- page => $page
- );
- }
- );
-
- # Documentation navigation helper
- $mojo->helper(
- doc_navi => sub {
- my $c = shift;
- my $items = pop;
- my $scope = shift;
-
- # Create unordered list
- my $html = '<ul class="nav">'."\n";
-
- # Embed all link tags
- foreach (@$items) {
-
- my ($active, $url) = 0;
-
- # There is a fragment!
- if (index($_->{id}, '#') == 0) {
-
- my $part_scope = scalar($scope);
- $part_scope =~ s!\/([^\/]+)$!!;
- my $page = $1;
- my $id = $_->{id};
- $id =~ s/^#//;
-
- $url = $c->doc->url($part_scope, $page);
- $url->fragment($id);
- }
-
- # There is no fragment
- else {
-
- # The item is active
- if ($c->stash('page') && $c->stash('page') eq $_->{id}) {
- $active = 1;
- };
-
- # Generate url with query parameter inheritance
- $url = $c->doc->url($scope, $_->{id});
-
- # Canonicalize (for empty scopes)
- $url->path->canonicalize;
- $url->fragment('tutorial-top');
- };
-
- my @classes;
- push(@classes, $_->{'class'}) if $_->{'class'};
- push(@classes, 'active') if $active;
-
-
- # New list item
- $html .= '<li';
- if (@classes) {
- $html .= ' class="' . join(' ', @classes) . '"';
- };
- $html .= '>';
-
- # Translate title
- my $title = $c->loc('Nav_' . $_->{id}, $_->{title});
-
- # Generate link
- $html .= $c->link_to($title, $url);
-
- # Set sub entries
- if ($_->{items} && ref($_->{items}) eq 'ARRAY') {
- $html .= "\n";
- my $subscope = $scope ? scalar($scope) . '/' . $_->{id} : $_->{id};
- $html .= $c->doc_navi($subscope, $_->{items});
- $html .= "</li>\n";
- }
- else {
- $html .= "</li>\n";
- };
- };
- return $html . "</ul>\n";
- }
- );
-
-
- # Create helper for queries in the tutorial
- $mojo->helper(
- doc_query => sub {
- my ($c, $ql, $q, %param) = @_;
-
- # Query is not supported in the corpus
- if ($q =~ s/^\*\*\s*//) {
- # Escape query for html embedding
- $q = xml_escape $q;
-
- return b(
- '<pre class="query tutorial unsupported">' .
- "<code>$q</code>" .
- '<span title="' . $c->loc('notAvailInCorpus') . '">*</span>' .
- '</pre>');
- };
-
- # Escape query for html embedding
- $q = xml_escape $q;
-
- # Return tag
- b('<pre class="query tutorial" ' .
- qq!data-query="$q" data-query-cutoff="! .
- ($param{cutoff} ? 1 : 0) .
- '"' .
- qq! data-query-language="$ql">! .
- '<code>' . $q . '</code>' .
- '</pre>'
- );
- }
- );
-
-
- # Check for test port
- $mojo->helper(
- kalamar_test_port => sub {
- my $c = shift;
-
- # 2018-11-15
- deprecated 'kalamar_test_port is deprecated and will be removed';
-
- # Test port is defined in the stash
- if (defined $c->stash('kalamar.test_port')) {
- return $c->stash('kalamar.test_port');
- };
-
- # Check the port
- if ($c->req->url->to_abs->port == 6666 ||
- $c->app->mode =~ m/^development|test$/) {
- $c->stash('kalamar.test_port' => 1);
- return 1;
- };
-
- # No test port
- $c->stash('kalamar.test_port' => 0);
- return 0;
- });
-
# Establish 'search_results' taghelper
# This is based on Mojolicious::Plugin::Search
@@ -393,104 +173,3 @@
1;
-
-
-__END__
-
-=pod
-
-=encoding utf8
-
-=head1 NAME
-
-Kalamar::Plugin::KalamarHelpers
-
-
-=head1 DESCRIPTION
-
-L<Kalamar::Plugin::KalamarHelpers> makes Kalamar specific
-helpers for Mojolicious available.
-
-
-=head1 HELPERS
-
-=head2 doc_link_to
-
- %# In templates
- %= doc_link_to 'Kalamar', 'korap', 'kalamar'
-
-Create a link to the documentation. Accepts a name, a scope, and a page.
-
-
-=head2 doc_ext_link_to
-
- %# In templates
- %= doc_ext_link_to 'GitHub', "https://github.com/KorAP/Koral"
-
-Creates a link to an external page, that will be opened in the top frame,
-in case it's in an embedded frame (used in the tutorial).
-
-=head2 doc_uc
-
- %# In templates
- %= doc_uc
-
-Generates an C<Under Construction> notification.
-
-
-=head2 doc_opener
-
-Currently not used.
-
-
-=head2 doc_navi
-
-Returns an HTML representation of the documentation navigation,
-based on active navigation items.
-
-
-=head2 doc_query
-
- %# In templates
- %= doc_query poliqarp => 'Baum'
-
-Creates an interactive query view for documentation purposes.
-
-
-=head2 kalamar_test_port
-
- # In controllers
- if ($c->kalamar_test_port) {
- $c->app->log->debug('Kalamar runs on test port');
- };
-
-Returns a C<true> value in case Kalamar runs on test port C<6666>.
-
-
-=head2 korap_overview
-
- %# In templates
- %= korap_overview 'kalamar'
-
-Returns a modified and relatified overview svg image to be embedded
-as an object in templates.
-
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright (C) 2015-2018, L<IDS Mannheim|http://www.ids-mannheim.de/>
-Author: L<Nils Diewald|http://nils-diewald.de/>
-
-Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
-Corpus Analysis Platform at the
-L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
-member of the
-L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
-and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
-funded by the
-L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
-
-Kalamar is free software published under the
-L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
-
-=cut
diff --git a/lib/Kalamar/Plugin/KalamarPages.pm b/lib/Kalamar/Plugin/KalamarPages.pm
new file mode 100644
index 0000000..858ca91
--- /dev/null
+++ b/lib/Kalamar/Plugin/KalamarPages.pm
@@ -0,0 +1,289 @@
+package Kalamar::Plugin::KalamarPages;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::JSON qw/decode_json true false/;
+use Mojo::ByteStream 'b';
+use Mojo::Util qw/xml_escape deprecated/;
+
+
+# TODO:
+# Add documentation plugin to programmatically
+# create documentation navigation as a content_block
+# so custom routes to custom templates can easily
+# be configured
+sub register {
+ my ($plugin, $mojo) = @_;
+
+ # Documentation link
+ # TODO: Support opener mechanism, so the link will open the embedded
+ # documentation in case it's not there.
+ $mojo->helper(
+ doc_link_to => sub {
+ my $c = shift;
+ my $title = shift;
+ my $page = pop;
+ my $scope = shift;
+
+ ($page, my $fragment) = split '#', $page;
+
+ my $url = $c->doc->url($scope, $page);
+ $url->fragment($fragment) if $fragment;
+
+ return $c->link_to(
+ $title,
+ $url,
+ class => 'doc-link'
+ );
+ }
+ );
+
+ $mojo->helper(
+ doc_ext_link_to => sub {
+ my $c = shift;
+ return $c->link_to(@_, target => '_top');
+ }
+ );
+
+
+ # Documentation alert - Under Construction!
+ $mojo->helper(
+ doc_uc => sub {
+ my $c = shift;
+ return $c->tag('p', $c->loc('underConstruction'));
+ }
+ );
+
+ $mojo->helper(
+ doc_opener => sub {
+ my $c = shift;
+ my $cb = pop;
+ my $page = pop;
+ my $scope = shift;
+ my $url;
+ if ($page) {
+ $url = $c->doc->url($scope, $page);
+ $url->path->canonicalize;
+ }
+ else {
+ $url = $c->url_for('doc_start');
+ };
+ return $c->link_to($cb->($c), $url);
+ }
+ );
+
+
+ $mojo->helper(
+ 'doc.url' => sub {
+ my $c = shift;
+ my $page = pop;
+ my $scope = shift;
+ if ($scope) {
+ return $c->url_with(
+ 'doc2',
+ page => $page,
+ scope => $scope
+ );
+ };
+
+ return $c->url_with(
+ 'doc1',
+ page => $page
+ );
+ }
+ );
+
+ # Documentation navigation helper
+ $mojo->helper(
+ doc_navi => sub {
+ my $c = shift;
+ my $items = pop;
+ my $scope = shift;
+
+ # Create unordered list
+ my $html = '<ul class="nav">'."\n";
+
+ # Embed all link tags
+ foreach (@$items) {
+
+ my ($active, $url) = 0;
+
+ # There is a fragment!
+ if (index($_->{id}, '#') == 0) {
+
+ my $part_scope = scalar($scope);
+ $part_scope =~ s!\/([^\/]+)$!!;
+ my $page = $1;
+ my $id = $_->{id};
+ $id =~ s/^#//;
+
+ $url = $c->doc->url($part_scope, $page);
+ $url->fragment($id);
+ }
+
+ # There is no fragment
+ else {
+
+ # The item is active
+ if ($c->stash('page') && $c->stash('page') eq $_->{id}) {
+ $active = 1;
+ };
+
+ # Generate url with query parameter inheritance
+ $url = $c->doc->url($scope, $_->{id});
+
+ # Canonicalize (for empty scopes)
+ $url->path->canonicalize;
+ $url->fragment('tutorial-top');
+ };
+
+ my @classes;
+ push(@classes, $_->{'class'}) if $_->{'class'};
+ push(@classes, 'active') if $active;
+
+
+ # New list item
+ $html .= '<li';
+ if (@classes) {
+ $html .= ' class="' . join(' ', @classes) . '"';
+ };
+ $html .= '>';
+
+ # Translate title
+ my $title = $c->loc('Nav_' . $_->{id}, $_->{title});
+
+ # Generate link
+ $html .= $c->link_to($title, $url);
+
+ # Set sub entries
+ if ($_->{items} && ref($_->{items}) eq 'ARRAY') {
+ $html .= "\n";
+ my $subscope = $scope ? scalar($scope) . '/' . $_->{id} : $_->{id};
+ $html .= $c->doc_navi($subscope, $_->{items});
+ $html .= "</li>\n";
+ }
+ else {
+ $html .= "</li>\n";
+ };
+ };
+ return $html . "</ul>\n";
+ }
+ );
+
+
+ # Create helper for queries in the tutorial
+ $mojo->helper(
+ doc_query => sub {
+ my ($c, $ql, $q, %param) = @_;
+
+ # Query is not supported in the corpus
+ if ($q =~ s/^\*\*\s*//) {
+ # Escape query for html embedding
+ $q = xml_escape $q;
+
+ return b(
+ '<pre class="query tutorial unsupported">' .
+ "<code>$q</code>" .
+ '<span title="' . $c->loc('notAvailInCorpus') . '">*</span>' .
+ '</pre>');
+ };
+
+ # Escape query for html embedding
+ $q = xml_escape $q;
+
+ # Return tag
+ b('<pre class="query tutorial" ' .
+ qq!data-query="$q" data-query-cutoff="! .
+ ($param{cutoff} ? 1 : 0) .
+ '"' .
+ qq! data-query-language="$ql">! .
+ '<code>' . $q . '</code>' .
+ '</pre>'
+ );
+ }
+ );
+
+}
+
+1;
+
+
+
+__END__
+
+=pod
+
+=encoding utf8
+
+=head1 NAME
+
+Kalamar::Plugin::KalamarHelpers
+
+
+=head1 DESCRIPTION
+
+L<Kalamar::Plugin::KalamarHelpers> makes Kalamar specific
+helpers for Mojolicious available.
+
+
+=head1 HELPERS
+
+=head2 doc_link_to
+
+ %# In templates
+ %= doc_link_to 'Kalamar', 'korap', 'kalamar'
+
+Create a link to the documentation. Accepts a name, a scope, and a page.
+
+
+=head2 doc_ext_link_to
+
+ %# In templates
+ %= doc_ext_link_to 'GitHub', "https://github.com/KorAP/Koral"
+
+Creates a link to an external page, that will be opened in the top frame,
+in case it's in an embedded frame (used in the tutorial).
+
+=head2 doc_uc
+
+ %# In templates
+ %= doc_uc
+
+Generates an C<Under Construction> notification.
+
+
+=head2 doc_opener
+
+Currently not used.
+
+
+=head2 doc_navi
+
+Returns an HTML representation of the documentation navigation,
+based on active navigation items.
+
+
+=head2 doc_query
+
+ %# In templates
+ %= doc_query poliqarp => 'Baum'
+
+Creates an interactive query view for documentation purposes.
+
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2015-2019, L<IDS Mannheim|http://www.ids-mannheim.de/>
+Author: L<Nils Diewald|http://nils-diewald.de/>
+
+Kalamar is developed as part of the L<KorAP|http://korap.ids-mannheim.de/>
+Corpus Analysis Platform at the
+L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
+member of the
+L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/en/about-us/leibniz-competition/projekte-2011/2011-funding-line-2/>
+and supported by the L<KobRA|http://www.kobra.tu-dortmund.de> project,
+funded by the
+L<Federal Ministry of Education and Research (BMBF)|http://www.bmbf.de/en/>.
+
+Kalamar is free software published under the
+L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
+
+=cut
diff --git a/t/docnavi.t b/t/docnavi.t
index 576db84..dc1e18a 100644
--- a/t/docnavi.t
+++ b/t/docnavi.t
@@ -15,7 +15,7 @@
$app->routes->get('/doc/*scope/:page')->to(cb => sub {})->name('doc2');
# Load plugin to test
-$app->plugin('KalamarHelpers');
+$app->plugin('KalamarPages');
my $languages = [qw/en de/];
$app->plugin('Localize' => {