-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaligner.htm
256 lines (228 loc) · 6.9 KB
/
aligner.htm
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<html>
<head>
<style type='text/css'>
table {
border-collapse: collapse ;
}
div.center-column {
margin-left : 10% ;
width : 80% ;
}
textarea.data {
width : 100% ;
height : 40% ;
font-family : monospace ;
white-space : pre;
word-wrap : normal;
}
input.format {
width : 100% ;
}
ul.options {
display : block ;
text-align : left ;
margin : 0px ;
padding : 0px ;
}
ul.options li {
display : inline-block;
}
input[name=tabwidth] {
width : 20 ;
text-align : center ;
}
table.options td {
width : 90px ;
white-space : nowrap ;
}
.github {
text-align : right ;
}
.github a {
padding-left : 10px ;
}
</style>
<script type='text/javascript'>
(function() {
// var
ns = {};
window.ns = ns;
// Check to see if the document is complete
ns.checkReady = function() {
// If the document is loaded
if(document.readyState == "complete") {
// Remove the readystate timer
window.clearInterval(ns.interval);
// Find format input element
ns.$format = document.querySelector("input.format");
// Listen to format changes
ns.$format.addEventListener("keyup", ns.format);
// Find theInput
ns.$input = document.querySelector("#theInput");
// Get all the inputs we need
for(name in {padLeft:1, sort:1, tabwidth:1})
// Find input name='foo' and assign to ns.$foo
ns["$" + name] = document.querySelector("input[name="+name+"]");
// Listen to input changes
(function() {
for(var i=0; i<arguments.length; ++i) {
arguments[i].addEventListener("keyup", ns.format);
arguments[i].addEventListener("click", ns.format);
}
})(ns.$input, ns.$padLeft, ns.$sort, ns.$tabwidth);
// Find theOutput
(ns.$output = document.querySelector("#theOutput"))
// Listen to the theOutput clicks
.addEventListener("click", ns.selectResult);
// Start off with a format
ns.format();
}
};
// Format the block of text
ns.format = function() {
var format = ns.$format.value.trim();
var components = format.split(/\s+/);
var input = ns.$input.value;
var lines = input.split("\n");
var condom = 0;
var padLeft= ns.$padLeft.checked;
var sort = ns.$sort.checked ;
var tabwidth = parseInt(ns.$tabwidth.value) || 0;
// The column that we'll be aligning to
var oldColumn = 0, column = 0;
var regex = new RegExp("\\s*(" + components.join("|") + ")");
// If there is a tabwidth in the first place, then untab lines
if(tabwidth > 0)
// Go through each line
for(var i=0, iMax=lines.length; i<iMax; ++i) {
// Get the current line
var line = lines[i];
// Go through each character in this line
for(var j=0, jMax=line.length; j<jMax; ++j) {
var c = line[j];
// If this character is a tab, then
if(c == "\t") {
line =
// Before the tab
line.substr(0, j)
// Spaces to replace the tab
+ new Array(1 + (tabwidth - (j % tabwidth))).join(" ")
// After the tab
+ line.substr(j+1)
;
}
}
// Put the candle back
lines[i] = line;
}
while(true) {
// Go through all the lines
for(var i=0, iMax=lines.length; i<iMax; ++i) {
var line = lines[i];
var offset = line.substr(oldColumn).search(regex);
if(offset > -1) {
// Move the column to this offset
column = Math.max(column, offset + oldColumn);
}
}
for(var i=0, iMax=lines.length; i<iMax; ++i) {
var line = lines[i];
var offset = line.substr(oldColumn).search(regex);
if(offset > -1) {
offset += oldColumn;
line =
// Everything left of the column
line.substr(0, offset)
// Padding based on longest line
+ new Array(column+1+(padLeft?1:0)-offset).join(" ")
// Everything from the column onward
+ line.substr(offset).replace(/^\s*/, "")
;
lines[i] = line;
}
}
if(column + 1 == oldColumn) break;
// Remember the previous column
oldColumn = column + 1 + (padLeft ? 1 : 0);
if(++condom > 100) break;
}
// If we're supposed to sort lines
if(sort) {
// Sort lines
lines.sort();
// If the first line is now whitespace, then put at end
if(/^\s*$/.test(lines[0])) lines.push(lines.shift());
}
// Retab lines if tabwidth is set
if(tabwidth > 0)
for(var i=0, iMax=lines.length; i<iMax; ++i) {
// Get leading whitespace
var leader = lines[i].match(/^ */)[0];
lines[i] =
// Replace leading whitespace with tabs
leader.replace(new RegExp(" {"+tabwidth+"}", "g"), "\t")
// Get everything after the leader
+ lines[i].substr(leader.length)
}
ns.$output.value = lines.join("\n");
};
ns.selectResult = function(e) {
// Resolve event
e = e || window.event;
// Don't bubble this event
e.cancelPropogation = true;
// Select from the beginning
ns.$output.selectionStart = 0;
// Select to the end
ns.$output.selectionEnd = ns.$output.value.length
};
// Add a ready state change listener
ns.interval = window.setInterval(ns.checkReady, 100);
})("aligner");
</script>
</head>
<body>
<div class='center-column'>
<table style='width: 100%'>
<tr>
<td>
Format:
</td>
<td style='width: 100%'>
<input type='text' class='format' value='; :' />
</td>
</tr>
</table>
<table class='options'><tr>
<td>
Pad Left:
<input type='checkbox' name='padLeft' />
</td>
<td>
Sort:
<input type='checkbox' name='sort' />
</td>
<td>
Tabstop:
<input type='text' name='tabwidth' value='4' />
</td>
</tr></table>
<textarea class='data' id='theInput'>
switch(something) {
case 1: doSomething(); break; // Do something
case 2: doSomethingElse(); break ; // Do something else
case 33: doSomethingElse(); break; // Do something else
</textarea>
<!--
default:
fail("Not a chance"); // Didn't expect this
break;
}
-->
<textarea class='data' id='theOutput'></textarea>
<div class='github link'>
<a class='download' href='https://raw.github.com/tarwich/Settings/master/aligner.htm'>download from github</a>
</div>
</div>
</body>
</html>