-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathAnnotations.php
More file actions
122 lines (93 loc) · 3.23 KB
/
Annotations.php
File metadata and controls
122 lines (93 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*!
* Annotations Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Acts as the store for annotations. Parse annotations.js and *.md files found in source/_annotations
*
*/
namespace PatternLab;
use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\Dispatcher;
use \PatternLab\Parsers\Documentation;
use \PatternLab\Timer;
use \Symfony\Component\Finder\Finder;
use \Symfony\Component\Yaml\Exception\ParseException;
use \Symfony\Component\Yaml\Yaml;
class Annotations {
protected static $store = array();
/**
* Clear all of the data in the $store
*/
public static function clear() {
self::$store = array();
}
/**
* 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");
}
/**
* Get the data in the store
*/
public static function get() {
return self::$store;
}
}