-
Notifications
You must be signed in to change notification settings - Fork 2
/
TaskPaperToThings.js
297 lines (266 loc) · 7.18 KB
/
TaskPaperToThings.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: check-double;
// These constants are used to determine if a field is a tag (e.g. @home) or an
// attribute (e.g. @due(2019-02-08)).
const FIELD_TYPE_TAG = 'tag';
const FIELD_TYPE_ATTR = 'attr';
// The output expected by the testcase.
const EXPECTED_OUTPUT = `[{"type":"project","attributes":{"when":"2019-02-08","tags":["Home"],"title":"Test project ","notes":"A note to the project","items":[{"type":"to-do","attributes":{"tags":[],"title":"A task","notes":"","checklist-items":[]}},{"type":"to-do","attributes":{"tags":["sometag"],"title":"Another task ","notes":"A note to another task\\nAnother line of notes for another task","checklist-items":[{"type":"checklist-item","attributes":{"tags":[],"title":"A subtask","notes":""}}]}}]}}]`;
// IIFE to capsule "main" function.
(function() {
const isRunningOnScriptable = typeof Pasteboard !== 'undefined';
// Get input, if we're in Scriptable from Pasteboard, otherwise test data.
let input = '';
if (isRunningOnScriptable) {
input = Pasteboard.paste();
} else {
input = `
- Test project @Home @today
A note to the project
- A task
- Another task @sometag
A note to another task
Another line of notes for another task
- A subtask
`;
}
// The Things JSON representation of the TaskPaper input.
let output = [];
// Parameters used by the following loop.
let parentProject = null;
let parentTask = null;
let lastItem = null; // Can be task, project or checklist item.
// Process each line.
input.split('\n').forEach(line => {
// Do nothing on empty line.
if (line.trim() === '') {
return;
}
// If the first char isn't `-`, we have a note.
if (line.trim().charAt(0) !== '-') {
if (lastItem === null) {
throw 'The first line cannot be a note';
}
// Add a linebreak if we already have notes on this item.
// This can happen for multi-line notes.
if (lastItem.attributes.notes !== '') {
lastItem.attributes.notes += '\n';
}
lastItem.attributes.notes += line.trim();
return;
}
// Check how many tabs we have.
// TODO: Also allow spaces as indention.
let tabs = 0;
for (let j = 0; j < line.length; j++) {
if (line[j] !== '\t') {
// We have a char which is not a tab.
break;
}
tabs++;
}
// Process line depending on indentation.
switch (tabs) {
case 0: // Project
if (parentProject !== null) {
// Add last project to output.
console.log(parentProject)
output.push(parentProject);
}
let project = parseProject(line);
parentProject = project;
parentTask = null;
lastItem = project;
break;
case 1: // Task
let task = parseTask(line);
parentTask = task;
parentProject.attributes.items.push(task);
lastItem = task;
break;
case 2: // Checklist item
let item = parseChecklistItem(line);
parentTask.attributes['checklist-items'].push(item);
lastItem = item;
break;
}
});
// Push last project.
output.push(parentProject);
// Create JSON string.
const json = JSON.stringify(output);
// Copy to clipboard (if in Scriptable), otherwise test.
if (isRunningOnScriptable) {
Pasteboard.copy(json);
} else {
// Test output.
if (json !== EXPECTED_OUTPUT) {
throw `Output differs!
expected: ${EXPECTED_OUTPUT}
actual: ${json}
`;
} else {
console.log('Looks good.');
}
}
})();
/**
* Parse a project line.
*
* @param {string} line
* @returns {object} Things representation of the project.
*/
function parseProject(line) {
const { title, attributes } = parseLine(line)
return {
type: 'project',
attributes: {
...attributes,
title,
notes: '',
items: [],
}
}
}
/**
* Parse a task line.
*
* @param {string} line
* @returns {object} Things representation of the task.
*/
function parseTask(line) {
const { title, attributes } = parseLine(line)
return {
type: 'to-do',
attributes: {
...attributes,
title,
notes: '',
'checklist-items': [],
}
}
}
/**
* Parse a checklist-item line.
*
* @param {string} line
* @returns {object} Things representation of the checklist item.
*/
function parseChecklistItem(line) {
const { title, attributes } = parseLine(line)
return {
type: 'checklist-item',
attributes: {
...attributes,
title,
notes: '',
}
}
}
/**
* Helper function used by parse{Project,Task,ChecklistItem} to get title and fields.
*
* @param {string} rawLine The raw, untrimmed line
* @return {object} An object containing the keys `title` and `attribute`
*/
function parseLine(rawLine) {
let line = rawLine.trim().substring(2);
// Get title
let matches = line.match(/^[^@]+/)
if (matches === null) {
throw 'Could not match title'
}
const title = matches[0];
// Get attributes
let tags = [];
let attributes = {};
matches = line.match(/(@[^\s]+)/g)
if (matches !== null) {
for (let i = 0; i < matches.length; i++) {
let { type, key, value } = parseField(matches[i])
if (type === FIELD_TYPE_TAG) {
tags.push(key);
} else if (type === FIELD_TYPE_ATTR) {
attributes[key] = value;
}
}
}
return {
title,
attributes: {
...attributes,
tags
}
}
}
/**
* Parse a field (e.g. @due(2019-02-08).
*
* There are two kinds of fields:
* 1. Tags (e.g. @home)
* 2. Attributes (e.g. @due(2019-02-08))
* The type is one of FIELD_TYPE_ATTR, FIELD_TYPE_TAG
*
* @param {string} raw
* @returns {object} An object with the keys `type`, `key` and `value`
*/
function parseField(raw) {
// This will be returned by this function
let res = {
type: FIELD_TYPE_TAG, // default, can also be FIELD_TYPE_ATTR
key: null,
value: null,
}
// Get key (required)
let matches = raw.match(/@([^\s(]+)/)
if (matches === null) {
throw 'Invalid tag: ' + raw
}
res.key = matches[1]
// Get value if set
let value = null;
matches = raw.match(/@[^\s(]+\(([^\)]+)\)/)
if (matches !== null) {
res.value = matches[1]
res.type = FIELD_TYPE_ATTR
}
// Check for basic special fields
if (res.type === FIELD_TYPE_TAG) {
switch (res.key) {
case 'done':
res.type = FIELD_TYPE_ATTR
res.key = 'completed'
res.value = true
break;
case 'today':
res.type = FIELD_TYPE_ATTR
res.key = 'when'
res.value = formatDate(new Date())
break;
case 'start':
res.type = FIELD_TYPE_ATTR
res.key = 'when'
break;
case 'due':
res.type = FIELD_TYPE_ATTR
res.key = 'deadline'
break;
}
}
return res
}
/**
* Format a date to YYYY-MM-DD
* @param {Date} Date to format
* @returns {string} Formatted date
*/
function formatDate(date) {
const pad = num => {
if (num < 10) {
return `0${num}`
}
return num
}
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
}