-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.waokon.js
84 lines (73 loc) · 2.31 KB
/
jquery.waokon.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
(function($) {
$.fn.waokon = function(){
var noLeftMarginWith = /[、。:;「」)!?#$%&’@]$/;
var noRightMarginWith = /^[、。:;「」(!?#$%’℃€£¢‰Å㏋㍉㌔㌢㍍㌘㌧㌃㌶㍑㍗㌍㌦㌣㌫㍊㌻㎜㎝㎞㎎㎏㏄㎡]/;
var romanString = /[\w\s\.,"'+-=*\/!#\$%\|@&;:\[\]\{\}<>°'″′“”‘’]+/ig;
var romanClass = 'waokon';
var numberClass = 'waokonNumber';
var shortMarginClass = 'shortMargin';
var noLeftMarginClass = 'noLeftMargin';
var noRightMarginClass = 'noRightMargin';
var langIdentifier = 'en';
return this.each(function(){
var elem = $(this);
elem.markTextByRegExp(romanString,$('<span class="'+romanClass+'" lang="'+langIdentifier+'" />'));
$('.'+romanClass, elem).markTextByRegExp(/[\d,\.\s-]+/, $('<span class="'+numberClass+'" />'));
var nodes = elem.contents();
var trimSpace = function(_elem){
var text = elem.text();
elem.text(text.replace(/^\s+?([^\s]+)\s+$/,RegExp.$1))
}
var isNumber = function(){
if(/^[\d,\.\s-]+$/.test($(nodes[i]).text())){
return true;
}
return false;
}
var isShortMargin = function(i){
if(/[\d\.,\(\)\/]+|^\w{1,3}$/.test($(nodes[i]).text())){
return true;
}
return false;
}
var isNoLeftMargin = function(i){
if(i==0){
if(elem.css('display') == 'block'){
return true;
}else if(elem.is(':first-child') && elem.parent().css('display') == 'block'){
return true;
}
}else if(noLeftMarginWith.test($(nodes[i-1]).text()) || noLeftMarginWith.test(nodes[i-1].data)){
return true;
}
return false;
}
var isNoRightMargin= function(i){
if(i<l-1 && typeof(nodes[i+1]) == 'object'){
if(noRightMarginWith.test($(nodes[i+1]).text()) || noRightMarginWith.test(nodes[i+1].data) || $(nodes[i+1]).is('sub,sup')){
return true;
}
}
return false;
}
for(var i=0,l=nodes.length;i<l;i++){
var node = $(nodes[i]);
if(node.is('.'+romanClass)){
//trimSpace(node);
if(isNumber(i)){
node.addClass(numberClass)
}
if(isShortMargin(i)){
node.addClass(shortMarginClass)
}
if(isNoLeftMargin(i)){
node.addClass(noLeftMarginClass)
}
if(isNoRightMargin(i)){
node.addClass(noRightMarginClass)
}
}
}
});
};
})(jQuery);