<commit_before>"use strict";

var path = require('path');
var fs = require('fs-extra');
var ae = require('./annotation_exporter');
var of = require('./object_factory');
var Pattern = of.Pattern;
var pa = require('./pattern_assembler');
var pattern_assembler = new pa();
var eol = require('os').EOL;

// PRIVATE FUNCTIONS

function addToPatternPaths(patternlab, pattern) {
  if (!patternlab.patternPaths[pattern.patternGroup]) {
    patternlab.patternPaths[pattern.patternGroup] = {};
  }
  patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name;
}

//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 type, subtype, further nested dirs, or filename is an underscore
  if (pattern.isPattern) {
    if (
      pattern.relPath.charAt(0) === '_' ||
      pattern.relPath.indexOf('/_') > -1
    ) {
      return true;
    }
  }
  return false;
}

// 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 = [];

  //todo this loop can be made more efficient
  if (styleguideExcludes && styleguideExcludes.length) {
    for (var i = 0; i < patternlab.patterns.length; i++) {

      var pattern = patternlab.patterns[i];

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

      //this is meant to be a homepage that is not present anywhere else
      if (pattern.patternPartial === patternlab.config.defaultPattern) {
        if (patternlab.config.debug) {
          console.log('omitting ' + pattern.patternPartial + ' from styleguide patterns because it is defined as a defaultPattern');
        }
        continue;
      }

      var partial = pattern.patternPartial;
      var partialType = partial.substring(0, partial.indexOf('-'));
      var isExcluded = (styleguideExcludes.indexOf(partialType) > -1);
      if (!isExcluded) {
        styleguidePatterns.push(pattern);
      }
    }
  } else {
    for (i = 0; i < patternlab.patterns.length; i++) {
      var pattern = patternlab.patterns[i];

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

      //this is meant to be a homepage that is not present anywhere else
      if (pattern.patternPartial === patternlab.config.defaultPattern) {
        if (patternlab.config.debug) {
          console.log('omitting ' + pattern.patternPartial + ' from styleguide patterns because it is defined as a defaultPattern');
        }
        continue;
      }

      styleguidePatterns.push(pattern);
    }
  }
  return styleguidePatterns;
}

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

    var pattern = patternlab.patterns[i];

    //exclude any named defaultPattern from the navigation.
    //this is meant to be a homepage that is not navigable
    if (pattern.patternPartial === patternlab.config.defaultPattern) {
      if (patternlab.config.debug) {
        console.log('omitting ' + pattern.patternPartial + ' from navigation because it is defined as a defaultPattern');
      }

      //add to patternPaths before continuing
      addToPatternPaths(patternlab, pattern);

      continue;
    }

    var patternSubTypeName;
    var patternSubTypeItemName;
    var flatPatternItem;
    var patternSubType;
    var patternSubTypeItem;
    var viewAllPatternSubTypeItem;

    //check if the patternType already exists
    var patternTypeIndex = patternlab.patternTypeIndex.indexOf(pattern.patternGroup);
    var patternType;
    if (patternTypeIndex === -1) {
      //add the patternType
      patternType = new of.oPatternType(pattern.patternGroup);

      //add the patternType.
      patternlab.patternTypes.push(patternType);
      patternlab.patternTypeIndex.push(pattern.patternGroup);

      //create a property for this group in the patternlab.viewAllPaths array
      //the viewall property needs to be first
      patternlab.viewAllPaths[pattern.patternGroup] = {viewall: ''};

    } else {
      //else find the patternType
      patternType = patternlab.patternTypes[patternTypeIndex];
    }

    //get the patternSubType.
    //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
    patternSubTypeName = pattern.subdir.split(path.sep).pop().replace(/^\d*\-/, '');

    //get the patternSubTypeItem
    patternSubTypeItemName = pattern.patternName.replace(/[\-~]/g, ' ');

    //assume the patternSubTypeItem does not exist.
    patternSubTypeItem = new of.oPatternSubTypeItem(patternSubTypeItemName);
    patternSubTypeItem.patternPath = pattern.patternLink;
    patternSubTypeItem.patternPartial = pattern.patternPartial;

    var isExcluded = isPatternExcluded(pattern);

    //add to patternPaths
    if (!isExcluded) {
      addToPatternPaths(patternlab, pattern);

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

      //test whether the pattern structure is flat or not - usually due to a template or page
      flatPatternItem = patternSubTypeName === pattern.patternGroup;
    }

    //if it is flat - we should not add the pattern to patternPaths
    if (flatPatternItem) {
      if (!isExcluded) {
        //add the patternSubType to patternItems
        patternType.patternItems.push(patternSubTypeItem);
      }

    } else {
      // skip underscore-prefixed files
      if (!isExcluded) {
        // only do this if pattern is included
        //check to see if patternSubType exists
        var patternTypeItemsIndex = patternType.patternTypeItemsIndex.indexOf(patternSubTypeName);
        if (patternTypeItemsIndex === -1) {
          patternSubType = new of.oPatternSubType(patternSubTypeName);

          //add the patternSubType and patternSubTypeItem
          patternSubType.patternSubtypeItems.push(patternSubTypeItem);
          patternSubType.patternSubtypeItemsIndex.push(patternSubTypeItemName);
          patternType.patternTypeItems.push(patternSubType);
          patternType.patternTypeItemsIndex.push(patternSubTypeName);<commit_msg>stronger viewall logic<commit_after>"use strict";

var path = require('path');
var fs = require('fs-extra');
var ae = require('./annotation_exporter');
var of = require('./object_factory');
var Pattern = of.Pattern;
var pa = require('./pattern_assembler');
var pattern_assembler = new pa();
var eol = require('os').EOL;

// PRIVATE FUNCTIONS

function addToPatternPaths(patternlab, pattern) {
  if (!patternlab.patternPaths[pattern.patternGroup]) {
    patternlab.patternPaths[pattern.patternGroup] = {};
  }
  patternlab.patternPaths[pattern.patternGroup][pattern.patternBaseName] = pattern.name;
}

//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 type, subtype, further nested dirs, or filename is an underscore
  if (pattern.isPattern) {
    if (
      pattern.relPath.charAt(0) === '_' ||
      pattern.relPath.indexOf('/_') > -1
    ) {
      return true;
    }
  }
  return false;
}

// 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 = [];

  //todo this loop can be made more efficient
  if (styleguideExcludes && styleguideExcludes.length) {
    for (var i = 0; i < patternlab.patterns.length; i++) {

      var pattern = patternlab.patterns[i];

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

      //this is meant to be a homepage that is not present anywhere else
      if (pattern.patternPartial === patternlab.config.defaultPattern) {
        if (patternlab.config.debug) {
          console.log('omitting ' + pattern.patternPartial + ' from styleguide patterns because it is defined as a defaultPattern');
        }
        continue;
      }

      var partial = pattern.patternPartial;
      var partialType = partial.substring(0, partial.indexOf('-'));
      var isExcluded = (styleguideExcludes.indexOf(partialType) > -1);
      if (!isExcluded) {
        styleguidePatterns.push(pattern);
      }
    }
  } else {
    for (i = 0; i < patternlab.patterns.length; i++) {
      var pattern = patternlab.patterns[i];

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

      //this is meant to be a homepage that is not present anywhere else
      if (pattern.patternPartial === patternlab.config.defaultPattern) {
        if (patternlab.config.debug) {
          console.log('omitting ' + pattern.patternPartial + ' from styleguide patterns because it is defined as a defaultPattern');
        }
        continue;
      }

      styleguidePatterns.push(pattern);
    }
  }
  return styleguidePatterns;
}

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

    var pattern = patternlab.patterns[i];

    //exclude any named defaultPattern from the navigation.
    //this is meant to be a homepage that is not navigable
    if (pattern.patternPartial === patternlab.config.defaultPattern) {
      if (patternlab.config.debug) {
        console.log('omitting ' + pattern.patternPartial + ' from navigation because it is defined as a defaultPattern');
      }

      //add to patternPaths before continuing
      addToPatternPaths(patternlab, pattern);

      continue;
    }

    var patternSubTypeName;
    var patternSubTypeItemName;
    var flatPatternItem;
    var patternSubType;
    var patternSubTypeItem;
    var viewAllPatternSubTypeItem;

    //check if the patternType already exists
    var patternTypeIndex = patternlab.patternTypeIndex.indexOf(pattern.patternGroup);
    var patternType;
    if (patternTypeIndex === -1) {
      //add the patternType
      patternType = new of.oPatternType(pattern.patternGroup);

      //add the patternType.
      patternlab.patternTypes.push(patternType);
      patternlab.patternTypeIndex.push(pattern.patternGroup);

      //create a property for this group in the patternlab.viewAllPaths array
      //the viewall property needs to be first
      patternlab.viewAllPaths[pattern.patternGroup] = {viewall: ''};

    } else {
      //else find the patternType
      patternType = patternlab.patternTypes[patternTypeIndex];
    }

    //get the patternSubType.
    //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
    patternSubTypeName = pattern.subdir.split(path.sep).pop().replace(/^\d*\-/, '');

    //get the patternSubTypeItem
    patternSubTypeItemName = pattern.patternName.replace(/[\-~]/g, ' ');

    //assume the patternSubTypeItem does not exist.
    patternSubTypeItem = new of.oPatternSubTypeItem(patternSubTypeItemName);
    patternSubTypeItem.patternPath = pattern.patternLink;
    patternSubTypeItem.patternPartial = pattern.patternPartial;

    var isExcluded = isPatternExcluded(pattern);

    if (!isExcluded) {
      //add to patternPaths
      addToPatternPaths(patternlab, pattern);

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

      //test whether the pattern structure is flat or not - usually due to a template or page
      flatPatternItem = patternSubTypeName === pattern.patternGroup;
    }

    //need to define patternTypeItemsIndex and patternSubType whether or not excluded
    var patternTypeItemsIndex = patternType.patternTypeItemsIndex.indexOf(patternSubTypeName);
    if (patternTypeItemsIndex === -1) {
      patternSubType = new of.oPatternSubType(patternSubTypeName);
    }

    if (!isExcluded) {
      //if it is flat - we should not add the pattern to patternPaths
      if (flatPatternItem) {
        //add the patternSubType to patternItems
        patternType.patternItems.push(patternSubTypeItem);

      } else {
        //check again whether the patternSubType exists
        if (patternTypeItemsIndex === -1) {
          //add the patternSubType and patternSubTypeItem
          patternSubType.patternSubtypeItems.push(patternSubTypeItem);
          patternSubType.patternSubtypeItemsIndex.push(patternSubTypeItemName);
          patternType.patternTypeItems.push(patternSubType);
          patternType.patternTypeItemsIndex.push(patternSubTypeName);
{'fraction_non_alphanumeric': 0.09597545833028998, 'fraction_numerical': 0.0013877729895551822, 'mean_word_length': 3.1129468308801442, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '5804903', 'n_tokens_mistral': 3803, 'n_tokens_neox': 3736, 'n_words': 1050}