-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexthelper.js
More file actions
134 lines (121 loc) · 3.36 KB
/
texthelper.js
File metadata and controls
134 lines (121 loc) · 3.36 KB
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
(function(window, undefined) {
// Useful whitespace regEx available throughout texthelperjs.
var regwsGlobal = /\s+/g,
regwsLeft = /^\s+/,
regwsRight = /\s+$/,
regwsBoth = /(^\s+|\s+$)/g,
regwsLarge = /[\t\n\r]/g;
var texthelper = {
version: '0.2',
truncate: function(text, length, omission) {
var truncated = '',
sentence = this.trimBoth(text),
append = ( omission == null ) ? "..." : omission;
if(length > 0){
truncated = text.substring(0, length-1) + append
}
else if(length < 0){
truncated = append + text.substring(length*-1, sentence.length)
}
else{
truncated = sentence;
}
return truncated;
},
excerpt: function(text, interest, radius ) {
var index = 0,
text = this.trimBoth(text),
excerpt = '',
radius = radius || 10
keyword = interest
start = 0,
stop = 0;
index = text.indexOf(keyword);
start = index-radius >= 0? index-radius: 0;
stop = index+keyword.length+radius < text.length? index+keyword.length+radius: text.length;
excerpt = text.substring(start, stop);
return excerpt;
},
highlight: function(text, keyword, className) {
var regEx = new RegExp(keyword, "g"),
sameChars = 0,
openTag = "",
closeTag = "",
className = this.trimBoth(className) || "",
text = this.trimBoth(text),
keyword = this.trimBoth(keyword);
if(className.length > 0){
openTag = "<span class='"+className+"'>";
closeTag = "</span>";
}
else{
openTag = "<mark>";
closeTag = "</mark>";
}
text = text.replace(regEx, openTag+keyword+closeTag);
return text;
},
wordWrap: function(text, lineWidth) {
var text = text,
i = lineWidth,
textLength = text.length,
editedText = text,
difference = 0;
if(lineWidth > 0){
for( ; i < textLength; i++){
if(text.charAt(i) == " "){
difference = editedText.length - textLength;
test = editedText.split("");
test.splice(i+difference, 0, "<br>");
editedText = test.join("");
i += lineWidth;
}
}
return editedText;
}
else{
return editedText;
}
},
htmlEscape: function(text) {
var text = this.trimBoth(text);
return text = text.replace(/(<|>|&|"|')/g, function(match) {
switch (match) {
case '<':
return match = '<';
case '>':
return match = '>';
case '&':
return match = '&';
case '"':
return match = '"';
case "'":
return match = ''';
}
});
},
htmlGenerate: function(name, attributes, contents) {
var autoclose = ['img','iframe'],
tag = "<" + name;
contents = contents ? contents : "";
for(key in attributes){
if(attributes.hasOwnProperty(key)){
tag += " " + key + "='" + attributes[key] + "'";
}
}
if(autoclose.indexOf(name) >= 0){
tag += " />"
}
else {
tag += ">" + contents + "</" + name + ">";
}
return tag;
},
// Helpful utility methods.
trimBoth: function(text) {
return text != undefined ? text.toString().replace(regwsBoth, '') : false;
}
};
// Make texthelper available throughout the global namespace.
window.texthelper = window._text = texthelper;
})(window);