<commit_before>/*
 * patternlab-node - v1.2.0 - 2016
 *
 * <NAME>, <NAME>, and the web community.
 * Licensed under the MIT license.
 *
 * Many thanks to <NAME> and <NAME> for inspiration, encouragement, and advice.
 *
 */

"use strict";

var path = require('path');
var fs = require('fs-extra');
var of = require('./object_factory');


// PRIVATE FUNCTIONS

function addToPatternPaths(patternlab, bucketName, pattern) {
  //this is messy, could use a refactor.
  patternlab.patternPaths[bucketName][pattern.patternName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName;
}

//todo: refactor this as a method on the pattern object itself once we merge dev with pattern-engines branch
function isPatternExcluded(pattern) {
  // returns whether or not the first character of the pattern filename is an underscore, or excluded
  return pattern.fileName.charAt(0) === '_';
}

// Returns the array of patterns to be rendered in the styleguide view and
// linked to in the pattern navigation. Checks if patterns are excluded.
function assembleStyleguidePatterns(patternlab) {
  var styleguideExcludes = patternlab.config.styleGuideExcludes;
  var styleguidePatterns = [];

  if (styleguideExcludes && styleguideExcludes.length) {
    for (var i = 0; i < patternlab.patterns.length; i++) {

      // skip underscore-prefixed files
      if (isPatternExcluded(patternlab.patterns[i])) {
        if (patternlab.config.debug) {
          console.log('Omitting ' + patternlab.patterns[i].key + " from styleguide pattern exclusion.");
        }
        continue;
      }

      var key = patternlab.patterns[i].key;
      var typeKey = key.substring(0, key.indexOf('-'));
      var isExcluded = (styleguideExcludes.indexOf(typeKey) > -1);
      if (!isExcluded) {
        styleguidePatterns.push(patternlab.patterns[i]);
      }
    }
  } else {
    styleguidePatterns = patternlab.patterns;
  }

  return styleguidePatterns;
}

function buildNavigation(patternlab) {
  for (var i = 0; i < patternlab.patterns.length; i++) {

    var pattern = patternlab.patterns[i];
    var bucketName = pattern.name.replace(/\\/g, '-').split('-')[1];

    // skip underscore-prefixed files. don't create a bucket on account of an underscored pattern
    if (isPatternExcluded(pattern)) {
      continue;
    }

    var navItemName;
    var navSubItemName;
    var flatPatternItem;
    var navItem;
    var navSubItem;
    var navViewAllItem;

    //get the navItem
    //if there is one or more slashes in the subdir, get everything after
    //the last slash. if no slash, get the whole subdir string and strip
    //any numeric + hyphen prefix
    navItemName = pattern.subdir.split('/').pop().replace(/^\d*\-/, '');
    //get the navSubItem
    navSubItemName = pattern.patternName.replace(/-/g, ' ');


    //check if the bucket already exists
    var bucketIndex = patternlab.bucketIndex.indexOf(bucketName);
    if (bucketIndex === -1) {
      //add the bucket
      var bucket = new of.oBucket(bucketName);

      //add patternPath and viewAllPath
      patternlab.patternPaths[bucketName] = {};
      patternlab.viewAllPaths[bucketName] = {};

      //test whether the pattern struture is flat or not - usually due to a template or page
      flatPatternItem = false;
      if (navItemName === bucketName) {
        flatPatternItem = true;
      }

      //assume the navItem does not exist.
      navItem = new of.oNavItem(navItemName);

      //assume the navSubItem does not exist.
      navSubItem = new of.oNavSubItem(navSubItemName);
      navSubItem.patternPath = pattern.patternLink;
      navSubItem.patternPartial = bucketName + "-" + pattern.patternName; //add the hyphenated name

      //add the patternState if it exists
      if (pattern.patternState) {
        navSubItem.patternState = pattern.patternState;
      }

      //if it is flat - we should not add the pattern to patternPaths
      if (flatPatternItem) {

        bucket.patternItems.push(navSubItem);

        //add to patternPaths
        addToPatternPaths(patternlab, bucketName, pattern);

      } else {

        bucket.navItems.push(navItem);
        bucket.navItemsIndex.push(navItemName);
        navItem.navSubItems.push(navSubItem);
        navItem.navSubItemsIndex.push(navSubItemName);

        //add to patternPaths
        addToPatternPaths(patternlab, bucketName, pattern);

        //add the navViewAllItem
        navViewAllItem = new of.oNavSubItem("View All");
        navViewAllItem.patternPath = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length) + "/index.html";
        navViewAllItem.patternPartial = "viewall-" + pattern.patternGroup;

        bucket.patternItems.push(navViewAllItem);
        patternlab.viewAllPaths[bucketName].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);

      }

      //add the bucket.
      patternlab.buckets.push(bucket);
      patternlab.bucketIndex.push(bucketName);

      //done

    } else {
      //find the bucket
      bucket = patternlab.buckets[bucketIndex];

      //assume the navSubItem does not exist.
      navSubItem = new of.oNavSubItem(navSubItemName);
      navSubItem.patternPath = pattern.patternLink;
      navSubItem.patternPartial = bucketName + "-" + pattern.patternName; //add the hyphenated name

      //add the patternState if it exists
      if (pattern.patternState) {
        navSubItem.patternState = pattern.patternState;
      }

      //test whether the pattern struture is flat or not - usually due to a template or page
      flatPatternItem = false;
      if (navItemName === bucketName) {
        flatPatternItem = true;
      }

      //if it is flat - we should not add the pattern to patternPaths
      if (flatPatternItem) {
        //add the navItem to patternItems
        bucket.patternItems.push(navSubItem);

        //add to patternPaths<commit_msg>factor out navSubItem creation<commit_after>/*
 * patternlab-node - v1.2.0 - 2016
 *
 * <NAME>, <NAME>, and the web community.
 * Licensed under the MIT license.
 *
 * Many thanks to <NAME> and <NAME> for inspiration, encouragement, and advice.
 *
 */

"use strict";

var path = require('path');
var fs = require('fs-extra');
var of = require('./object_factory');


// PRIVATE FUNCTIONS

function addToPatternPaths(patternlab, bucketName, pattern) {
  //this is messy, could use a refactor.
  patternlab.patternPaths[bucketName][pattern.patternName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName;
}

//todo: refactor this as a method on the pattern object itself once we merge dev with pattern-engines branch
function isPatternExcluded(pattern) {
  // returns whether or not the first character of the pattern filename is an underscore, or excluded
  return pattern.fileName.charAt(0) === '_';
}

// Returns the array of patterns to be rendered in the styleguide view and
// linked to in the pattern navigation. Checks if patterns are excluded.
function assembleStyleguidePatterns(patternlab) {
  var styleguideExcludes = patternlab.config.styleGuideExcludes;
  var styleguidePatterns = [];

  if (styleguideExcludes && styleguideExcludes.length) {
    for (var i = 0; i < patternlab.patterns.length; i++) {

      // skip underscore-prefixed files
      if (isPatternExcluded(patternlab.patterns[i])) {
        if (patternlab.config.debug) {
          console.log('Omitting ' + patternlab.patterns[i].key + " from styleguide pattern exclusion.");
        }
        continue;
      }

      var key = patternlab.patterns[i].key;
      var typeKey = key.substring(0, key.indexOf('-'));
      var isExcluded = (styleguideExcludes.indexOf(typeKey) > -1);
      if (!isExcluded) {
        styleguidePatterns.push(patternlab.patterns[i]);
      }
    }
  } else {
    styleguidePatterns = patternlab.patterns;
  }

  return styleguidePatterns;
}

function buildNavigation(patternlab) {
  for (var i = 0; i < patternlab.patterns.length; i++) {

    var pattern = patternlab.patterns[i];
    var bucketName = pattern.name.replace(/\\/g, '-').split('-')[1];

    // skip underscore-prefixed files. don't create a bucket on account of an underscored pattern
    if (isPatternExcluded(pattern)) {
      continue;
    }

    var navItemName;
    var navSubItemName;
    var flatPatternItem;
    var navItem;
    var navSubItem;
    var navViewAllItem;

    //get the navItem.
    //if there is one or more slashes in the subdir, get everything after
    //the last slash. if no slash, get the whole subdir string and strip
    //any numeric + hyphen prefix
    navItemName = pattern.subdir.split('/').pop().replace(/^\d*\-/, '');

    //get the navSubItem
    navSubItemName = pattern.patternName.replace(/-/g, ' ');

    //assume the navSubItem does not exist.
    navSubItem = new of.oNavSubItem(navSubItemName);
    navSubItem.patternPath = pattern.patternLink;
    navSubItem.patternPartial = bucketName + "-" + pattern.patternName; //add the hyphenated name



    //check if the bucket already exists
    var bucketIndex = patternlab.bucketIndex.indexOf(bucketName);
    if (bucketIndex === -1) {
      //add the bucket
      var bucket = new of.oBucket(bucketName);

      //add patternPath and viewAllPath
      patternlab.patternPaths[bucketName] = {};
      patternlab.viewAllPaths[bucketName] = {};

      //test whether the pattern struture is flat or not - usually due to a template or page
      flatPatternItem = false;
      if (navItemName === bucketName) {
        flatPatternItem = true;
      }

      //assume the navItem does not exist.
      navItem = new of.oNavItem(navItemName);

      //add the patternState if it exists
      if (pattern.patternState) {
        navSubItem.patternState = pattern.patternState;
      }

      //if it is flat - we should not add the pattern to patternPaths
      if (flatPatternItem) {

        bucket.patternItems.push(navSubItem);

        //add to patternPaths
        addToPatternPaths(patternlab, bucketName, pattern);

      } else {

        bucket.navItems.push(navItem);
        bucket.navItemsIndex.push(navItemName);
        navItem.navSubItems.push(navSubItem);
        navItem.navSubItemsIndex.push(navSubItemName);

        //add to patternPaths
        addToPatternPaths(patternlab, bucketName, pattern);

        //add the navViewAllItem
        navViewAllItem = new of.oNavSubItem("View All");
        navViewAllItem.patternPath = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length) + "/index.html";
        navViewAllItem.patternPartial = "viewall-" + pattern.patternGroup;

        bucket.patternItems.push(navViewAllItem);
        patternlab.viewAllPaths[bucketName].viewall = pattern.subdir.slice(0, pattern.subdir.indexOf(pattern.patternGroup) + pattern.patternGroup.length);

      }

      //add the bucket.
      patternlab.buckets.push(bucket);
      patternlab.bucketIndex.push(bucketName);

      //done

    } else {
      //find the bucket
      bucket = patternlab.buckets[bucketIndex];

      //add the patternState if it exists
      if (pattern.patternState) {
        navSubItem.patternState = pattern.patternState;
      }

      //test whether the pattern struture is flat or not - usually due to a template or page
      flatPatternItem = false;
      if (navItemName === bucketName) {
        flatPatternItem = true;
      }

      //if it is flat - we should not add the pattern to patternPaths
      if (flatPatternItem) {
        //add the navItem to patternItems
        bucket.patternItems.push(navSubItem);

        //add to patternPaths
{'fraction_non_alphanumeric': 0.10711829436038514, 'fraction_numerical': 0.002751031636863824, 'mean_word_length': 2.9128826101580896, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '5215855', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3361, 'n_words': 928}