-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathresult.rb
444 lines (390 loc) · 10.2 KB
/
result.rb
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
def strip_or_self(str)
str.strip! || str if str
end
$today = Time.now
$calendar = nil
$desc = nil
$location = "\"\""
$hour = 9
$minute = 0
$day = $today.day
$month = $today.month
$year = $today.year
$all_day = "false"
$alarm_to_set = []
$has_on_defined = false
$recurrence_to_set = []
$freq = 0
$rep = 0
$duration = 0
$hour_to = -1
$minute_to = 0
$multiplier = {}
$multiplier["m"] = 1
$multiplier["min"] = 1
$multiplier["minute"] = 1
$multiplier["minutes"] = 1
$multiplier["h"] = 60
$multiplier["hour"] = 60
$multiplier["hours"] = 60
$multiplier["d"] = 1440
$multiplier["day"] = 1440
$multiplier["days"] = 1440
$multiplier["w"] = 10080
$multiplier["week"] = 10080
$multiplier["weeks"] = 10080
$multiplier["mo"] = 43200
$multiplier["mon"] = 43200
$multiplier["month"] = 43200
$multiplier["months"] = 43200
$multiplier["y"] = 525600
$multiplier["year"] = 525600
$multiplier["years"] = 525600
$weeks = {}
$weeks["sun"] = 1
$weeks["sunday"] = 1
$weeks["mon"] = 2
$weeks["monday"] = 2
$weeks["tuesday"] = 3
$weeks["tue"] = 3
$weeks["wednesday"] = 4
$weeks["wed"] = 4
$weeks["thursday"] = 5
$weeks["thu"] = 5
$weeks["friday"] = 6
$weeks["fri"] = 6
$weeks["saturday"] = 7
$weeks["sat"] = 7
$months = {}
$months["jan"] = 1
$months["feb"] = 2
$months["mar"] = 3
$months["apr"] = 4
$months["may"] = 5
$months["jun"] = 6
$months["jul"] = 7
$months["aug"] = 8
$months["sep"] = 9
$months["oct"] = 10
$months["nov"] = 11
$months["dec"] = 12
$months["january"] = 1
$months["february"] = 2
$months["march"] = 3
$months["april"] = 4
$months["may"] = 5
$months["june"] = 6
$months["july"] = 7
$months["august"] = 8
$months["september"] = 9
$months["october"] = 10
$months["november"] = 11
$months["december"] = 12
def parse_cal(query)
# Alarm
if (query.index(" alarm "))
list = query.split(" alarm ")
alarms = strip_or_self(list[1])
query = list[0]
return nil if alarms.nil?
alarm_list = alarms.split(" ")
if (alarm_list.size > 1)
counter = "."
alarm_list.each { |val|
if (counter != ".")
if (!$multiplier[val].nil?)
new_alarm = counter * $multiplier[val]
$alarm_to_set.push(new_alarm)
counter = "."
end
else
counter = val.to_i
end
}
end
end
# Calendar Name
if (query.index(" @"))
list = query.split(" @")
$calendar = strip_or_self(list[1])
query = list[0]
end
# Location
if (query.index(" in "))
list = query.split(" in ")
$location = strip_or_self(list[1])
query = list[0]
end
# Repeat
# Recurrence
if (query.index(" every "))
list = query.split(" every ")
if (list[1])
recurrencies = strip_or_self(list[1])
query = list[0]
recurrence_list = recurrencies.split(" ")
if (recurrence_list.size > 0)
counter = "."
recurrence_list.each { |val|
value = strip_or_self(val)
value = value.downcase
$recurrence_to_set.push(value)
}
end
end
end
# Duration
if (query.index(" dur "))
list = query.split(" dur ")
values = strip_or_self(list[1])
return nil if values.nil?
params = values.split(" ")
if (params.size > 0)
value = params[0].to_i
unit = params[1]
if (!$multiplier[unit].nil?)
value = value * $multiplier[unit]
if (value > 0)
$duration = value
end
end
end
query = list[0]
end
# Date
# Check if "on" exists
if (query.index(" on "))
$has_on_defined = true
list = query.split(" on ")
return nil if list[1].nil?
# If month is defined
if (list[1].index("\/"))
list_d = list[1].split("\/")
$day = list_d[0]
$month = list_d[1]
if (list_d.size > 2)
$year = list_d[2]
if ($year.size < 4)
cur_year = $today.year.to_s
$year = (cur_year[0].chr + cur_year[1].chr + $year.to_s).to_i
end
end
else
c_day = strip_or_self(list[1])
c_day = c_day.downcase
if (!$weeks[c_day].nil?)
c_week = $today.wday
c_add = ($weeks[c_day] - 1) - c_week
if (c_add < 0)
c_add = 7 + c_add
end
new_day = $today + (c_add * 86400)
$day = new_day.day
$month = new_day.month
$year = new_day.year
else
$day = c_day
end
end
query = list[0]
elsif (query.index(/ [tT]oday/))
$has_on_defined = true
list = query.split(/ [tT]oday/)
query = list[0]
elsif (query.index(" tomorrow"))
$has_on_defined = true
list = query.split(" tomorrow")
tomorrow = $today + 86400
$day = tomorrow.day
$month = tomorrow.month
$year = tomorrow.year
query = list[0]
end
# #Time - to
# if (query.index(" to "))
# list = query.split(" to ")
# query = strip_or_self(list[0])
#
# $hour_to = list[1]
# $minute_to = 0
#
# if ($hour_to.index(":"))
# h_list = $hour_to.split(":")
# $hour_to = h_list[0]
# $minute_to = h_list[1]
#
# if ($minute_to.downcase.index("pm"))
# min_list = $minute_to.split("pm")
# $minute_to = strip_or_self(min_list[0])
# if ($hour_to.to_i < 13)
# $hour_to = ($hour_to.to_i + 12).to_s
# end
# end
#
# if ($minute_to.downcase.index("am"))
# min_list = $minute_to.split("am")
# $minute_to = strip_or_self(min_list[0])
# end
# else
# if ($hour_to.downcase.index("pm"))
# min_list = $hour_to.split("pm")
# $hour_to = strip_or_self(min_list[0])
# if ($hour_to.to_i < 13)
# $hour_to = ($hour_to.to_i + 12).to_s
# end
# end
#
# if ($hour_to.downcase.index("am"))
# min_list = $hour_to.split("am")
# $hour_to = strip_or_self(min_list[0])
# end
# end
# end
# Time - at
if (query.index(" at "))
list = query.split(" at ")
$desc = strip_or_self(list[0])
$hour = list[1]
return nil if $hour.nil?
$minute = 0
if ($hour.index(":"))
h_list = $hour.split(":")
$hour = h_list[0]
$minute = h_list[1]
if ($minute.downcase.index("pm"))
min_list = $minute.split("pm")
$minute = strip_or_self(min_list[0])
if ($hour.to_i < 13)
$hour = ($hour.to_i + 12).to_s
end
end
if ($minute.downcase.index("am"))
min_list = $minute.split("am")
$minute = strip_or_self(min_list[0])
end
else
if ($hour.downcase.index("pm"))
min_list = $hour.split("pm")
$hour = strip_or_self(min_list[0])
if ($hour.to_i < 13)
$hour = ($hour.to_i + 12).to_s
end
end
if ($hour.downcase.index("am"))
min_list = $hour.split("am")
$hour = strip_or_self(min_list[0])
end
end
else
$desc = query
$all_day = "true"
end
if ($recurrence_to_set.size > 0)
# se tem dia
if ($recurrence_to_set.include?("day"))
$freq = "DAILY;"
elsif ($recurrence_to_set.include?("week"))
$freq = "WEEKLY;"
elsif ($recurrence_to_set.include?("month"))
$freq = "MONTHLY;"
elsif ($recurrence_to_set.include?("year"))
$freq = "YEARLY;"
else
# procura por um numero (representa um dia)
day_list = []
is_first = true
$recurrence_to_set.each { |val|
if (val.to_i > 0)
if (is_first and not $has_on_defined)
$day = val
is_first = false
end
day_list.push(val)
end
}
if (day_list.size > 0)
$freq = "MONTHLY;"
$rep = "BYMONTHDAY=" + day_list.join(",")
else
week_list = []
is_first = true
$recurrence_to_set.each { |val|
# procura por semanas
if ($weeks[val])
if (is_first and not $has_on_defined)
is_first = false
c_week = $today.wday
c_add = ($weeks[val] - 1) - c_week
if (c_add < 0)
c_add = 7 + c_add
end
new_day = $today + (c_add * 86400)
$day = new_day.day
$month = new_day.month
$year = new_day.year
end
value = val[0..1].upcase
week_list.push(value)
end
}
if (week_list.size > 0)
$freq = "WEEKLY;"
$rep = "BYDAY=" + week_list.join(",")
else
month_list = []
$recurrence_to_set.each { |val|
# procura por meses
if ($months[val])
month_list.push($months[val])
end
}
if (month_list.size > 0)
$freq = "MONTHLY;"
$rep = "BYMONTH=" + month_list.join(",")
end
end
end
end
end
end
query = ARGV[0]
if (query[/^default/])
item = "<item uid=\"addcalendarevent\" arg=\"#{query}\" ><title>Change Default Calendar Name</title><subtitle>Click to change</subtitle><icon>icon.png</icon></item>"
puts "<?xml version=\"1.0\"?><items>#{item}</items>"
else
parse_cal(query)
subtitle = "Date: #{$day}/#{$month}/#{$year} - #{$hour}:" + ("%02d" % $minute)
if ($duration > 0)
fromTime = Time.new($year, $month, $day, $hour, $minute, 0)
toTime = fromTime + ($duration * 60)
subtitle = subtitle + " to #{toTime.day}/#{toTime.month}/#{toTime.year} - #{toTime.hour}:" + ("%02d" % toTime.min)
end
if $location != ""
subtitle = subtitle + " Location:#{$location}"
end
if $freq != 0
freq_str = ""
if ($freq == "DAILY;")
freq_str = "daily"
elsif ($freq == "WEEKLY;")
freq_str = "weekly"
elsif ($freq == "MONTHLY;")
freq_str = "monthly"
elsif ($freq == "YEARLY;")
freq_str = "yearly"
elsif ($freq == "DAILY;")
freq_str = "daily"
end
subtitle = subtitle + " - Repeat: " + freq_str
end
item = "<item uid=\"addcalendarevent\" arg=\"#{query}\" ><title>#{$desc}</title><subtitle>#{subtitle}</subtitle><icon>icon.png</icon></item>"
if ($alarm_to_set.size == 0)
$alarm_to_set.push("at event time")
end
count = 1
$alarm_to_set.each { |val|
item = item + "<item uid=\"alarm#{val}\" arg=\"\" valid=\"no\" ><title>Alarm #{count}</title><subtitle>#{val}</subtitle><icon>alarm.png</icon></item>"
count = count + 1
}
puts "<?xml version=\"1.0\"?><items>#{item}</items>"
end