[% USE site = XML::Simple( 
       'sitemap.xml'
       ForceArray = ['page']
       KeyAttr = ['' ]
   )
%]

[% INCLUDE explore node=site;
   BLOCK explore;
     # nodes is already set by breadcrumbs.tmpl
     PROCESS crumblist urllist=[] cnode=nodes.0;

     itemcount = 0;
     FOREACH page IN node.page;
       IF page.url;
         IF page.url.match("^http://") OR page.url.match("^https://");
           page.path = page.url;
         ELSE;
           page.path = (page.url == '.') ? toplink : "$toplink$page.url";
         END;
       ELSE;
         # add page id to current path to get page path
         page.path = "$toplink$path/$page.id";

         # add suffix for a section (/index.html) or page (.html)
         suffix    = page.page ? '/index.en.html' : '.en.html';
         page.url  = "${page.path}${suffix}";
       END;

       # add complete URL to sitemap lookup table
       site.url2page.${page.url} = page;

       t_url = "$toplink${template.name}"|replace('/index.html|/index.en.html','/')|replace('.html|.en.html','');
       in_list = 0;
       FOREACH url IN urllist;
         IF page.path == "$toplink$url";
           in_list = 1;
         END;
       END;

       IF !page.hide || in_list;
         IF itemcount == 0;
           node == site ?  "<ul id=\"nav\">" : "\n<ul>";
           "\n";
         END;
         itemcount = itemcount + 1;
         # print some debugging info
         # "page: $page.id\n  path: $page.path\n   url: $page.url\n" | stderr;
         "\t<li><a href=\"$page.path\">$page.title</a>";

         # process and child nodes
         INCLUDE explore node=page path=page.path
           IF page.page AND (t_url.match("^$page.path") OR in_list);

         "</li>\n";
       END;
     END;
     IF itemcount > 0;
       "</ul>\n";
     END;
   END;

   BLOCK crumblist;
     IF cnode && cnode.getName() == "page";
       urllist.push(cnode.getAttribute("url"));
       PROCESS crumblist cnode=cnode.getParentNode();
     END;
   END;

   IF template.name == "index.en.html";
     "<div style=\"font-size: x-small; text-align: center;\">\n";
     "<span><a href=\"https://go.nordvpn.net/aff_c?offer_id=15&aff_id=81347&url_id=902\" target=\"_blank\">
         Partnered with NordVPN</a></span><br>\n";
     "<img src=\"/images/nordvpn-banner.png\" width=\"240\" height=\"240\">\n";
     "</div>";
   END;
%]
