1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | define = require("amdefine")(module) if not define or typeof define isnt "function"
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | define [], ->
|
16 |
|
17 |
|
18 | isServer = (typeof process isnt "undefined")
|
19 | register = (Handlebars) ->
|
20 | Dates = {}
|
21 | HTML = {}
|
22 | Utils = {}
|
23 | __indexOf_ = [].indexOf or (item) ->
|
24 | i = 0
|
25 | l = @length
|
26 |
|
27 | while i < l
|
28 | return i if i of this and this[i] is item
|
29 | i++
|
30 | -1
|
31 |
|
32 |
|
33 |
|
34 | assemble = require("../../../assemble.js")
|
35 | fs = require("fs")
|
36 | util = require("util")
|
37 | path = require("path")
|
38 | dest = "./"
|
39 | markdown = assemble.Markdown(
|
40 | gfm: true
|
41 | highlight: "auto"
|
42 | )
|
43 |
|
44 |
|
45 |
|
46 | |
47 |
|
48 |
|
49 | Utils.toString = Object::toString
|
50 | Utils.isUndefined = (value) ->
|
51 | value is "undefined" or Utils.toString.call(value) is "[object Function]" or (value.hash?)
|
52 |
|
53 | Utils.safeString = (str) ->
|
54 | new Handlebars.SafeString(str)
|
55 |
|
56 | Utils.trim = (str) ->
|
57 | trim = undefined
|
58 | trim = (if /\S/.test(" ") then /^[\s\xA0]+|[\s\xA0]+$/g else /^\s+|\s+$/g)
|
59 | str.toString().replace trim, ""
|
60 |
|
61 |
|
62 |
|
63 | Utils.urlNormalize = (urlString) ->
|
64 | urlString.replace /\\/g, "/"
|
65 |
|
66 |
|
67 | ###
|
68 | HTML
|
69 | ###
|
70 | HTML.parseAttributes = (hash) ->
|
71 | Object.keys(hash).map((key) ->
|
72 | "" + key + "=\"" + hash[key] + "\""
|
73 | ).join " "
|
74 |
|
75 |
|
76 | |
77 |
|
78 |
|
79 | Dates.padNumber = (num, count, padCharacter) ->
|
80 | lenDiff = undefined
|
81 | padding = undefined
|
82 | padCharacter = "0" if typeof padCharacter is "undefined"
|
83 | lenDiff = count - String(num).length
|
84 | padding = ""
|
85 | padding += padCharacter while lenDiff-- if lenDiff > 0
|
86 | padding + num
|
87 |
|
88 | Dates.dayOfYear = (date) ->
|
89 | oneJan = undefined
|
90 | oneJan = new Date(date.getFullYear(), 0, 1)
|
91 | Math.ceil (date - oneJan) / 86400000
|
92 |
|
93 | Dates.weekOfYear = (date) ->
|
94 | oneJan = undefined
|
95 | oneJan = new Date(date.getFullYear(), 0, 1)
|
96 | Math.ceil (((date - oneJan) / 86400000) + oneJan.getDay() + 1) / 7
|
97 |
|
98 | Dates.isoWeekOfYear = (date) ->
|
99 | dayDiff = undefined
|
100 | dayNr = undefined
|
101 | jan4 = undefined
|
102 | target = undefined
|
103 | target = new Date(date.valueOf())
|
104 | dayNr = (date.getDay() + 6) % 7
|
105 | target.setDate target.getDate() - dayNr + 3
|
106 | jan4 = new Date(target.getFullYear(), 0, 4)
|
107 | dayDiff = (target - jan4) / 86400000
|
108 | 1 + Math.ceil(dayDiff / 7)
|
109 |
|
110 | Dates.tweleveHour = (date) ->
|
111 | if date.getHours() > 12
|
112 | date.getHours() - 12
|
113 | else
|
114 | date.getHours()
|
115 |
|
116 | Dates.timeZoneOffset = (date) ->
|
117 | hoursDiff = undefined
|
118 | result = undefined
|
119 | hoursDiff = -date.getTimezoneOffset() / 60
|
120 | result = Dates.padNumber(Math.abs(hoursDiff), 4)
|
121 | ((if hoursDiff > 0 then "+" else "-")) + result
|
122 |
|
123 | Dates.format = (date, format) ->
|
124 | format.replace Dates.formats, (m, p) ->
|
125 | switch p
|
126 | when "a"
|
127 | Dates.abbreviatedWeekdays[date.getDay()]
|
128 | when "A"
|
129 | Dates.fullWeekdays[date.getDay()]
|
130 | when "b"
|
131 | Dates.abbreviatedMonths[date.getMonth()]
|
132 | when "B"
|
133 | Dates.fullMonths[date.getMonth()]
|
134 | when "c"
|
135 | date.toLocaleString()
|
136 | when "C"
|
137 | Math.round date.getFullYear() / 100
|
138 | when "d"
|
139 | Dates.padNumber date.getDate(), 2
|
140 | when "D"
|
141 | Dates.format date, "%m/%d/%y"
|
142 | when "e"
|
143 | Dates.padNumber date.getDate(), 2, " "
|
144 | when "F"
|
145 | Dates.format date, "%Y-%m-%d"
|
146 | when "h"
|
147 | Dates.format date, "%b"
|
148 | when "H"
|
149 | Dates.padNumber date.getHours(), 2
|
150 | when "I"
|
151 | Dates.padNumber Dates.tweleveHour(date), 2
|
152 | when "j"
|
153 | Dates.padNumber Dates.dayOfYear(date), 3
|
154 | when "k"
|
155 | Dates.padNumber date.getHours(), 2, " "
|
156 | when "l"
|
157 | Dates.padNumber Dates.tweleveHour(date), 2, " "
|
158 | when "L"
|
159 | Dates.padNumber date.getMilliseconds(), 3
|
160 | when "m"
|
161 | Dates.padNumber date.getMonth() + 1, 2
|
162 | when "M"
|
163 | Dates.padNumber date.getMinutes(), 2
|
164 | when "n"
|
165 | "\n"
|
166 | when "p"
|
167 | if date.getHours() > 11
|
168 | return "PM"
|
169 | else
|
170 | return "AM"
|
171 | when "P"
|
172 | Dates.format(date, "%p").toLowerCase()
|
173 | when "r"
|
174 | Dates.format date, "%I:%M:%S %p"
|
175 | when "R"
|
176 | Dates.format date, "%H:%M"
|
177 | when "s"
|
178 | date.getTime() / 1000
|
179 | when "S"
|
180 | Dates.padNumber date.getSeconds(), 2
|
181 | when "t"
|
182 | "\t"
|
183 | when "T"
|
184 | Dates.format date, "%H:%M:%S"
|
185 | when "u"
|
186 | if date.getDay() is 0
|
187 | return 7
|
188 | else
|
189 | return date.getDay()
|
190 | when "U"
|
191 | Dates.padNumber Dates.weekOfYear(date), 2
|
192 | when "v"
|
193 | Dates.format date, "%e-%b-%Y"
|
194 | when "V"
|
195 | Dates.padNumber Dates.isoWeekOfYear(date), 2
|
196 | when "W"
|
197 | Dates.padNumber Dates.weekOfYear(date), 2
|
198 | when "w"
|
199 | Dates.padNumber date.getDay(), 2
|
200 | when "x"
|
201 | date.toLocaleDateString()
|
202 | when "X"
|
203 | date.toLocaleTimeString()
|
204 | when "y"
|
205 | String(date.getFullYear()).substring 2
|
206 | when "Y"
|
207 | date.getFullYear()
|
208 | when "z"
|
209 | Dates.timeZoneOffset date
|
210 | else
|
211 | match
|
212 |
|
213 |
|
214 | Dates.formats = /%(a|A|b|B|c|C|d|D|e|F|h|H|I|j|k|l|L|m|M|n|p|P|r|R|s|S|t|T|u|U|v|V|W|w|x|X|y|Y|z)/g
|
215 | Dates.abbreviatedWeekdays = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"]
|
216 | Dates.fullWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
217 | Dates.abbreviatedMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
|
218 | Dates.fullMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | |
229 |
|
230 |
|
231 |
|
232 | Handlebars.registerHelper "if_eq", (context, options) ->
|
233 | return options.fn(this) if context is options.hash.compare
|
234 | options.inverse this
|
235 |
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 | Handlebars.registerHelper "unless_eq", (context, options) ->
|
242 | return options.inverse(this) if context is options.hash.compare
|
243 | options.fn this
|
244 |
|
245 |
|
246 | |
247 |
|
248 |
|
249 |
|
250 | Handlebars.registerHelper "if_gt", (context, options) ->
|
251 | return options.fn(this) if context > options.hash.compare
|
252 | options.inverse this
|
253 |
|
254 |
|
255 | |
256 |
|
257 |
|
258 |
|
259 | Handlebars.registerHelper "unless_gt", (context, options) ->
|
260 | return options.inverse(this) if context > options.hash.compare
|
261 | options.fn this
|
262 |
|
263 |
|
264 | |
265 |
|
266 |
|
267 |
|
268 | Handlebars.registerHelper "if_lt", (context, options) ->
|
269 | return options.fn(this) if context < options.hash.compare
|
270 | options.inverse this
|
271 |
|
272 |
|
273 | |
274 |
|
275 |
|
276 |
|
277 | Handlebars.registerHelper "unless_lt", (context, options) ->
|
278 | return options.inverse(this) if context < options.hash.compare
|
279 | options.fn this
|
280 |
|
281 |
|
282 | |
283 |
|
284 |
|
285 |
|
286 | Handlebars.registerHelper "if_gteq", (context, options) ->
|
287 | return options.fn(this) if context >= options.hash.compare
|
288 | options.inverse this
|
289 |
|
290 |
|
291 | |
292 |
|
293 |
|
294 |
|
295 | Handlebars.registerHelper "unless_gteq", (context, options) ->
|
296 | return options.inverse(this) if context >= options.hash.compare
|
297 | options.fn this
|
298 |
|
299 |
|
300 | |
301 |
|
302 |
|
303 |
|
304 | Handlebars.registerHelper "if_lteq", (context, options) ->
|
305 | return options.fn(this) if context <= options.hash.compare
|
306 | options.inverse this
|
307 |
|
308 |
|
309 | |
310 |
|
311 |
|
312 |
|
313 | Handlebars.registerHelper "unless_lteq", (context, options) ->
|
314 | return options.inverse(this) if context <= options.hash.compare
|
315 | options.fn this
|
316 |
|
317 |
|
318 | |
319 |
|
320 |
|
321 |
|
322 | Handlebars.registerHelper "nl2br", (text) ->
|
323 | nl2br = (text + "").replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1" + "<br>" + "$2")
|
324 | new Handlebars.SafeString(nl2br)
|
325 |
|
326 |
|
327 | |
328 |
|
329 |
|
330 |
|
331 | Handlebars.registerHelper "hyphenate", (tag) ->
|
332 | tag.split(" ").join "-"
|
333 |
|
334 |
|
335 | |
336 |
|
337 |
|
338 |
|
339 | Handlebars.registerHelper "dashify", (tag) ->
|
340 | tag.split(".").join "-"
|
341 |
|
342 |
|
343 | |
344 |
|
345 |
|
346 |
|
347 | Handlebars.registerHelper "lowercase", (str) ->
|
348 | str.toLowerCase()
|
349 |
|
350 |
|
351 | |
352 |
|
353 |
|
354 |
|
355 | Handlebars.registerHelper "uppercase", (str) ->
|
356 | str.toUpperCase()
|
357 |
|
358 |
|
359 | |
360 |
|
361 |
|
362 | Handlebars.registerHelper "capitalizeFirst", (str) ->
|
363 | str.charAt(0).toUpperCase() + str.slice(1)
|
364 |
|
365 |
|
366 | |
367 |
|
368 |
|
369 | Handlebars.registerHelper "capitalizeEach", (str) ->
|
370 | str.replace /\w\S*/g, (txt) ->
|
371 | txt.charAt(0).toUpperCase() + txt.substr(1)
|
372 |
|
373 |
|
374 |
|
375 | |
376 |
|
377 |
|
378 | Handlebars.registerHelper "titleize", (str) ->
|
379 | capitalize = undefined
|
380 | title = undefined
|
381 | word = undefined
|
382 | words = undefined
|
383 | title = str.replace(/[ \-_]+/g, " ")
|
384 | words = title.match(/\w+/g)
|
385 | capitalize = (word) ->
|
386 | word.charAt(0).toUpperCase() + word.slice(1)
|
387 |
|
388 | ((->
|
389 | _i = undefined
|
390 | _len = undefined
|
391 | _results = undefined
|
392 | _results = []
|
393 | _i = 0
|
394 | _len = words.length
|
395 |
|
396 | while _i < _len
|
397 | word = words[_i]
|
398 | _results.push capitalize(word)
|
399 | _i++
|
400 | _results
|
401 | )()).join " "
|
402 |
|
403 |
|
404 | |
405 |
|
406 |
|
407 | Handlebars.registerHelper "sentence", (str) ->
|
408 | str.replace /((?:\S[^\.\?\!]*)[\.\?\!]*)/g, (txt) ->
|
409 | txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
410 |
|
411 |
|
412 |
|
413 | |
414 |
|
415 |
|
416 |
|
417 | Handlebars.registerHelper "reverse", (str) ->
|
418 | str.split("").reverse().join ""
|
419 |
|
420 |
|
421 | |
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 | Handlebars.registerHelper "truncate", (str, length, omission) ->
|
435 | omission = "" if Utils.isUndefined(omission)
|
436 | if str.length > length
|
437 | str.substring(0, length - omission.length) + omission
|
438 | else
|
439 | str
|
440 |
|
441 |
|
442 | |
443 |
|
444 |
|
445 |
|
446 | Handlebars.registerHelper "center", (str, spaces) ->
|
447 | i = undefined
|
448 | space = undefined
|
449 | space = ""
|
450 | i = 0
|
451 | while i < spaces
|
452 | space += " "
|
453 | i++
|
454 | "" + space + str + space
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | |
464 |
|
465 |
|
466 |
|
467 | Handlebars.registerHelper "first", (array, count) ->
|
468 | if Utils.isUndefined(count)
|
469 | array[0]
|
470 | else
|
471 | array.slice 0, count
|
472 |
|
473 |
|
474 | |
475 |
|
476 |
|
477 |
|
478 | Handlebars.registerHelper "withFirst", (array, count, options) ->
|
479 | item = undefined
|
480 | result = undefined
|
481 | if Utils.isUndefined(count)
|
482 | options = count
|
483 | options.fn array[0]
|
484 | else
|
485 | array = array.slice(0, count)
|
486 | result = ""
|
487 | for item of array
|
488 | result += options.fn(array[item])
|
489 | result
|
490 |
|
491 |
|
492 | |
493 |
|
494 |
|
495 |
|
496 | Handlebars.registerHelper "last", (array, count) ->
|
497 | if Utils.isUndefined(count)
|
498 | array[array.length - 1]
|
499 | else
|
500 | array.slice -count
|
501 |
|
502 |
|
503 | |
504 |
|
505 |
|
506 |
|
507 | Handlebars.registerHelper "withLast", (array, count, options) ->
|
508 | item = undefined
|
509 | result = undefined
|
510 | if Utils.isUndefined(count)
|
511 | options = count
|
512 | options.fn array[array.length - 1]
|
513 | else
|
514 | array = array.slice(-count)
|
515 | result = ""
|
516 | for item of array
|
517 | result += options.fn(array[item])
|
518 | result
|
519 |
|
520 |
|
521 | |
522 |
|
523 |
|
524 |
|
525 | Handlebars.registerHelper "after", (array, count) ->
|
526 | array.slice count
|
527 |
|
528 |
|
529 | |
530 |
|
531 |
|
532 |
|
533 | Handlebars.registerHelper "withAfter", (array, count, options) ->
|
534 | item = undefined
|
535 | result = undefined
|
536 | array = array.slice(count)
|
537 | result = ""
|
538 | for item of array
|
539 | result += options.fn(array[item])
|
540 | result
|
541 |
|
542 |
|
543 | |
544 |
|
545 |
|
546 |
|
547 | Handlebars.registerHelper "before", (array, count) ->
|
548 | array.slice 0, -count
|
549 |
|
550 |
|
551 | |
552 |
|
553 |
|
554 |
|
555 | Handlebars.registerHelper "withBefore", (array, count, options) ->
|
556 | item = undefined
|
557 | result = undefined
|
558 | array = array.slice(0, -count)
|
559 | result = ""
|
560 | for item of array
|
561 | result += options.fn(array[item])
|
562 | result
|
563 |
|
564 |
|
565 | |
566 |
|
567 |
|
568 |
|
569 | Handlebars.registerHelper "join", (array, separator) ->
|
570 | array.join (if Utils.isUndefined(separator) then " " else separator)
|
571 |
|
572 |
|
573 | |
574 |
|
575 |
|
576 |
|
577 | Handlebars.registerHelper "sort", (array, field) ->
|
578 | if Utils.isUndefined(field)
|
579 | array.sort()
|
580 | else
|
581 | array.sort (a, b) ->
|
582 | a[field] > b[field]
|
583 |
|
584 |
|
585 |
|
586 | |
587 |
|
588 |
|
589 |
|
590 | Handlebars.registerHelper "withSort", (array, field, options) ->
|
591 | item = undefined
|
592 | result = undefined
|
593 | _i = undefined
|
594 | _len = undefined
|
595 | result = ""
|
596 | if Utils.isUndefined(field)
|
597 | options = field
|
598 | array = array.sort()
|
599 | _i = 0
|
600 | _len = array.length
|
601 |
|
602 | while _i < _len
|
603 | item = array[_i]
|
604 | result += options.fn(item)
|
605 | _i++
|
606 | else
|
607 | array = array.sort((a, b) ->
|
608 | a[field] > b[field]
|
609 | )
|
610 | for item of array
|
611 | result += options.fn(array[item])
|
612 | result
|
613 |
|
614 |
|
615 | |
616 |
|
617 |
|
618 |
|
619 | Handlebars.registerHelper "length", (array) ->
|
620 | array.length
|
621 |
|
622 |
|
623 | |
624 |
|
625 |
|
626 |
|
627 | Handlebars.registerHelper "lengthEqual", (array, length, options) ->
|
628 | if array.length is length
|
629 | options.fn this
|
630 | else
|
631 | options.inverse this
|
632 |
|
633 |
|
634 | |
635 |
|
636 |
|
637 |
|
638 | Handlebars.registerHelper "empty", (array, options) ->
|
639 | if array.length <= 0
|
640 | options.fn this
|
641 | else
|
642 | options.inverse this
|
643 |
|
644 |
|
645 | |
646 |
|
647 |
|
648 |
|
649 | Handlebars.registerHelper "any", (array, options) ->
|
650 | if array.length > 0
|
651 | options.fn this
|
652 | else
|
653 | options.inverse this
|
654 |
|
655 |
|
656 | |
657 |
|
658 |
|
659 |
|
660 | Handlebars.registerHelper "inArray", (array, value, options) ->
|
661 | if array.indexOf(value) isnt -1
|
662 | options.fn this
|
663 | else
|
664 | options.inverse this
|
665 |
|
666 |
|
667 | |
668 |
|
669 |
|
670 |
|
671 |
|
672 | Handlebars.registerHelper "eachIndex", (context, options) ->
|
673 | data = undefined
|
674 | i = undefined
|
675 | j = undefined
|
676 | ret = undefined
|
677 | ret = ""
|
678 | data = Handlebars.createFrame(options.data) if options.data?
|
679 | if context and context.length > 0
|
680 | i = 0
|
681 | j = context.length
|
682 | while i < j
|
683 | data.index = i if data
|
684 | context[i].index = i
|
685 | ret = ret + options.fn(context[i])
|
686 | i++
|
687 | else
|
688 | ret = options.inverse(this)
|
689 | ret
|
690 |
|
691 |
|
692 | |
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 | Handlebars.registerHelper "keyValue", (obj, fn) ->
|
705 | buffer = ""
|
706 | key = undefined
|
707 | for key of obj
|
708 | if obj.hasOwnProperty(key)
|
709 | buffer += fn(
|
710 | key: key
|
711 | value: obj[key]
|
712 | )
|
713 | buffer
|
714 |
|
715 |
|
716 | |
717 |
|
718 |
|
719 |
|
720 | Handlebars.registerHelper "relative", (from, to) ->
|
721 | relativePath = path.relative(from, to)
|
722 | relativePath = dest if relativePath is "." or relativePath.length is 0
|
723 | relativePath = Utils.urlNormalize(path.relative(path.resolve(path.join(dest, relative)), path.resolve(relativePath)))
|
724 | relativePath
|
725 |
|
726 |
|
727 | |
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 | Handlebars.registerHelper "absolute", (absolute) ->
|
738 | absolute = __filename
|
739 | absolute
|
740 |
|
741 |
|
742 | |
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 | Handlebars.registerHelper "basename", (base, ext) ->
|
753 | fullName = path.basename(base, ext)
|
754 | base = path.basename(base, path.extname(fullName))
|
755 | base
|
756 |
|
757 |
|
758 | |
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 | Handlebars.registerHelper "filename", (base, ext) ->
|
769 | filename = path.basename(base, ext)
|
770 | filename
|
771 |
|
772 |
|
773 | |
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 | Handlebars.registerHelper "ext", (ext) ->
|
784 | extension = path.extname(ext)
|
785 | extension
|
786 |
|
787 | Handlebars.registerHelper "file", (pathname, host, url) ->
|
788 | pathname = url.parse(host).pathname
|
789 | pathname = pathname.substring(0, pathname.lastIndexOf("/"))
|
790 | pathname
|
791 |
|
792 |
|
793 | |
794 |
|
795 |
|
796 |
|
797 | Handlebars.registerHelper "add", (value, addition) ->
|
798 | value + addition
|
799 |
|
800 |
|
801 | |
802 |
|
803 |
|
804 |
|
805 | Handlebars.registerHelper "subtract", (value, substraction) ->
|
806 | value - substraction
|
807 |
|
808 |
|
809 | |
810 |
|
811 |
|
812 |
|
813 | Handlebars.registerHelper "divide", (value, divisor) ->
|
814 | value / divisor
|
815 |
|
816 |
|
817 | |
818 |
|
819 |
|
820 |
|
821 | Handlebars.registerHelper "multiply", (value, multiplier) ->
|
822 | value * multiplier
|
823 |
|
824 |
|
825 | |
826 |
|
827 |
|
828 |
|
829 | Handlebars.registerHelper "floor", (value) ->
|
830 | Math.floor value
|
831 |
|
832 |
|
833 | |
834 |
|
835 |
|
836 |
|
837 | Handlebars.registerHelper "ceil", (value) ->
|
838 | Math.ceil value
|
839 |
|
840 |
|
841 | |
842 |
|
843 |
|
844 |
|
845 | Handlebars.registerHelper "round", (value) ->
|
846 | Math.round value
|
847 |
|
848 |
|
849 | |
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 | Handlebars.registerHelper "toFixed", (number, digits) ->
|
856 | digits = 0 if Utils.isUndefined(digits)
|
857 | number.toFixed digits
|
858 |
|
859 |
|
860 | |
861 |
|
862 |
|
863 |
|
864 |
|
865 | Handlebars.registerHelper "toPrecision", (number, precision) ->
|
866 | precision = 1 if Utils.isUndefined(precision)
|
867 | number.toPrecision precision
|
868 |
|
869 |
|
870 | |
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 | Handlebars.registerHelper "toExponential", (number, fractions) ->
|
877 | fractions = 0 if Utils.isUndefined(fractions)
|
878 | number.toExponential fractions
|
879 |
|
880 |
|
881 | |
882 |
|
883 |
|
884 |
|
885 | Handlebars.registerHelper "toInt", (number) ->
|
886 | parseInt number, 10
|
887 |
|
888 |
|
889 | |
890 |
|
891 |
|
892 |
|
893 | Handlebars.registerHelper "toFloat", (number) ->
|
894 | parseFloat number
|
895 |
|
896 |
|
897 | |
898 |
|
899 |
|
900 |
|
901 | Handlebars.registerHelper "addCommas", (number) ->
|
902 | number.toString().replace /(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"
|
903 |
|
904 |
|
905 | |
906 |
|
907 |
|
908 |
|
909 | Handlebars.registerHelper "is", (value, test, options) ->
|
910 | if value is test
|
911 | options.fn this
|
912 | else
|
913 | options.inverse this
|
914 |
|
915 |
|
916 | |
917 |
|
918 |
|
919 |
|
920 | Handlebars.registerHelper "isnt", (value, test, options) ->
|
921 | if value isnt test
|
922 | options.fn this
|
923 | else
|
924 | options.inverse this
|
925 |
|
926 |
|
927 | |
928 |
|
929 |
|
930 |
|
931 | Handlebars.registerHelper "gt", (value, test, options) ->
|
932 | if value > test
|
933 | options.fn this
|
934 | else
|
935 | options.inverse this
|
936 |
|
937 |
|
938 | |
939 |
|
940 |
|
941 |
|
942 |
|
943 | Handlebars.registerHelper "gte", (value, test, options) ->
|
944 | if value >= test
|
945 | options.fn this
|
946 | else
|
947 | options.inverse this
|
948 |
|
949 |
|
950 | |
951 |
|
952 |
|
953 |
|
954 |
|
955 | Handlebars.registerHelper "lt", (value, test, options) ->
|
956 | if value < test
|
957 | options.fn this
|
958 | else
|
959 | options.inverse this
|
960 |
|
961 |
|
962 | |
963 |
|
964 |
|
965 |
|
966 | Handlebars.registerHelper "lte", (value, test, options) ->
|
967 | if value <= test
|
968 | options.fn this
|
969 | else
|
970 | options.inverse this
|
971 |
|
972 |
|
973 | |
974 |
|
975 |
|
976 |
|
977 | Handlebars.registerHelper "or", (testA, testB, options) ->
|
978 | if testA or testB
|
979 | options.fn this
|
980 | else
|
981 | options.inverse this
|
982 |
|
983 |
|
984 | |
985 |
|
986 |
|
987 |
|
988 | Handlebars.registerHelper "and", (testA, testB, options) ->
|
989 | if testA and testB
|
990 | options.fn this
|
991 | else
|
992 | options.inverse this
|
993 |
|
994 |
|
995 | |
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 | Handlebars.registerHelper "formatPhoneNumber", (phoneNumber) ->
|
1005 | phoneNumber = phoneNumber.toString()
|
1006 | "(" + phoneNumber.substr(0, 3) + ") " + phoneNumber.substr(3, 3) + "-" + phoneNumber.substr(6, 4)
|
1007 |
|
1008 |
|
1009 | |
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 | Handlebars.registerHelper "formatDate", (date, format) ->
|
1017 | date = new Date(date)
|
1018 | Dates.format date, format
|
1019 |
|
1020 |
|
1021 | |
1022 |
|
1023 |
|
1024 |
|
1025 | Handlebars.registerHelper "now", (format) ->
|
1026 | date = undefined
|
1027 | date = new Date()
|
1028 | if Utils.isUndefined(format)
|
1029 | date
|
1030 | else
|
1031 | Dates.format date, format
|
1032 |
|
1033 |
|
1034 | |
1035 |
|
1036 |
|
1037 |
|
1038 | Handlebars.registerHelper "timeago", (date) ->
|
1039 | interval = undefined
|
1040 | seconds = undefined
|
1041 | date = new Date(date)
|
1042 | seconds = Math.floor((new Date() - date) / 1000)
|
1043 | interval = Math.floor(seconds / 31536000)
|
1044 | return "" + interval + " years ago" if interval > 1
|
1045 | interval = Math.floor(seconds / 2592000)
|
1046 | return "" + interval + " months ago" if interval > 1
|
1047 | interval = Math.floor(seconds / 86400)
|
1048 | return "" + interval + " days ago" if interval > 1
|
1049 | interval = Math.floor(seconds / 3600)
|
1050 | return "" + interval + " hours ago" if interval > 1
|
1051 | interval = Math.floor(seconds / 60)
|
1052 | return "" + interval + " minutes ago" if interval > 1
|
1053 | if Math.floor(seconds) is 0
|
1054 | "Just now"
|
1055 | else
|
1056 | Math.floor(seconds) + " seconds ago"
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 | |
1066 |
|
1067 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 | Handlebars.registerHelper "inflect", (count, singular, plural, include) ->
|
1087 | word = undefined
|
1088 | word = (if count > 1 or count is 0 then plural else singular)
|
1089 | if Utils.isUndefined(include) or include is false
|
1090 | word
|
1091 | else
|
1092 | "" + count + " " + word
|
1093 |
|
1094 |
|
1095 | |
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 |
|
1104 |
|
1105 |
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 | Handlebars.registerHelper "ordinalize", (value) ->
|
1113 | normal = undefined
|
1114 | _ref = undefined
|
1115 | normal = Math.abs(Math.round(value))
|
1116 | _ref = normal % 100
|
1117 | if __indexOf_.call([11, 12, 13], _ref) >= 0
|
1118 | "" + value + "th"
|
1119 | else
|
1120 | switch normal % 10
|
1121 | when 1
|
1122 | "" + value + "st"
|
1123 | when 2
|
1124 | "" + value + "nd"
|
1125 | when 3
|
1126 | "" + value + "rd"
|
1127 | else
|
1128 | "" + value + "th"
|
1129 |
|
1130 |
|
1131 | |
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 |
|
1166 |
|
1167 | Handlebars.registerHelper "ul", (context, options) ->
|
1168 | ("<ul " + (HTML.parseAttributes(options.hash)) + ">") + context.map((item) ->
|
1169 | "<li>" + (options.fn(item)) + "</li>"
|
1170 | ).join("\n") + "</ul>"
|
1171 |
|
1172 |
|
1173 | |
1174 |
|
1175 |
|
1176 |
|
1177 | Handlebars.registerHelper "ol", (context, options) ->
|
1178 | ("<ol " + (HTML.parseAttributes(options.hash)) + ">") + context.map((item) ->
|
1179 | "<li>" + (options.fn(item)) + "</li>"
|
1180 | ).join("\n") + "</ol>"
|
1181 |
|
1182 |
|
1183 | |
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 | Handlebars.registerHelper "stripes", (array, even, odd, fn) ->
|
1202 | if array and array.length > 0
|
1203 | buffer = ""
|
1204 | i = 0
|
1205 | j = array.length
|
1206 |
|
1207 | while i < j
|
1208 | item = array[i]
|
1209 |
|
1210 |
|
1211 | item.stripeClass = ((if i % 2 is 0 then even else odd))
|
1212 |
|
1213 |
|
1214 | buffer += fn(item)
|
1215 | i++
|
1216 |
|
1217 |
|
1218 | buffer
|
1219 |
|
1220 |
|
1221 | |
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 |
|
1233 |
|
1234 |
|
1235 |
|
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 |
|
1241 | Handlebars.registerHelper "timeline", (array, left, right, fn) ->
|
1242 | if array and array.length > 0
|
1243 | buffer = ""
|
1244 | i = 0
|
1245 | j = array.length
|
1246 |
|
1247 | while i < j
|
1248 | item = array[i]
|
1249 |
|
1250 |
|
1251 | item.columnClass = ((if i % 2 is 0 then left else right))
|
1252 |
|
1253 |
|
1254 | buffer += fn(item)
|
1255 | i++
|
1256 |
|
1257 |
|
1258 | buffer
|
1259 |
|
1260 |
|
1261 | |
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 | Handlebars.registerHelper "br", (count, options) ->
|
1271 | br = undefined
|
1272 | i = undefined
|
1273 | br = "<br>"
|
1274 | unless Utils.isUndefined(count)
|
1275 | i = 0
|
1276 | while i < count - 1
|
1277 | br += "<br>"
|
1278 | i++
|
1279 | Utils.safeString br
|
1280 |
|
1281 |
|
1282 | |
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 | Handlebars.registerHelper "eachProperty", (context, options) ->
|
1292 | ret = ""
|
1293 | for prop of context
|
1294 | ret = ret + options.fn(
|
1295 | property: prop
|
1296 | value: context[prop]
|
1297 | )
|
1298 | ret
|
1299 |
|
1300 |
|
1301 | |
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 | inlinePartials = {}
|
1317 | Handlebars.registerHelper "extend", (name, context) ->
|
1318 | include = inlinePartials[name]
|
1319 | include = inlinePartials[name] = [] unless include
|
1320 | include.push context.fn(this)
|
1321 |
|
1322 | Handlebars.registerHelper "include", (name) ->
|
1323 | val = (inlinePartials[name] or []).join("\n")
|
1324 |
|
1325 |
|
1326 | inlinePartials[name] = []
|
1327 | val
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 | |
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 | Handlebars.registerHelper "log", (value) ->
|
1349 | console.log value
|
1350 |
|
1351 |
|
1352 | |
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 |
|
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 |
|
1377 |
|
1378 | Handlebars.registerHelper "debug", (value) ->
|
1379 | console.log "Context: ", this
|
1380 | console.log "Value: ", value unless Utils.isUndefined(value)
|
1381 | console.log "-----------------------------------------------"
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 | |
1391 |
|
1392 |
|
1393 |
|
1394 |
|
1395 |
|
1396 |
|
1397 |
|
1398 |
|
1399 |
|
1400 |
|
1401 |
|
1402 | Handlebars.registerHelper "default", (value, defaultValue) ->
|
1403 | (if value? then value else defaultValue)
|
1404 |
|
1405 |
|
1406 | |
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 | Handlebars.registerHelper "markdown", (options) ->
|
1422 | content = options.fn(this)
|
1423 | markdown.convert content
|
1424 |
|
1425 | if isServer
|
1426 |
|
1427 | |
1428 |
|
1429 |
|
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
1435 | Handlebars.registerHelper "md", (path) ->
|
1436 | content = markdown.read(path)
|
1437 | content
|
1438 |
|
1439 |
|
1440 | register: register
|