forked from aigarskadikis/aigarskadikis.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleLine.togglerIE.js
148 lines (134 loc) · 4.01 KB
/
singleLine.togglerIE.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
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
138
139
140
141
142
143
144
145
146
// IE doesn't support endWith
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
/**
* String.prototype.replaceAll() polyfill
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
* @author Chris Ferdinandi
* @license MIT
*/
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(str, newStr){
// If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
// If a string
return this.replace(new RegExp(str, 'g'), newStr);
};
}
storage = {};
converted = {};
processedArrays = {};
function toSingleLine(ev) {
var blocks = document.querySelectorAll('pre code');
var ieArray = [];
for(var i = 0; i< blocks.length; i++) {
ieArray.push(blocks[i]);
}
if (ev) {
ieArray.forEach(function (block, i) {
processBlock(block, i);
});
return;
}
if(!storage) {
//IE cached view without stored real data
return;
}
ieArray.forEach(function (block, i) {
block.innerHTML = storage[i].text;
});
}
function processBlock(block, i) {
if (!storage[i]) {
storage[i] = {
index: i,
text: block.innerHTML
};
}
// not workning in IE
// var rawText = block.innerText;
// var lines = rawText.split('/\n/');
var lines = block.innerHTML.split('\n');
var result = [];
var shouldMerge = false;
var doubleQuote = false;
lines.forEach(function (line) {
// Rule 1
if (line.endsWith('\\')){
line = line.substr(0, line.length-1)
shouldMerge = true;
}
// Rule 3
if (line.endsWith('"') && line.indexOf('"') == line.length -1) {
doubleQuote = true;
}
// Rule 4, if line contains ' AS '
if (/^.* AS .*$/.test(line)) {
var startsWithCapital = (/^[A-Z]+.* AS/.test(line));
// either it starts with capital letter (and contain 'AS') or not do the merging, but do it only once.
if ( !startsWithCapital ) {
containsAS = true;
line = line + ' ';
shouldMerge = true;
} else {
containsAS = true;
line = line + ' ';
shouldMerge = true;
}
}
// Rule 5, if line ends with ',' and there are no double spaces. Rule 5 cannot be active together with Rule 4.
else if (/^.*,$/.test(line) && /^\S+$/.test(line)) {
noComaNoSpaces = true;
line = line + ' ';
shouldMerge = true;
} else
// Rule 2
if (/^[A-Z]/.test(line)) {
// first letter is capital. Let's check for '=' in first word
var hasEqual = (line.split(' ') || [''])[0].indexOf('=') < 0;
if ( hasEqual) {
line = line + ' ';
shouldMerge = true;
}
} else
if (/^[a-z_]+\.[a-z_]+$/.test(line)) {
tableAndColumn = true;
line = line + ' ';
shouldMerge = true;
} else
if (/^\)\s+\S+$/.test(line)) {
parentacyAndAlias = true;
line = line + ' ';
shouldMerge = true;
}
if (shouldMerge || doubleQuote) {
result.push(line);
result.push('MergeMark');
if (doubleQuote && !shouldMerge) {
doubleQuote = false;
}
shouldMerge = false;
} else {
result.push(line);
}
});
processedArrays[i] = result;
var combined = result.join('\n');
// simply replace merge marge with empty line = merge
var prefinalLine = combined.replaceAll(/\nMergeMark\n/g, '');
// clean up last occurance if merged with last line
var finalLine = prefinalLine.replace('\nMergeMark', '').replace(' )', ')').replace(/\( SELECT /g, '(SELECT ');
converted[i] = {
index: i,
text: finalLine
};
block.innerText = converted[i].text;
}