<% 
  var
    generateSitemapList = function(the_head, the_tail) {
      var 
        filter = /(\.html$)/,
        replace = /(\.html$)|(^index\.html$)/,
        tree = function (head, tail) {
          var 
            output = '',
            tree_output = '',
            data, content;
          for (var key in head) {
            var 
              val = head[key];
            if (key !== '.git') {
              if (key == '_data') {
                data = val;
              } else if (key == '_contents') {
                content = val;
              } else {
                tree_output += tree(val, tail + key + "/");
              }
            }
          }
        
          if (content && data) {
            for (var i in content) {
              var 
                file = content[i],
                slug = file.replace(replace, ""),
                file_data = data && data[slug] ? data[slug] : {},
                now = new Date(),
                show_item = false,
                title, date;
              if (filter.test(file) && !(/^404\.html/).test(file)) {
                show_item = true;
                if (file_data) {
                  var is_draft = file_data.is_draft || false;
                  date = file_data.date_pub ? new Date(file_data.date_pub) : '';
                  title = file_data.title || '';
                  if (is_draft || title === '' || (date && date.getTime() > now.getTime())) {
                    show_item = false;
                  }
                }
              }
              if (show_item) {
//              file = file.replace(replace, "");
                output += 
                  '\n<li><a href="' + tail + file + '">' + title + '</a></li>\n';                
              }
            }      
          }
          return output + tree_output;
        };
      return tree(the_head, the_tail);
    };
%>

<urlset>
  <%- generateSitemapList(public, "/") %>
</urlset>

