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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
title: $:/plugins/local/org-gtd/library.js
type: application/javascript
module-type: library
/*\
title: $:/plugins/local/org-gtd/library.js
type: application/javascript
module-type: library
\*/
(function() {
"use strict";
var TODO_STATES = ["TODO","NEXT","WAIT","DONE"],
TODO_LOOKUP = Object.create(null),
HEADING_PATTERN = /^(\*+)\s+(?:(TODO|NEXT|WAIT|DONE)\s+)?(.*)$/,
TAG_PATTERN = /\s+:([A-Za-z0-9_@#%+.-]+(?::[A-Za-z0-9_@#%+.-]+)*):\s*$/,
DATE_PATTERN = /\b(SCHEDULED|DEADLINE):\s*<([0-9]{4}-[0-9]{2}-[0-9]{2})(?:\s+[^>]*)?>/g;
TODO_STATES.forEach(function(state) {
TODO_LOOKUP[state] = true;
});
function parseHeading(line,lineIndex) {
var match = HEADING_PATTERN.exec(line),
tagMatch,
title,
tags = [];
if(!match) {
return null;
}
title = match[3];
tagMatch = TAG_PATTERN.exec(title);
if(tagMatch) {
tags = tagMatch[1].split(":");
title = title.substring(0,tagMatch.index).trim();
}
return {
level: match[1].length,
state: match[2] || "",
title: title,
tags: tags,
line: lineIndex,
sourceLine: lineIndex + 1,
endLine: lineIndex,
scheduled: "",
deadline: ""
};
}
function parse(text) {
var lines = (text || "").split("\n"),
headings = [],
current = null;
lines.forEach(function(line,index) {
var heading = parseHeading(line,index),
match;
if(heading) {
if(current) {
current.endLine = index - 1;
}
headings.push(heading);
current = heading;
return;
}
if(current) {
DATE_PATTERN.lastIndex = 0;
while((match = DATE_PATTERN.exec(line)) !== null) {
if(match[1] === "SCHEDULED") {
current.scheduled = match[2];
} else {
current.deadline = match[2];
}
}
}
});
if(current) {
current.endLine = lines.length - 1;
}
for(var index = 0; index < headings.length; index++) {
var heading = headings[index],
nextIndex = index + 1;
while(nextIndex < headings.length && headings[nextIndex].level > heading.level) {
nextIndex++;
}
heading.subtreeEndLine = nextIndex < headings.length ? headings[nextIndex].line - 1 : lines.length - 1;
}
return {lines: lines,headings: headings};
}
function headingAtLine(parsed,line) {
var result = null;
parsed.headings.forEach(function(heading) {
if(heading.line <= line && heading.subtreeEndLine >= line) {
if(!result || heading.level >= result.level) {
result = heading;
}
}
});
return result;
}
function cycleState(line) {
var heading = parseHeading(line,0),
stars,
nextState,
rest;
if(!heading) {
return null;
}
stars = new Array(heading.level + 1).join("*");
if(heading.state) {
nextState = TODO_STATES[(TODO_STATES.indexOf(heading.state) + 1) % TODO_STATES.length];
rest = line.replace(/^(\*+)\s+(?:TODO|NEXT|WAIT|DONE)\s+/,"$1 " + nextState + " ");
} else {
rest = line.replace(/^(\*+)\s+/,"$1 TODO ");
}
return {line: rest,state: nextState || "TODO",level: stars.length};
}
function dateToTime(value) {
var match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value || ""),
date;
if(!match) {
return NaN;
}
date = new Date(Number(match[1]),Number(match[2]) - 1,Number(match[3]));
if(date.getFullYear() !== Number(match[1]) || date.getMonth() !== Number(match[2]) - 1 || date.getDate() !== Number(match[3])) {
return NaN;
}
return date.getTime();
}
exports.TODO_STATES = TODO_STATES;
exports.TODO_LOOKUP = TODO_LOOKUP;
exports.parseHeading = parseHeading;
exports.parse = parse;
exports.headingAtLine = headingAtLine;
exports.cycleState = cycleState;
exports.dateToTime = dateToTime;
})();
|