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
298
299
300
301
302
303
304
305
306
307
308
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const EXPORTED_SYMBOLS = ["CalExtractParserService"];
const { CalExtractParser } = ChromeUtils.import(
"resource:///modules/calendar/extract/CalExtractParser.jsm"
);
const defaultRules = [
[
// Start clean up patterns.
// remove last line preceding quoted message and first line of the quote
[/^(\r?\n[^>].*\r?\n>+.*$)/, ""],
// remove the rest of quoted content
[/^(>+.*$)/, ""],
// urls often contain dates dates that can confuse extraction
[/^(https?:\/\/[^\s]+\s)/, ""],
[/^(www\.[^\s]+\s)/, ""],
// remove phone numbers
[/^(\d-\d\d\d-\d\d\d-\d\d\d\d)/, ""],
// remove standard signature
[/^(\r?\n-- \r?\n[\S\s]+$)/, ""],
// XXX remove timezone info, for now
[/^(gmt[+-]\d{2}:\d{2})/i, ""],
// End clean up patterns.
[/^meet\b/i, "MEET"],
[/^(we will|we'll|we)\b/i, "WE"],
// Meridiem
[/^(a[.]?m[.]?)/i, "AM"],
[/^(p[.]?m[.]?)/i, "PM"],
[/^(hours|hour|hrs|hr)\b/i, "HOURS"],
[/^(minutes|min|mins)\b/i, "MINUTES"],
[/^(days|day)\b/i, "DAYS"],
// Words commonly used when specifying begin/end time or duration.
[/^at\b/i, "AT"],
[/^until\b/i, "UNTIL"],
[/^for\b/i, "FOR"],
// Units of time
[/^(((0|1)?[0-9])|(2[0-4]))\b/, "HOUR_VALUE"],
[/^\d+\b/, "NUMBER"],
// Any text we don't know the meaning of.
[/^\S+/, "TEXT"],
// Whitespace
[/^\s+/, ""],
],
[
{
name: "event-guess",
patterns: ["subject", "meet", "start-time", "text*", "end-time"],
action: ([, , startTime, , endTime]) => ({
type: "event-guess",
startTime,
endTime,
priority: 0,
}),
},
{
name: "event-guess",
patterns: ["subject", "meet", "start-time", "text*", "duration-time"],
action: ([, , startTime, , endTime]) => ({
type: "event-guess",
startTime,
endTime,
priority: 0,
}),
},
{
name: "subject",
patterns: ["WE"],
action: ([subject]) => ({
type: "subject",
subject: subject.text,
}),
},
{
name: "start-time",
patterns: ["start-time-prefix", "meridiem-time"],
action: ([, time]) => time,
},
{
name: "start-time-prefix",
patterns: ["AT"],
action: ([prefix]) => prefix.text,
},
{
name: "end-time",
patterns: ["end-time-prefix", "meridiem-time"],
action: ([, time]) => time,
},
{
name: "end-time-prefix",
patterns: ["UNTIL"],
action: ([prefix]) => prefix.text,
},
{
name: "meridiem-time",
patterns: ["HOUR_VALUE", "meridiem"],
action: ([hour, meridiem]) => ({
type: "meridiem-time",
hour: Number(hour.text),
meridiem,
}),
},
{
name: "meridiem",
patterns: ["AM"],
action: () => "am",
},
{
name: "meridiem",
patterns: ["PM"],
action: () => "pm",
},
{
name: "duration-time",
patterns: ["duration-prefix", "duration"],
action: ([, duration]) => ({
type: "duration-time",
duration,
}),
},
{
name: "duration-prefix",
patterns: ["FOR"],
action: ([prefix]) => prefix.text,
},
{
name: "duration",
patterns: ["NUMBER", "MINUTES"],
action: ([value]) => Number(value.text),
},
{
name: "duration",
patterns: ["NUMBER", "HOURS"],
action: ([value]) => Number(value.text) * 60,
},
{
name: "duration",
patterns: ["NUMBER", "DAYS"],
action: ([value]) => Number(value.text) * 60 * 24,
},
{
name: "meet",
patterns: ["MEET"],
action: () => "meet",
},
{
name: "text",
patterns: ["TEXT"],
action: ([text]) => text,
},
],
];
/**
* CalExtractParserServiceContext represents the context parsing and extraction
* takes place in. It holds values used in various calculations. For example,
* the current date.
*
* @typedef {object} CalExtractParserServiceContext
* @property {Date} now - The Date to use when calculating start and relative
* times.
*/
/**
* CalExtractParserService provides a frontend to the CalExtractService.
* It holds lexical and parse rules for multiple locales (or any string
* identifier) that can be used on demand when parsing text.
*/
class CalExtractParserService {
rules = new Map([["en-US", defaultRules]]);
/**
* Parses and extract the most relevant event creation data based on the
* rules of the locale given.
*
* @param {string} source
* @param {CalExtractParserServiceContext} context
* @param {string} locale
*/
extract(source, ctx = { now: new Date() }, locale = "en-US") {
let rules = this.rules.get(locale);
if (!rules) {
return null;
}
let [lex, parse] = rules;
let parser = CalExtractParser.createInstance(lex, parse);
let result = parser.parse(source).sort((a, b) => a - b)[0];
return result && convertDurationToEndTime(populateTimes(result, ctx.now));
}
}
/**
* Populates the missing values of the startTime and endTime.
*
* @param {object?} guess - The result of CalExtractParserService.extract().
* @param {Date} now - A Date object representing the contextual date and
* time.
*
* @returns {object} The result with the populated startTime and endTime.
*/
function populateTimes(guess, now) {
return populateTime(populateTime(guess, now, "startTime"), now, "endTime");
}
/**
* Populates the missing values of the specified time property based on the Date
* provided.
*
* @param {object?} guess
* @param {Date} now
* @param {string} prop
*
* @returns {object}
*/
function populateTime(guess, now, prop) {
let time = guess[prop];
if (!time) {
return guess;
}
if (time.hour && time.meridiem) {
time.hour = normalizeHour(time.hour, time.meridiem);
}
time.year = time.year || now.getFullYear();
time.month = time.month || now.getMonth() + 1;
time.day = time.day || now.getDay();
time.hour = time.hour || now.getHours();
time.minute = time.minute || now.getMinutes();
return guess;
}
/**
* Coverts an hour using the Meridiem to a 24 hour value.
*
* @param {number} hour - The hour value.
* @param {string} meridiem - "am" or "pm"
*
* @returns {number}
*/
function normalizeHour(hour, meridiem) {
if (meridiem == "am" && hour == 12) {
return hour - 12;
} else if (meridiem == "pm" && hour != 12) {
return hour + 12;
}
let dayStart = Services.prefs.getIntPref("calendar.view.daystarthour", 6);
if (hour < dayStart && hour <= 11) {
return hour + 12;
}
return hour;
}
/**
* Takes care of converting an end duration to an actual time relative to the
* start time detected (if any).
*
* @param {object} guess - Results from CalExtractParserService#extract()
*
* @returns {object} The result with the endTime property expanded.
*/
function convertDurationToEndTime(guess) {
if (guess.startTime && guess.endTime && guess.endTime.type == "duration-time") {
let startTime = guess.startTime;
let duration = guess.endTime.duration;
if (duration != 0) {
let startDate = new Date(startTime.year, startTime.month - 1, startTime.day);
if ("hour" in startTime) {
startDate.setHours(startTime.hour);
startDate.setMinutes(startTime.minute);
}
let endDate = new Date(startDate.getTime() + duration * 60 * 1000);
let endTime = { type: "date-time" };
endTime.year = endDate.getFullYear();
endTime.month = endDate.getMonth() + 1;
endTime.day = endDate.getDate();
if (endDate.getHours() != 0 || endDate.getMinutes() != 0) {
endTime.hour = endDate.getHours();
endTime.minute = endDate.getMinutes();
}
guess.endTime = endTime;
}
}
return guess;
}
|