Exemplo n.º 1
0
 /**
  * Gather data from annotations.js and *.md files found in source/_annotations
  *
  * @return {Array}        populates Annotations::$store
  */
 public static function gather()
 {
     // set-up default var
     $annotationsDir = Config::getOption("annotationsDir");
     // set-up the dispatcher
     $dispatcherInstance = Dispatcher::getInstance();
     // dispatch that the data gather has started
     $dispatcherInstance->dispatch("annotations.gatherStart");
     // set-up the comments store
     self::$store["comments"] = array();
     // create the annotations dir if it doesn't exist
     if (!is_dir($annotationsDir)) {
         mkdir($annotationsDir);
     }
     // find the markdown-based annotations
     $finder = new Finder();
     $finder->files()->name("*.md")->in($annotationsDir);
     $finder->sortByName();
     foreach ($finder as $name => $file) {
         $data = array();
         $data[0] = array();
         $text = file_get_contents($file->getPathname());
         $matches = strpos($text, PHP_EOL . "~*~" . PHP_EOL) !== false ? explode(PHP_EOL . "~*~" . PHP_EOL, $text) : array($text);
         foreach ($matches as $match) {
             list($yaml, $markdown) = Documentation::parse($match);
             if (isset($yaml["el"]) || isset($yaml["selector"])) {
                 $data[0]["el"] = isset($yaml["el"]) ? $yaml["el"] : $yaml["selector"];
             } else {
                 $data[0]["el"] = "#someimpossibleselector";
             }
             $data[0]["title"] = isset($yaml["title"]) ? $yaml["title"] : "";
             $data[0]["comment"] = $markdown;
             self::$store["comments"] = array_merge(self::$store["comments"], $data);
         }
     }
     // read in the old style annotations.js, modify the data and generate JSON array to merge
     $data = array();
     $oldStyleAnnotationsPath = $annotationsDir . DIRECTORY_SEPARATOR . "annotations.js";
     if (file_exists($oldStyleAnnotationsPath)) {
         $text = trim(file_get_contents($oldStyleAnnotationsPath));
         $text = str_replace("var comments = ", "", $text);
         if ($text[strlen($text) - 1] == ";") {
             $text = rtrim($text, ";");
         }
         $data = json_decode($text, true);
         if ($jsonErrorMessage = JSON::hasError()) {
             JSON::lastErrorMsg(Console::getHumanReadablePath($oldStyleAnnotationsPath), $jsonErrorMessage, $data);
         }
     }
     // merge in any data from the old file if the json decode was successful
     if (is_array($data) && isset($data["comments"])) {
         self::$store["comments"] = array_merge(self::$store["comments"], $data["comments"]);
     }
     $dispatcherInstance->dispatch("annotations.gatherEnd");
 }
 /**
  * Gather data from annotations.js and *.md files found in source/_annotations
  *
  * @return {Array}        populates Annotations::$store
  */
 public static function gather()
 {
     // set-up default var
     $sourceDir = Config::getOption("sourceDir");
     // set-up the dispatcher
     $dispatcherInstance = Dispatcher::getInstance();
     // dispatch that the data gather has started
     $dispatcherInstance->dispatch("annotations.gatherStart");
     // set-up the comments store
     self::$store["comments"] = array();
     // iterate over all of the files in the annotations dir
     if (!is_dir($sourceDir . "/_annotations")) {
         Console::writeWarning("<path>_annotations/</path><warning> doesn't exist so you won't have annotations...");
         mkdir($sourceDir . "/_annotations");
     }
     // find the markdown-based annotations
     $finder = new Finder();
     $finder->files()->name("*.md")->in($sourceDir . "/_annotations");
     $finder->sortByName();
     foreach ($finder as $name => $file) {
         $data = array();
         $data[0] = array();
         $text = file_get_contents($file->getPathname());
         $matches = strpos($text, PHP_EOL . "~*~" . PHP_EOL) !== false ? explode(PHP_EOL . "~*~" . PHP_EOL, $text) : array($text);
         foreach ($matches as $match) {
             list($yaml, $markdown) = Documentation::parse($match);
             if (isset($yaml["el"]) || isset($yaml["selector"])) {
                 $data[0]["el"] = isset($yaml["el"]) ? $yaml["el"] : $yaml["selector"];
             } else {
                 $data[0]["el"] = "#someimpossibleselector";
             }
             $data[0]["title"] = isset($yaml["title"]) ? $yaml["title"] : "";
             $data[0]["comment"] = $markdown;
             self::$store["comments"] = array_merge(self::$store["comments"], $data);
         }
     }
     // read in the old style annotations.js, modify the data and generate JSON array to merge
     if (file_exists($sourceDir . "/_annotations/annotations.js")) {
         $text = file_get_contents($sourceDir . "/_annotations/annotations.js");
         $text = str_replace("var comments = ", "", $text);
         $text = rtrim($text, ";");
         $data = json_decode($text, true);
         if ($jsonErrorMessage = JSON::hasError()) {
             JSON::lastErrorMsg("_annotations/annotations.js", $jsonErrorMessage, $data);
         }
     }
     // merge in any data from the old file
     self::$store["comments"] = array_merge(self::$store["comments"], $data["comments"]);
     $dispatcherInstance->dispatch("annotations.gatherEnd");
 }
 public function run($depth, $ext, $path, $pathName, $name)
 {
     // load default vars
     $patternTypeDash = PatternData::getPatternTypeDash();
     // should this pattern get rendered?
     $hidden = $name[0] == "_";
     // set-up the names, $name == foo.json
     $pattern = str_replace("." . $ext, "", $name);
     // foo
     $patternDash = $this->getPatternName($pattern, false);
     // foo
     $patternPartial = $patternTypeDash . "-" . $patternDash;
     // atoms-foo
     if (!$hidden) {
         $patternStoreData = array("category" => "pattern");
         $file = file_get_contents(Config::getOption("patternSourceDir") . "/" . $pathName);
         if ($ext == "json") {
             $data = json_decode($file, true);
             if ($jsonErrorMessage = JSON::hasError()) {
                 JSON::lastErrorMsg($name, $jsonErrorMessage, $data);
             }
         } else {
             try {
                 $data = YAML::parse($file);
             } catch (ParseException $e) {
                 printf("unable to parse " . $pathNameClean . ": %s..\n", $e->getMessage());
             }
             // single line of text won't throw a YAML error. returns as string
             if (gettype($data) == "string") {
                 $data = array();
             }
         }
         $patternStoreData["data"] = $data;
         // create a key for the data store
         $patternStoreKey = $patternPartial;
         // 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;
         PatternData::setOption($patternStoreKey, $patternStoreData);
     }
 }
 protected function starterKitSuggestions()
 {
     Console::writeLine("");
     $composerPath = Config::getOption("baseDir") . "/composer.json";
     if (file_exists($composerPath)) {
         $json = file_get_contents($composerPath);
         $data = json_decode($json, true);
         if ($jsonErrorMessage = JSON::hasError()) {
             JSON::lastErrorMsg(Console::getHumanReadablePath($oldStyleAnnotationsPath), $jsonErrorMessage, $data);
         }
         if (isset($data["extra"]) && isset($data["extra"]["patternlab"]) && isset($data["extra"]["patternlab"]["starterKitSuggestions"])) {
             $starterKitSuggestions = $data["extra"]["patternlab"]["starterKitSuggestions"];
             Console::writeInfo("suggested starterkits that work with this edition:", false, true);
             foreach ($starterKitSuggestions as $i => $suggestion) {
                 $num = $i + 1;
                 Console::writeLine($num . ": " . $suggestion, true);
             }
             // hack around installer util feature in Console::promptInput
             InstallerUtil::$isInteractive = true;
             // prompt for input on the suggestions
             Console::writeLine("");
             $prompt = "choose an option or hit return to cancel:";
             $options = "(ex. 1)";
             $input = Console::promptInput($prompt, $options, "1");
             $result = (int) $input - 1;
             if (isset($starterKitSuggestions[$result])) {
                 Console::writeLine("");
                 $f = new Fetch();
                 $result = $f->fetchStarterKit($starterKitSuggestions[$result]);
             }
         } else {
             Console::writeWarning("this edition has no starterkits to suggested...", false, true);
         }
     } else {
         Console::writeError("can't find composer.json to get suggestions...", false, true);
     }
 }
