Fixed tutorial view and reimplemented API for new frontend
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index abd4d5e..f465802 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -13,37 +13,42 @@
   my $pkg = b($self->home . '/package.json')->slurp;
   $Kalamar::VERSION = decode_json($pkg)->{version};
 
-  # Load documentation navigation
-  my $navi = b($self->home . '/templates/doc/_nav.json')->slurp;
-
   # Add additional plugin path
   push(@{$self->plugins->namespaces}, __PACKAGE__ . '::Plugin');
 
+  # Set secrets for signed cookies
+  $self->secrets([
+    b($self->home . '/kalamar.secret')->slurp->split("\n")
+  ]);
+
   # Load plugins
-  foreach (qw/Config
-	      Localize
-	      Notifications
-	      DocNavi
-	      KalamarTagHelpers/) {
+  foreach (
+    'Config',                 # Configuration framework
+    'Localize',               # Localization framework
+    'Notifications',          # Client notifications
+    'Search',                 # Abstract Search framework
+    'CHI',                    # Global caching mechanism
+    'TagHelpers::Pagination', # Pagination widget
+    'DocNavi',                # Navigation for documentation
+    'KalamarHelpers',         # Specific Helpers for Kalamar
+    'KalamarTagHelpers'       # Specific Taghelpers for Kalamar
+  ) {
     $self->plugin($_);
   };
 
-  $self->config(navi => decode_json($navi));
-
+  # Configure mail exception
   $self->plugin('MailException' => $self->config('MailException'));
 
+  # Configure documentation navigation
+  my $navi = b($self->home . '/templates/doc/_nav.json')->slurp;
+  $self->config(navi => decode_json($navi));
+
   # Establish routes
   my $r = $self->routes;
 
-  $r->get('/')->to(
-    cb => sub {
-      return shift->render(template => 'intro');
-    });
-
   # Base query page
   $r->get('/')->to('search#query')->name('index');
 
-
   # Documentation
   $r->get('/doc')->to('documentation#page', page => 'korap');
   $r->get('/doc/:page')->to('documentation#page', scope => undef);
@@ -63,10 +68,6 @@
 #    title => 'KorAP - Corpus Analysis Platform'
 #  );
 
-  # Set secret for signed cookies
-  $self->secrets([
-    b($self->home . '/kalamar.secret')->slurp->split("\n")
-  ]);
 
   $self->hook(
     before_dispatch => sub {
@@ -77,22 +78,11 @@
       };
     }) if $self->mode eq 'production';
 
