forked from ffissore/presentz.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.coffee
137 lines (110 loc) · 4.52 KB
/
utils.coffee
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
###
Presentz.org - A website to publish presentations with video and slides synchronized.
Copyright (C) 2012 Federico Fissore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
###
"use strict"
if require?
XRegExp = require("./node_modules/xregexp/xregexp-all.js").XRegExp
else if window?
XRegExp = window.XRegExp
exec_for_each = (callable, elements, callback) ->
return callback(undefined, elements) if elements.length is 0
exec_times = 0
for element in elements
callable element, (err) ->
return callback(err) if err?
exec_times++
return callback(undefined, elements) if exec_times is elements.length
cut_string_at = (str, size) ->
if str? and str.length > size + 3
return "#{str.substring(0, size)}..."
str
remove_unwanted_fields_from = (obj, fields_to_remove) ->
for key, value of obj
delete obj[key] if key in fields_to_remove
ensure_only_wanted_fields_in = (obj, allowed_fields) ->
for key, value of obj
delete obj[key] unless key in allowed_fields
remove_unwanted_map_of_fields_from = (objtype, obj, map_of_fields_to_remove) ->
return unless map_of_fields_to_remove[objtype]?
fields_to_remove = map_of_fields_to_remove[objtype]
remove_unwanted_fields_from obj, fields_to_remove
ensure_only_wanted_map_of_fields_in = (objtype, obj, map_of_allowed_fields) ->
return unless map_of_allowed_fields[objtype]?
allowed_fields = map_of_allowed_fields[objtype]
ensure_only_wanted_fields_in obj, allowed_fields
visit_presentation = (presentation, func, fields_or_maps_of_fields) ->
if func.length is 3
apply_on = (objtype, obj) ->
func.call null, objtype, obj, fields_or_maps_of_fields
else
apply_on = (objtype, obj) ->
func.call null, obj, fields_or_maps_of_fields
apply_on "presentation", presentation
if presentation.comments?
for comment in presentation.comments
apply_on "comment", comment
apply_on "user", comment.user if comment.user?
return unless presentation.chapters?
for chapter in presentation.chapters
apply_on "chapter", chapter
apply_on "video", chapter.video if chapter.video?
if chapter.slides?
for slide in chapter.slides
apply_on "slide", slide
if slide.comments?
for comment in slide.comments
apply_on "comment", comment
apply_on "user", comment.user if comment.user?
presentation
chars = "0123456789qwertyuiopasdfghjklzxcvbnm"
non_word_chars = XRegExp("[^\\p{L}\\p{N}]", "g")
punct = XRegExp("\\p{P}", "g")
short_words = XRegExp("\\b\\w{1,2}\\b", "g")
spaces = XRegExp("\\s", "g")
generate_id = (accent_fold, title) ->
id = ""
for idx in [0...5]
c = parseInt(Math.random() * chars.length)
id = id.concat(chars[c])
return id unless title? and title isnt ""
title = accent_fold(title.toLowerCase())
#title = XRegExp.replace(title, non_word_chars, " ")
title = XRegExp.replace(title, punct, "")
title = XRegExp.replace(title, short_words, "")
title = XRegExp.replace(title, spaces, "_")
while title.indexOf("__") isnt -1
title = title.replace("__", "_")
title = title.replace(/[_]+$/, "")
title = title.replace(/^[_]+/, "")
title.concat("_", id)
is_url_valid = (url) ->
url = "http://www.example.com#{url}" if url.indexOf("/") is 0
uri = Uri(url)
host = uri.host()
path = uri.path()
host? and host isnt "" and host.indexOf(".") isnt -1 and path? and path isnt ""
my_parse_float = (s, precision = 100) ->
f = parseFloat(s)
Math.round(f * precision) / precision
root = exports ? (@utils = {})
root.exec_for_each = exec_for_each
root.cut_string_at = cut_string_at
root.remove_unwanted_fields_from = remove_unwanted_fields_from
root.remove_unwanted_map_of_fields_from = remove_unwanted_map_of_fields_from
root.ensure_only_wanted_fields_in = ensure_only_wanted_fields_in
root.ensure_only_wanted_map_of_fields_in = ensure_only_wanted_map_of_fields_in
root.visit_presentation = visit_presentation
root.generate_id = generate_id
root.is_url_valid = is_url_valid
root.my_parse_float = my_parse_float