git @ Cat's Eye Technologies HTML5-Gewgaws / 1e820e2
Add text-uniquifier because I guess it's a gewgaw. Chris Pressey 10 years ago
2 changed file(s) with 138 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 <!DOCTYPE html>
1 <head>
2 <meta charset="utf-8">
3 <title>Text Uniquifier</title>
4 <style>
5 #canvas { border: 1px solid blue; }
6 </style>
7 </head>
8 <body>
9
10 <h1>Text Uniquifier</h1>
11
12 <div id="control_panel">
13 </div>
14
15 </body>
16 <script src="text-uniquifier.js"></script>
17 <script>
18 launch('../common-yoob.js-0.6/', 'control_panel');
19 </script>
0 "use strict";
1
2 function launch(prefix, containerId) {
3 var deps = [
4 "element-factory.js"
5 ];
6 var loaded = 0;
7 for (var i = 0; i < deps.length; i++) {
8 var elem = document.createElement('script');
9 elem.src = prefix + deps[i];
10 elem.onload = function() {
11 if (++loaded == deps.length) {
12 var container = document.getElementById(containerId);
13 var input = yoob.makeTextArea(
14 container, 80, 10
15 );
16 yoob.makeLineBreak(container);
17 var caseSensitive = yoob.makeCheckbox(
18 container, true, "Case-sensitive"
19 );
20 var span = document.createElement('span');
21 span.innerHTML = "&nbsp;&nbsp;";
22 container.appendChild(span);
23 var puncSensitive = yoob.makeCheckbox(
24 container, true, "Punctuation-sensitive"
25 );
26 var preserve = yoob.makeSelect(
27 container, "&nbsp; Preserve:", [
28 ['paragraph_breaks', "Paragraph breaks only", true],
29 ['line_breaks', "All line breaks"],
30 ['no_breaks', "Nothing"]
31 ]
32 );
33
34 var button = yoob.makeButton(container, "Uniquify");
35
36 yoob.makeLineBreak(container);
37
38 var output = document.createElement('pre');
39 container.appendChild(output);
40 output.style.textAlign = "left";
41 output.style.whiteSpace = "pre-wrap";
42 output.style.wordBreak = "normal";
43
44 button.style.cursor = 'pointer';
45 button.onclick = function() {
46 button.style.cursor = 'wait';
47 var preserveWhat = preserve.options[preserve.selectedIndex].value;
48 output.innerHTML = uniquify(
49 input.value, preserveWhat,
50 caseSensitive.checked, puncSensitive.checked
51 );
52 button.style.cursor = 'pointer';
53 };
54 }
55 };
56 document.body.appendChild(elem);
57 }
58 }
59
60 function stripPunctuation(s) {
61 var t = '';
62 for (var i = 0; i < s.length; i++) {
63 var c = s.charAt(i);
64 if ((c >= "0" && c <= "9") || (c >= "A" && c <= "Z") || (c >= 'a' && c <= 'z')) {
65 t += c;
66 }
67 }
68 return t;
69 }
70
71 function uniquify(text, preserve, caseSensitive, puncSensitive) {
72 var set = {}
73 var result = '';
74
75 text = text.replace('\r', '\n').replace('\t', ' ');
76
77 var lines = text.split('\n');
78
79 for (var l = 0; l < lines.length; l++) {
80 var words = lines[l].split(" ");
81
82 var textLine = '';
83 for (var i = 0; i < words.length; i++) {
84 var word = words[i];
85 if (word === '') {
86 continue;
87 }
88 var z = word;
89 if (!puncSensitive) {
90 z = stripPunctuation(z);
91 }
92 if (!caseSensitive) {
93 z = z.toUpperCase();
94 }
95 if (!set[z]) {
96 textLine += ' ' + word;
97 }
98 set[z] = true;
99 }
100
101 result += textLine.substring(1);
102
103 if (preserve === 'line_breaks') {
104 result += '\n';
105 } else if (preserve === 'paragraph_breaks') {
106 if (words.length === 1 && words[0] === '') {
107 result += '\n\n';
108 } else {
109 result += ' ';
110 }
111 } else {
112 result += ' ';
113 }
114 }
115
116 return result;
117 };