-  $self->hook(before_dispatch => sub {
-      my $c = shift;
-      my $h = $c->res->headers;
-      $h->header( 'Access-Control-Allow-Origin' => '*' );
-      $h->header( 'Access-Control-Allow-Methods' => 'GET, PUT, POST, DELETE, OPTIONS' );
-      $h->header( 'Access-Control-Max-Age' => 3600 );
-      $h->header( 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' );
-    });
 
 
   # Load plugins
-  foreach (qw/CHI
-	      TagHelpers::Pagination
+  foreach (qw/
 	      Number::Commify
-	      Search
-	      KalamarHelpers
 	      /) {
     $self->plugin($_);
   };
diff --git a/lib/Kalamar/Controller/Search.pm b/lib/Kalamar/Controller/Search.pm
index 25954d4..4e18e28 100644
--- a/lib/Kalamar/Controller/Search.pm
+++ b/lib/Kalamar/Controller/Search.pm
@@ -1,8 +1,8 @@
 package Kalamar::Controller::Search;
 use Mojo::Base 'Mojolicious::Controller';
 
-# Add X-Forwarded-For to user agent call everywhere
 
+# Query the KorAP backends and render a template
 sub query {
   my $c = shift;
 
@@ -10,18 +10,8 @@
 
   # No query
   unless ($query) {
-    return $c->render(template => 'index');
-  }
-};
-
-
-1;
-
-
-__END__
-
-# Query the KorAP backends and render a template
-sub query {
+    return $c->render(template => 'intro');
+  };
 
   # Base parameters for remote access
   my %param = (
@@ -29,6 +19,7 @@
     query => $query,
   );
 
+  # May be not relevant
   my $inspect = (scalar $c->param('action') // '') eq 'inspect' ? 1 : 0;
 
   # Just check the serialization non-blocking
@@ -41,32 +32,52 @@
     return;
   };
 
+  # Choose the snippet based on the parameter
   my $template = scalar $c->param('snippet') ? 'snippet' : 'search';
 
   # Search non-blocking
   $c->delay(
     sub {
       my $delay = shift;
+
+      # Search with a callback (async)
       $c->search(
-	cutoff => scalar $c->param('cutoff'),
-	count => scalar $c->param('count'),
+	cutoff     => scalar $c->param('cutoff'),
+	count      => scalar $c->param('count'),
 	start_page => scalar $c->param('p'),
-	cb => $delay->begin,
+	cb         => $delay->begin,
 	%param
       ) if $query;
 
+      # Search resource (async)
       $c->search->resource(
 	type => 'collection',
 	$delay->begin
       );
     },
+
+    # Collected search
     sub {
+
+
+
+      # Render to the template
       return $c->render(template => $template);
     }
   );
 };
 
 
+1;
+
+
+__END__
+
+
+
+
+
+
 # Get informations about a match
 sub match_info {
   my $c = shift;
@@ -121,3 +132,14 @@
 
 
 __END__
+
+
+# Todo: Add X-Forwarded-For to user agent call everywhere
+  $self->hook(before_dispatch => sub {
+      my $c = shift;
+      my $h = $c->res->headers;
+      $h->header( 'Access-Control-Allow-Origin' => '*' );
+      $h->header( 'Access-Control-Allow-Methods' => 'GET, PUT, POST, DELETE, OPTIONS' );
+      $h->header( 'Access-Control-Max-Age' => 3600 );
+      $h->header( 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' );
+    });
diff --git a/lib/Kalamar/Plugin/KalamarHelpers.pm b/lib/Kalamar/Plugin/KalamarHelpers.pm
index 08ff41f..d514ceb 100644
--- a/lib/Kalamar/Plugin/KalamarHelpers.pm
+++ b/lib/Kalamar/Plugin/KalamarHelpers.pm
@@ -1,22 +1,29 @@
 package Kalamar::Plugin::KalamarHelpers;
 use Mojo::Base 'Mojolicious::Plugin';
 
+
+# Helpers for Kalamar
 sub register {
   my ($plugin, $mojo) = @_;
 
+  # Check for test port
   $mojo->helper(
     kalamar_test_port => sub {
       my $c = shift;
+
+      # 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;
     });
diff --git a/lib/Kalamar/Plugin/KalamarTagHelpers.pm b/lib/Kalamar/Plugin/KalamarTagHelpers.pm
index 7a51d7b..d039604 100644
--- a/lib/Kalamar/Plugin/KalamarTagHelpers.pm
+++ b/lib/Kalamar/Plugin/KalamarTagHelpers.pm
@@ -8,15 +8,21 @@
 sub register {
   my ($plugin, $mojo) = @_;
 
-
+  # Embed the korap architecture image
   $mojo->helper(
     korap_overview => sub {
       my $c = shift;
       my $scope = shift;
-      my $base = $c->stash('doc_base') ? '?' . $c->stash('doc_base') : '';
+
+      my $url = $c->url_with('/img/korap-overview.svg');
+
+      # If there is a different base - append this as a base
+      $url->query([base => $c->stash('doc_base') // '/']);
+
+      $url->fragment($scope);
 
       return $c->tag('object',
-	data => $c->url_for('/img/korap-overview.svg' . $base . '#' . $scope),
+	data => $url,
 	type => 'image/svg+xml',
 	alt  => $c->loc('korap_overview'),
 	id   => 'overview'
@@ -26,6 +32,78 @@
 
 
 
+  $mojo->helper(
+    doc_link_to => sub {
+      my $c = shift;
+      my $title = shift;
+      my $page = pop;
+      my $scope = shift;
+      return $c->link_to($title, $c->url_with('doc', scope => $scope, page => $page));
+    }
+  );
+
+  $mojo->helper(
+    doc_uc => sub {
+      return shift->tag('p', 'Under Construction!')
+    }
+  );
+
+
+  # Create helper for queries in the tutorial
+  $mojo->helper(
+    kalamar_tut_query => sub {
+      my ($c, $ql, $q, %param) = @_;
+
+      # 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>'
+		);
+    }
+  );
+
+
+  # Create links in the tutorial that make sure the current position is preserved,
+  # in case the tutorial was opened embedded
+  $mojo->helper(
+    kalamar_tut_link_to => sub {
+      my $c = shift;
+      my $title = shift;
+      my $link = shift;
+      my $host = $c->req->headers->header('X-Forwarded-Host');
+      my $url = $c->url_for($link);
+
+      # Link is part of the embedded tutorial
+      if ($c->param('embedded')) {
+	$url->query({ embedded => 1 });
+	return $c->link_to(
+	  $title,
+	  $url,
+	  onclick => qq!setTutorialPage("$url")!
+	);
+      };
+
+      # Build link
+      return $c->link_to($title, $url);
+    }
+  );
+};
+
+1;
+
+
+__END__
+
+
+
+
   # Create helper for queries in the tutorial
   $mojo->helper(
     kalamar_tut_query => sub {
@@ -155,48 +233,3 @@
 		    '</code></pre>' . $msg);
     }
   );
-
-  $mojo->helper(
-    doc_link_to => sub {
-      my $c = shift;
-      my $title = shift;
-      my $page = pop;
-      my $scope = shift;
-      return $c->link_to($title, $c->url_with('doc', scope => $scope, page => $page));
-    }
-  );
-
-  $mojo->helper(
-    doc_uc => sub {
-      return shift->tag('p', 'Under Construction!')
-    }
-  );
-
-
-  # Create links in the tutorial that make sure the current position is preserved,
-  # in case the tutorial was opened embedded
-  $mojo->helper(
-    kalamar_tut_link_to => sub {
-      my $c = shift;
-      my $title = shift;
-      my $link = shift;
-      my $host = $c->req->headers->header('X-Forwarded-Host');
-      my $url = $c->url_for($link);
-
-      # Link is part of the embedded tutorial
-      if ($c->param('embedded')) {
-	$url->query({ embedded => 1 });
-	return $c->link_to(
-	  $title,
-	  $url,
-	  onclick => qq!setTutorialPage("$url")!
-	);
-      };
-
-      # Build link
-      return $c->link_to($title, $url);
-    }
-  );
-};
-
-1;