-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (91 loc) · 3.26 KB
/
Copy pathindex.js
File metadata and controls
111 lines (91 loc) · 3.26 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
var fs = require('fs')
var postcss = require('postcss')
module.exports.parse = function parse (css) {
var root = postcss.parse(css)
var res = []
root.walkComments(function (node, i) {
if (node.type === 'comment') {
var text = node.text
var tmp = {}
var names = text.match(/\@.+?(\n|$|\s.+?(\n|$))/gm)
if (names) {
names.forEach(function (name, i) {
// Parsing annotation with value
// e.g. @foo bar
if (name.match(/\@(.+?)\s(.+?)(\n|$)/g)) {
var key = RegExp.$1
var val = RegExp.$2
// Parsing multiple annotations
// e.g. @array foo, bar, baz
if (val.match(/\,/)) {
val = val.split(',')
val.forEach(function (v, i) {
val[i] = v.trim()
})
}
// Extend the existing value
if (tmp[key] !== undefined && tmp[key] !== true) {
// Turn value in array
if (!Array.isArray(tmp[key])) {
tmp[key] = [tmp[key]]
}
tmp[key] = tmp[key].concat(val)
}
// Set the initial value
else {
tmp[key] = val
}
}
// Parsing annotation without value
// e.g. @foo
if (name.match(/\@(\w+)\s*(\n|$)/g)) {
var key = RegExp.$1
tmp[key] = tmp[key] || true
}
})
}
var parent = node.parent
if (parent.type ==='atrule') {
tmp.atrule = parent.name
tmp.params = parent.params
}
if (parent.type === 'rule') {
tmp.rule = parent.selector
}
res.push(tmp)
}
})
return res
}
module.exports.read = function read (commentText) {
var tmp = {}
var names = commentText.match(/\@.+?(\n|$|\s.+?(\n|$))/gm)
if (names) {
names.forEach(function (name, i) {
if (name.match(/\@(.+?)\s(.+?)(\n|$)/g)) {
var key = RegExp.$1
var val = RegExp.$2
if (val.match(/\,/)) {
val = val.split(',')
val.forEach(function (v, i) {
val[i] = v.trim()
})
}
if (tmp[key] !== undefined && tmp[key] !== true) {
if (!Array.isArray(tmp[key])) {
tmp[key] = [tmp[key]]
}
tmp[key] = tmp[key].concat(val)
}
else {
tmp[key] = val
}
}
if (name.match(/\@(\w+)\s*(\n|$)/g)) {
var key = RegExp.$1
tmp[key] = tmp[key] || true
}
})
}
return tmp
}