-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
87 lines (75 loc) · 2.11 KB
/
index.html
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
<a href=//github.com/xem/prettycode><img style="position:absolute;top:0;right:0;border:0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
<style>
body { width: 850px; font-family: arial; margin: auto; }
pre { text-align: left; }
pre p { margin: 10px 0; padding: 10px; background: #ffb; porder-radius: 10px; }
input { width: 100% }
</style>
<h1>PrettyCode</h1>
<br>
<b>A code-golfed tool that helps formatting code-golfed code</b>
<br>
<br>
Golfed by xem, subzey and aemkei in <a href=new.html>166 bytes</a> / <a href=tweet.html>140 chars</a>
<br>
<br>
<br>
<h2>Features:</h2>
<br>
- Splits minified code in lines of the same length (1 to 100 chars/line)
<br>
- Eliminates the splits that break words, numbers, HTML tags or JS operators
<br>
- Avoid orphan characters (trailing character on the last line)
<br>
- Display the results in real-time
<br>
<br>
<br>
<h2>Demo:</h2>
<br>
<br>
Enter some code to prettify here:
<br>
<br>
<input id=n>
<br>
<br>
Result(s):
<br>
<br>
<pre id=p>
<script>
// Load example
n.value="<input oninput='h=n=\"\n\";for(i=99;--i;v?0:h+=n+i+n+t.join(n)+n)for(t=value.match(RegExp(\".{0,\"+i+\"}\",\"g\")),v=j=0;t[j+1];)v|=!t[j][2]|/[\w<>.]{2}|[+|&]{2}|[+*\/%&=|<>^!-]=/.test(t[j][i-1]+t[++j][0]);p.textContent=h'><pre id=p>";
// Onload, on input: process
(oninput=function(){
// Flush result
p.innerHTML="";
// Loop on lines lengths
for(i=10;i<100;i++){
// Make an array of lines
t=[];
// Copy the input
d=n.value;
// Slice it
while(d){
t.push(d.substr(0,i));
d=d.slice(i);
}
// Assume the result is valid
v=1;
// Loop on the lines
for(j=0;j<t.length-1;j++){
// If a word / number / html tag / operator is cut
if(t[j+1].length<2||(t[j].charAt(i-1)+t[j+1].charAt(0)).match(/[\w]{2}|<\w|\w>|\.\d|\d\.|\+=|-=|\*=|\/=|%=|&=|\|=|^=|\+\+|!=|>=|<=|&&|\|\||<<|>>/))
// Invalid
v=0;
}
// Print the valid results
if(v){
p.innerHTML+="<c>"+i+" chars/line<p>"+t.join("\n").replace(/</g,"<");
}
}
})()
</script>