-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuam.js
90 lines (73 loc) · 2.55 KB
/
Guam.js
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
/**
* Created by ToothlessRebel on 12-May-16.
*/
function Guam(input) {
input = input || {};
var options = {
debug: false,
toString: function () {
return '{debug: ' + this.debug + '}';
}
};
var not_parsed = [
'script'
];
options = $.extend(options, input);
var elements = {
to_parse: function () {
var top_level = $('body').children().filter(function (index, element) {
return 0 > not_parsed.indexOf($(element).prop('tagName').toLowerCase());
});
var elements = top_level.toArray();
$.each(top_level, function (index, element) {
var $element = $(element);
var tag = $element.prop('tagName');
if (0 > not_parsed.indexOf(tag)) {
if (0 <= $element.children().length) {
$element.children().each(function (index, element) {
elements.push(element);
});
}
}
});
return elements;
},
toString: function () {
var elements = [];
$.each(this.to_parse(), function (index, element) {
elements.push($(element).prop('tagName').toLowerCase());
});
return '[' + elements.join(', ') + ']';
}
};
var iterateElements = function () {
$.each(elements.to_parse(), function (index, element) {
var $element = $(element);
var tag = $element.prop('tagName');
if (0 > not_parsed.indexOf(tag)) {
if (0 <= $element.children().length) {
$(elements).push($element.children());
}
}
});
return elements;
};
var translateElement = function (element) {
var $element = $(element);
console.log('Replacing:', $element.html());
var translated = $element.html().replace(/(<.*?>\s*)*[a-zA-Z]+([.!?,])*(\s*<\/.*?>)*/g, ' $1guam$2$3 ');
console.log('With:', translated);
$element.html(translated);
};
this.translate = function () {
$.each(elements.to_parse(), function (index, element) {
translateElement(element);
})
};
if (options.debug) {
this.toString = function () {
return 'Guam{options: ' + options.toString() + ', elements: ' + elements.toString() + ', not_parsed: ' + not_parsed + '}';
}
}
elements.elements = iterateElements();
}