forked from cambridge-healthcare/grunt-stencil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.js
49 lines (41 loc) · 998 Bytes
/
parse.js
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
"use strict";
/*
* A set of functions to parse file sources
* into meta data headers and actual content
*/
module.exports = setup;
function setup (separator) {
return {
header: with_chunks(header),
content: with_chunks(content)
};
function header (chunks) {
return chunks.header ? JSON.parse(chunks.header) : {};
}
function content (chunks) {
return chunks.content;
}
function with_chunks (fn) {
return function (text) {
return fn(split(text));
};
}
function split (text) {
if (has_meta_data(text)) {
var match = text.match(separator);
return (match
? {
header: text.substring(0, match.index),
content: text.substring(match.index + match[0].length)
} : {
header: text,
content: ""
});
} else {
return { content: text };
}
}
function has_meta_data (chunk) {
return /^\{(?!\{)/.test(chunk);
}
}