diff --git a/src/PatternLab/Builder.php b/src/PatternLab/Builder.php index a869e329..fb91068f 100644 --- a/src/PatternLab/Builder.php +++ b/src/PatternLab/Builder.php @@ -391,8 +391,8 @@ protected function generateViewAllPages() { $patternData["patternPartial"] = "viewall-".$patternStoreData["nameDash"]."-all"; // add the pattern lab specific mark-up - $partials["patternLabHead"] = $stringLoader->render(array("string" => $htmlHead, "data" => array("cacheBuster" => $partials["cacheBuster"]))); - $partials["patternLabFoot"] = $stringLoader->render(array("string" => $htmlFoot, "data" => array("cacheBuster" => $partials["cacheBuster"], "patternData" => json_encode($patternData)))); + $globalData["patternLabHead"] = $stringLoader->render(array("string" => $htmlHead, "data" => array("cacheBuster" => $partials["cacheBuster"]))); + $globalData["patternLabFoot"] = $stringLoader->render(array("string" => $htmlFoot, "data" => array("cacheBuster" => $partials["cacheBuster"], "patternData" => json_encode($patternData)))); // render the parts and join them $header = $patternLoader->render(array("pattern" => $patternHead, "data" => $globalData)); diff --git a/src/PatternLab/Data.php b/src/PatternLab/Data.php index e0da3fc7..75b05654 100644 --- a/src/PatternLab/Data.php +++ b/src/PatternLab/Data.php @@ -125,7 +125,7 @@ public static function gather($options = array()) { $pathName = $file->getPathname(); $pathNameClean = str_replace($sourceDir."/","",$pathName); - if (!$hidden && (($ext == "json") || ($ext == "yaml"))) { + if (!$hidden && (($ext == "json") || ($ext == "yaml") || ($ext == "yml"))) { if ($isListItems === false) { @@ -137,7 +137,7 @@ public static function gather($options = array()) { JSON::lastErrorMsg($pathNameClean,$jsonErrorMessage,$data); } - } else if ($ext == "yaml") { + } else if (($ext == "yaml") || ($ext == "yml")) { $file = file_get_contents($pathName); diff --git a/src/PatternLab/InstallerUtil.php b/src/PatternLab/InstallerUtil.php index 66a3fab5..fcc7fa99 100644 --- a/src/PatternLab/InstallerUtil.php +++ b/src/PatternLab/InstallerUtil.php @@ -349,14 +349,15 @@ protected static function parseComponentList($packageName,$sourceBase,$destinati // iterate over the returned objects foreach ($finder as $file) { - $ext = $file->getExtension(); + $ext = $file->getExtension(); + $pathName = $file->getPathname(); if ($ext == "css") { - $componentTypes["stylesheets"][] = str_replace($sourceBase.$source,$destination,$file->getPathname()); + $componentTypes["stylesheets"][] = str_replace(DIRECTORY_SEPARATOR,"/",str_replace($sourceBase.$source,$destination,$pathName)); } else if ($ext == "js") { - $componentTypes["javascripts"][] = str_replace($sourceBase.$source,$destination,$file->getPathname()); + $componentTypes["javascripts"][] = str_replace(DIRECTORY_SEPARATOR,"/",str_replace($sourceBase.$source,$destination,$pathName)); } else if ($ext == $templateExtension) { - $componentTypes["templates"][] = str_replace($sourceBase.$source,$destination,$file->getPathname()); + $componentTypes["templates"][] = str_replace(DIRECTORY_SEPARATOR,"/",str_replace($sourceBase.$source,$destination,$pathName)); } } diff --git a/src/PatternLab/PatternData.php b/src/PatternLab/PatternData.php index 39129553..f3d05a05 100644 --- a/src/PatternLab/PatternData.php +++ b/src/PatternLab/PatternData.php @@ -116,15 +116,14 @@ public static function gather($options = array()) { if (!is_dir(Config::getOption("patternSourceDir"))) { Console::writeError("having patterns is important. please make sure you've installed a starterkit and/or that ".Console::getHumanReadablePath(Config::getOption("patternSourceDir"))." exists..."); } - $patternObjects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(Config::getOption("patternSourceDir")), \RecursiveIteratorIterator::SELF_FIRST); - $patternObjects->setFlags(\FilesystemIterator::SKIP_DOTS); + + $patternSourceDir = Config::getOption("patternSourceDir"); + $patternObjects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($patternSourceDir, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); // sort the returned objects $patternObjects = iterator_to_array($patternObjects); ksort($patternObjects); - $patternSourceDir = Config::getOption("patternSourceDir"); - foreach ($patternObjects as $name => $object) { $ext = $object->getExtension(); @@ -387,18 +386,52 @@ public static function getRules() { } - /** - * Load all of the rules related to Pattern Data + +/** + * Load all of the rules related to Pattern Data, plus any extra add-ons */ public static function loadRules($options) { + // First handle default PL rules, minus any being disabled foreach (glob(__DIR__."/PatternData/Rules/*.php") as $filename) { $ruleName = str_replace(".php","",str_replace(__DIR__."/PatternData/Rules/","",$filename)); - if ($ruleName[0] != "_") { + + $disabledRules = array(); + if (Config::getOption("disabledPatternRules")) { + $disabledRules = Config::getOption("disabledPatternRules"); + } + + // Load all rules that aren't on the disabledPatternRules list + if (($ruleName[0] != "_") && (!in_array($ruleName, $disabledRules))) { $ruleClass = "\PatternLab\PatternData\Rules\\".$ruleName; $rule = new $ruleClass($options); self::setRule($ruleName, $rule); } } + + // Then handle any extra rules to add on top of the default PL rules + $extraPatternRulesDir = Config::getOption("sourceDir") . '/_extensions/rules'; // Default extra rules location to check + if (Config::getOption("extraPatternRulesDir")) { + $extraPatternRulesDir = Config::getOption("extraPatternRulesDir"); + } + + if (is_dir($extraPatternRulesDir)){ + foreach (glob($extraPatternRulesDir . "/*.php") as $filename) { + $ruleName = str_replace(".php","",str_replace($extraPatternRulesDir . "/","",$filename)); + + $extraRules = array(); + if (Config::getOption("extraPatternRules")) { + $extraRules = Config::getOption("extraPatternRules"); + } + + // Only load extra rules that are on the extraPatternRules list + if (($ruleName[0] != "_") && (in_array($ruleName, $extraRules))) { + require_once($filename); // Pull in extra rule so we can use it + $ruleClass = "\PatternLab\PatternData\Rules\\".$ruleName; + $rule = new $ruleClass($options); + self::setRule($ruleName, $rule); + } + } + } } /** diff --git a/src/PatternLab/PatternData/Helpers/LineageHelper.php b/src/PatternLab/PatternData/Helpers/LineageHelper.php index 61185d65..1d5676e5 100644 --- a/src/PatternLab/PatternData/Helpers/LineageHelper.php +++ b/src/PatternLab/PatternData/Helpers/LineageHelper.php @@ -53,6 +53,51 @@ public function run() { foreach ($foundLineages as $lineage) { + /** + * Fix for Pattern Lab Lineages when using Twig Namespaces. + * Converts the full file path to PL-friendly shorthand so + * they are internally registered. + * + * 1. Only handle instances where we aren't or can't use the + * shorthand PL path reference in templates, specifically + * in Twig / D8 when we need to use Twig namespaces in + * our template paths. + * 2. Strip off the @ sign at the beginning of our $lineage string. + * 3. Break apart the full lineage path based on any slashes that + * may exist. + * 4. Store the length of our broken up path for reference below + * 5. Store the first part of the string up to the first slash "/" + * 6. Now grab the last part of the pattern key, based on the length + * of the path we previously exploded. + * 7. Remove any "_" from pattern Name. + * 8. Remove any potential prefixed numbers or number + dash + * combos on our Pattern Name. + * 9. Strip off the pattern path extension (.twig, + * .mustache, etc) if it exists. + * 10. If the pattern name parsed had an extension, + * re-assign our Pattern Name to that. + * 11. Finally, re-assign $lineage to the default PL pattern key. + */ + + if ($lineage[0] == '@') { /* [1] */ + $lineage = ltrim($lineage, '@'); /* [2] */ + $lineageParts = explode('/', $lineage); /* [3] */ + $length = count($lineageParts); /* [4] */ + $patternType = $lineageParts[0]; /* [5] */ + + $patternName = $lineageParts[$length - 1]; /* [6] */ + $patternName = ltrim($patternName, '_'); /* [7] */ + $patternName = preg_replace('/^[0-9\-]+/', '', + $patternName); /* [8] */ + + $patternNameStripped = explode('.' . $patternExtension, $patternName); /* [9] */ + + if (count($patternNameStripped) > 1) { /* [10] */ + $patternName = $patternNameStripped[0]; + } + $lineage = $patternType . "-" . $patternName; /* [11] */ + } + if (PatternData::getOption($lineage)) { $patternLineages[] = array("lineagePattern" => $lineage, diff --git a/src/PatternLab/PatternData/Rule.php b/src/PatternLab/PatternData/Rule.php index a7c3ab25..bbb500b4 100644 --- a/src/PatternLab/PatternData/Rule.php +++ b/src/PatternLab/PatternData/Rule.php @@ -104,6 +104,9 @@ protected function compareProp($name, $propCompare, $exact = false) { protected function getPatternName($pattern, $clean = true) { $patternBits = explode("-",$pattern,2); $patternName = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern; + // replace possible dots with dashes. pattern names cannot contain dots + // since they are used as id/class names in the styleguidekit. + $patternName = str_replace('.', '-', $patternName); return ($clean) ? (str_replace("-"," ",$patternName)) : $patternName; } diff --git a/src/PatternLab/PatternData/Rules/PseudoPatternRule.php b/src/PatternLab/PatternData/Rules/PseudoPatternRule.php index 426332e9..8575790c 100644 --- a/src/PatternLab/PatternData/Rules/PseudoPatternRule.php +++ b/src/PatternLab/PatternData/Rules/PseudoPatternRule.php @@ -171,7 +171,14 @@ public function run($depth, $ext, $path, $pathName, $name) { $patternStoreData["data"] = is_array($patternData) ? array_replace_recursive($patternDataBase, $patternData) : $patternDataBase; // if the pattern data store already exists make sure it is merged and overwrites this data - $patternStoreData = (PatternData::checkOption($patternStoreKey)) ? array_replace_recursive(PatternData::getOption($patternStoreKey),$patternStoreData) : $patternStoreData; + if (PatternData::checkOption($patternStoreKey)) { + $existingData = PatternData::getOption($patternStoreKey); + if (array_key_exists('nameClean', $existingData)) { + // don't overwrite nameClean + unset($patternStoreData['nameClean']); + } + $patternStoreData = array_replace_recursive($existingData, $patternStoreData); + } PatternData::setOption($patternStoreKey, $patternStoreData); }