Pattern Library Utilities

convertTwigIncludes

method
exports.convertTwigIncludes()

@param Type Description
options Object pattern importer options
twigContent String content from a twig file

Convert file paths in twig include statements

exports.convertTwigIncludes = function (options, twigContent) {
  'use strict';

  if (!options.convertCategoryTitles) {
    return twigContent;
  }

  // regex expression to find twig include code blocks
  var twigIncludeRegex = /{%.*?include.*?%}/g;

  var twigIncludes;

  // go through twig's contents and find the includes
  while ((twigIncludes = twigIncludeRegex.exec(twigContent)) !== null) {

    // get clean path from twig include statement
    var includePath = utility.extractTwigIncludePath(twigIncludes[0]);

    // get the new include path
    var newIncludePath = utility.createNewCategoryPath(options, includePath[1]);

    // replace old include path with new one
    twigContent = twigContent.replace(includePath[1], newIncludePath);

  }

  return twigContent;
};

createNewCategoryPath

method
exports.createNewCategoryPath()

@param Type Description
options object object of options
includePath String current category path

Create new category path

exports.createNewCategoryPath = function (options, includePath) {
  'use strict';

  // split up the include path
  var paths = includePath.split('/');

  var newIncludePath = [];

  // convert main category
  var one = utility.categoryNameConverter(options.convertCategoryTitlesData['categories'], paths[0]);
  newIncludePath.push(one);

  // check paths for four parts - denotes a subcategory
  if (paths.length === 4) {
    var two = utility.categoryNameConverter(options.convertCategoryTitlesData.subcategories[one], paths[1]);
    newIncludePath.push(two);
  }

  // add our twig file's name
  newIncludePath.push(paths[paths.length - 1]);

  // return the new include path
  return newIncludePath.join('/');
};

extractTwigIncludePath

method
exports.extractTwigIncludePath()

@param Type Description
twigInclude String twig include statement

Extracts the path for a file from a twig include statement using regex

exports.extractTwigIncludePath = function (twigInclude) {
  'use strict';

  // regex searches for single or double quotes with a .twig inside them
  var includeRegex = /['"](.+\.twig)['"]/;
  return includeRegex.exec(twigInclude);
};