Exemplo n.º 5
0
 /**
  * Generate the listItems array
  * @param  {String}       the filename for the pattern to be parsed
  * @param  {String}       the extension so that a flag switch can be used to parse data
  *
  * @return {Array}        the final set of list items
  */
 public static function getListItems($filepath, $ext = "json")
 {
     // set-up the dispatcher
     $dispatcherInstance = Dispatcher::getInstance();
     // dispatch that the data gather has started
     $dispatcherInstance->dispatch("data.getListItemsStart");
     // default vars
     $sourceDir = Config::getOption("sourceDir");
     $listItems = array();
     $listItemsData = array();
     // add list item data, makes 'listItems' a reserved word
     if (file_exists($sourceDir . "/" . $filepath)) {
         $file = file_get_contents($sourceDir . "/" . $filepath);
         if ($ext == "json") {
             $listItemsData = json_decode($file, true);
             if ($jsonErrorMessage = JSON::hasError()) {
                 JSON::lastErrorMsg($filepath, $jsonErrorMessage, $listItems);
             }
         } else {
             try {
                 $listItemsData = YAML::parse($file);
             } catch (ParseException $e) {
                 printf("unable to parse " . $pathNameClean . ": %s..\n", $e->getMessage());
             }
             // single line of text won't throw a YAML error. returns as string
             if (gettype($listItemsData) == "string") {
                 $listItemsData = array();
             }
         }
         $numbers = array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve");
         $i = 0;
         $k = 1;
         $c = count($listItemsData) + 1;
         while ($k < $c) {
             shuffle($listItemsData);
             $itemsArray = array();
             while ($i < $k) {
                 $itemsArray[] = $listItemsData[$i];
                 $i++;
             }
             $listItems[$numbers[$k - 1]] = $itemsArray;
             $i = 0;
             $k++;
         }
     }
     $dispatcherInstance->dispatch("data.getListItemsEnd");
     return $listItems;
 }
 public function run($depth, $ext, $path, $pathName, $name)
 {
     // load default vars
     $patternSubtype = PatternData::getPatternSubtype();
     $patternSubtypeClean = PatternData::getPatternSubtypeClean();
     $patternSubtypeDash = PatternData::getPatternSubtypeDash();
     $patternType = PatternData::getPatternType();
     $patternTypeClean = PatternData::getPatternTypeClean();
     $patternTypeDash = PatternData::getPatternTypeDash();
     $dirSep = PatternData::getDirSep();
     $frontMeta = PatternData::getFrontMeta();
     // should this pattern get rendered?
     $hidden = $name[0] == "_";
     $noviewall = $name[0] == "-";
     // set-up the names
     $patternFull = in_array($name[0], $frontMeta) ? substr($name, 1) : $name;
     // 00-colors~foo.mustache
     $patternState = "";
     // check for pattern state
     if (strpos($patternFull, "@") !== false) {
         $patternBits = explode("@", $patternFull, 2);
         $patternState = str_replace("." . $ext, "", $patternBits[1]);
         $patternFull = preg_replace("/@(.*?)\\./", ".", $patternFull);
     }
     // finish setting up vars
     $patternBits = explode("~", $patternFull);
     $patternBase = $patternBits[0] . "." . Config::getOption("patternExtension");
     // 00-homepage.mustache
     $patternBaseDash = $this->getPatternName($patternBits[0], false);
     // homepage
     $patternBaseOrig = $patternTypeDash . "-" . $patternBaseDash;
     // pages-homepage
     $patternBaseData = $patternBits[0] . "." . $ext;
     // 00-homepage.json
     $stripJSON = str_replace("." . $ext, "", $patternBits[1]);
     $patternBitClean = preg_replace("/@(.*?)/", "", $patternBits[0]);
     $pattern = $patternBitClean . "-" . $stripJSON;
     // 00-homepage-00-emergency
     $patternInt = $patternBitClean . "-" . $this->getPatternName($stripJSON, false);
     // 00-homepage-emergency
     $patternDash = $this->getPatternName($patternInt, false);
     // homepage-emergency
     $patternClean = str_replace("-", " ", $patternDash);
     // homepage emergency
     $patternPartial = $patternTypeDash . "-" . $patternDash;
     // pages-homepage-emergency
     $patternPath = str_replace("." . $ext, "", str_replace("~", "-", $pathName));
     // 00-atoms/01-global/00-colors
     $patternPathDash = str_replace($dirSep, "-", $patternPath);
     // 00-atoms-01-global-00-colors (file path)
     // check the original pattern path. if it doesn't exist make a guess
     $patternPathOrig = PatternData::getPatternOption($patternBaseOrig, "pathName");
     // 04-pages/00-homepage
     $patternPathOrigDash = PatternData::getPatternOption($patternBaseOrig, "pathDash");
     // 04-pages-00-homepage
     if (!$patternPathOrig) {
         $patternPathOrigBits = explode("~", $pathName);
         $patternPathOrig = $patternPathOrigBits[0];
         // 04-pages/00-homepage
         $patternPathOrigDash = str_replace($dirSep, "-", $patternPathOrig);
         // 04-pages-00-homepage
     }
     // create a key for the data store
     $patternStoreKey = $patternPartial;
     // collect the data
     $patternStoreData = array("category" => "pattern", "name" => $pattern, "partial" => $patternPartial, "nameDash" => $patternDash, "nameClean" => $patternClean, "type" => $patternType, "typeDash" => $patternTypeDash, "breadcrumb" => array("patternType" => $patternTypeClean), "state" => $patternState, "hidden" => $hidden, "noviewall" => $noviewall, "depth" => $depth, "ext" => $ext, "path" => $path, "pathName" => $patternPath, "pathDash" => $patternPathDash, "isDir" => $this->isDirProp, "isFile" => $this->isFileProp, "pseudo" => true, "original" => $patternBaseOrig, "pathOrig" => $patternPathOrig, "pathOrigDash" => $patternPathOrigDash);
     // add any subtype info if necessary
     if ($depth > 1) {
         $patternStoreData["subtype"] = $patternSubtype;
         $patternStoreData["subtypeDash"] = $patternSubtypeDash;
         $patternStoreData["breadcrumb"] = array("patternType" => $patternTypeClean, "patternSubtype" => $patternSubtypeClean);
     }
     $patternDataBase = array();
     if (file_exists(Config::getOption("patternSourceDir") . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $patternBaseData)) {
         $data = file_get_contents(Config::getOption("patternSourceDir") . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $patternBaseData);
         if ($ext == "json") {
             $patternDataBase = json_decode($data, true);
             if ($jsonErrorMessage = JSON::hasError()) {
                 JSON::lastErrorMsg($patternBaseJSON, $jsonErrorMessage, $data);
             }
         } else {
             try {
                 $patternDataBase = YAML::parse($data);
             } catch (ParseException $e) {
                 printf("unable to parse " . $pathNameClean . ": %s..\n", $e->getMessage());
             }
             // single line of text won't throw a YAML error. returns as string
             if (gettype($patternDataBase) == "string") {
                 $patternDataBase = array();
             }
         }
     }
     // get the data for the pseudo-pattern
     $data = file_get_contents(Config::getOption("patternSourceDir") . DIRECTORY_SEPARATOR . $pathName);
     if ($ext == "json") {
         $patternData = json_decode($data, true);
         if ($jsonErrorMessage = JSON::hasError()) {
             JSON::lastErrorMsg($name, $jsonErrorMessage, $data);
         }
     } else {
         try {
             $patternData = YAML::parse($data);
         } catch (ParseException $e) {
             printf("unable to parse " . $pathNameClean . ": %s..\n", $e->getMessage());
         }
         // single line of text won't throw a YAML error. returns as string
         if (gettype($patternData) == "string") {
             $patternData = array();
         }
     }
     // make sure the pattern data is an array before merging the data
     $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;
     PatternData::setOption($patternStoreKey, $patternStoreData);
 }