<commit_before>var findit = require('findit'), 
    path   = require('path'),
    hl     = require('../../vendor/highlight/lib/highlight').Highlight,
    markdown = require('github-flavored-markdown'),
    mkdirp = require('mkdirp'),
    fs     = require('fs'),
    fs2 = require('../fs2'),
    docs   = require('../docs'),
    weld   = require('weld').weld,
    helpers = require('../helpers');

var content = exports;


content.weld = function(dom, pages) {

  //
  //  Remark: Create a short-cut reference to the jQuery object
  //
  var $ = docs.window.$;


  // Here, we build up the table of contents.
  var toc = helpers.buildToc(docs.src);

  Object.keys(pages).forEach( function (i){

    // parse content if it exists
    if (pages[i].content) {
      var md;
      try {
        md = markdown.parse(pages[i].content.toString());
      } catch (err) {
        md = err.message;
      }
    }

    var metadata = pages[i].metadata;

    //join the author metadata
    if (metadata && metadata.author) {
      metadata.author = docs.content.authors[metadata.author]
                     || { name: metadata.author};
    }


    // Use article view if there's content
    if (md) {

      if (!metadata.title) {
        console.log(i);
        console.log(metadata);
      }

      dom.innerHTML = docs.content.theme['./theme/article.html'].toString();

      var data = { 
        metadata: metadata, 
        content: md,
        toc: toc
      };

      // something breaks here.
      weld(dom, data, {
        map: function(parent, element, key, val) {

          //
          // Create breadcrumb
          //
          if ($(element).hasClass('breadcrumb')) {
            var crumb = '';
            $('.breadcrumb', parent).each(function(i,v){
              crumb += ('/' + $(v).html());
            });
            crumb += ('/' + val);
            $(element).attr('href', crumb);
            $(element).html(val);
            return false
          }

          //Handle dates
          if ($(element).hasClass("date")) {
            var date = val ? new Date(val).toISOString() : undefined;
            if (val) {
              $(element).attr("datetime", new Date(val).toISOString());
              $(element).text(val);
            } else {
              //TODO: If there isn't any metadata, we shouldn't have the 
              //"by x on y" div.
            }
            return false;
          }

          //Handle author metadata
          if ($(element).hasClass("github")) {
            if (val) {
              $(element).append(
                $("<a>")
                  .attr("href", "https://github.com/"+val)
                  .text("[github]")
              );
            }
            return false;
          }

          element.innerHTML = val;
          return false;

        }

      });

    // use directory view
    } else {
      dom.innerHTML = docs.content.theme['./theme/directory.html'];

      //set up the data
      var data = {
        pwd: helpers.unresolve(docs.src, i),
        ls: pages[i].ls,
        toc: toc,
        metadata: metadata
      };

      weld(dom, data, {
        map: function(parent, element, key, val) {

          if ($(element).attr("id") === "toc") {
            element.innerHTML = val;
            return false;
          }

          if ($(element).hasClass("ls")) {
            var title = val.split("/");
            title = title[title.length - 1].replace(/-/g, " ");
            var listing = $("<tr>").attr("class", "ls").append(
              $("<td>").append(
                $("<a>").attr("href", val.replace("pages/", "")).text(title)
              )
            );

            $("tr", $(element)).replaceWith(listing);
            return false;
          }

          //
          // Create breadcrumb
          //
          if ($(element).hasClass('breadcrumb')) {
            var crumb = '';
            $('.breadcrumb', parent).each(function(i,v){
              crumb += ('/' + $(v).html());
            });
            crumb += ('/' + val);
            $(element).attr('href', crumb);
            $(element).html(val);
            return false;
          }

          return true;

        }
      });
    }



    //Give the page a title
    $('title', dom).html('node docs - ' + (metadata && metadata.title) || "");

    //Add some meta tags
    $('meta[name=keywords]', dom).attr('content', (metadata && metadata.tags || []).concat(docs.config.get("tags") || []).join(','));

    //
    // Remark: perform code highlighting, convert only inside <code/>
    // 
    // TODO: the current code highlighting is slow, very slow.
    //

    //
    // Remark: The hilighter tries to hilight "&gt;" as
    //<commit_msg>[refactor] content.js now uses contains one weld for 'both' cases<commit_after>var findit = require('findit'), 
    path   = require('path'),
    hl     = require('../../vendor/highlight/lib/highlight').Highlight,
    markdown = require('github-flavored-markdown'),
    mkdirp = require('mkdirp'),
    fs     = require('fs'),
    fs2 = require('../fs2'),
    docs   = require('../docs'),
    weld   = require('weld').weld,
    helpers = require('../helpers');

var content = exports;


content.weld = function(dom, pages) {

  //
  //  Remark: Create a short-cut reference to the jQuery object
  //
  var $ = docs.window.$;


  // Here, we build up the table of contents.
  var toc = helpers.buildToc(docs.src);

  Object.keys(pages).forEach( function (i){

    // parse content if it exists
    if (pages[i].content) {
      var md;
      try {
        md = markdown.parse(pages[i].content.toString());
      } catch (err) {
        md = err.message;
      }
    }

    var metadata = pages[i].metadata;

    //join the author metadata
    if (metadata && metadata.author) {
      metadata.author = docs.content.authors[metadata.author]
                     || { name: metadata.author};
    }


    // Use article view if there's content
    if (md) {

      dom.innerHTML = docs.content.theme['./theme/article.html'].toString();

      var data = { 
        metadata: metadata, 
        content: md,
        toc: toc
      };

    // use directory view
    } else {
      dom.innerHTML = docs.content.theme['./theme/directory.html'];

      //set up the data
      var data = {
        pwd: helpers.unresolve(docs.src, i),
        ls: pages[i].ls,
        toc: toc,
        metadata: metadata
      };
    }

    weld(dom, data, {
      map: function(parent, element, key, val) {

        //
        // Create breadcrumb
        //
        if ($(element).hasClass('breadcrumb')) {
          var crumb = '';
          $('.breadcrumb', parent).each(function(i,v){
            crumb += ('/' + $(v).html());
          });
          crumb += ('/' + val);
          $(element).attr('href', crumb);
          $(element).html(val);
          return false
        }

        if ($(element).hasClass("ls")) {
          var title = val.split("/");
          title = title[title.length - 1].replace(/-/g, " ");
          var listing = $("<tr>").attr("class", "ls").append(
            $("<td>").append(
              $("<a>").attr("href", val.replace("pages/", "")).text(title)
            )
          );

          $("tr", $(element)).replaceWith(listing);
          return false;
        }

        //Handle dates
        if ($(element).hasClass("date")) {
          var date = val ? new Date(val).toISOString() : undefined;
          if (val) {
            $(element).attr("datetime", new Date(val).toISOString());
            $(element).text(val);
          } else {
            //TODO: If there isn't any metadata, we shouldn't have the 
            //"by x on y" div.
          }
          return false;
        }

        //Handle author metadata
        if ($(element).hasClass("github")) {
          if (val) {
            $(element).append(
              $("<a>")
                .attr("href", "https://github.com/"+val)
                .text("[github]")
            );
          }
          return false;
        }

        element.innerHTML = val;
        return false;
      }

    });


    //Give the page a title
    $('title', dom).html('node docs - ' + (metadata && metadata.title) || "");

    //Add some meta tags
    $('meta[name=keywords]', dom).attr('content', (metadata && metadata.tags || []).concat(docs.config.get("tags") || []).join(','));

    //
    // Remark: perform code highlighting, convert only inside <code/>
    // 
    // TODO: the current code highlighting is slow, very slow.
    //

    //
    // Remark: The hilighter tries to hilight "&gt;" as
    //
{'fraction_non_alphanumeric': 0.172461914176067, 'fraction_numerical': 0.0006977555529712757, 'mean_word_length': 1.604482132041187, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '484182', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2445, 'n_words': 573}