-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_v0.3.2.html
More file actions
186 lines (175 loc) · 6.32 KB
/
split_v0.3.2.html
File metadata and controls
186 lines (175 loc) · 6.32 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text Splitter Tool v0.3.2</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.6.0/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
padding: 20px;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
}
.container {
display: flex;
justify-content: space-between;
width: 90%;
margin-top: 20px;
}
.column {
flex: 1;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
margin: 0 10px;
}
h1 {
color: #0056b3;
}
button {
background-color: #0056b3;
color: white;
border: none;
padding: 10px 20px;
margin: 10px 0;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #003d80;
}
input, select {
padding: 10px;
margin: 5px 0;
border-radius: 5px;
border: 1px solid #ccc;
width: 100%;
}
label {
font-weight: bold;
}
.action-buttons {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Text Document Splitter v0.3.2</h1>
<div class="container">
<div class="column">
<h2>Input</h2>
<label for="fileInput">Text File to Split (must be .txt):</label>
<input type="file" id="fileInput" accept=".txt">
<select id="tokenSize">
<option value="1000">1000 tokens</option>
<option value="2000">2000 tokens</option>
<option value="4000">4000 tokens</option>
<option value="8000">8000 tokens</option>
<option value="16000">16000 tokens</option>
</select>
</div>
<div class="column">
<h2>Output</h2>
<label for="fileNamePrefix">Output Prefix Name:</label>
<div class="flex-row">
<input type="text" id="fileNamePrefix" placeholder="Output Prefix Name">
<span class="suffix">_part01.txt</span>
</div>
</div>
</div>
<div class="action-buttons">
<button onclick="splitFile()">Split File</button>
<button onclick="location.reload();">Reset</button>
</div>
<div id="downloadLinks"></div>
<button id="downloadAllButton" style="display:none;">Download All</button>
<script>
function splitFile() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const fileNamePrefix = document.getElementById('fileNamePrefix').value || file.name.split('.')[0];
if (!file) {
alert('Please select a file.');
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const text = event.target.result;
const tokenSize = parseInt(document.getElementById('tokenSize').value);
const tokens = preserveTokenize(text);
const parts = chunkTokens(tokens, tokenSize);
const downloadLinks = document.getElementById('downloadLinks');
downloadLinks.innerHTML = ''; // Clear previous links
const zip = new JSZip();
parts.forEach((part, index) => {
const partName = `${fileNamePrefix}_part${String(index + 1).padStart(2, '0')}.txt`;
const blob = new Blob([part], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.textContent = `Download ${partName}`;
a.download = partName;
a.style.display = 'block';
downloadLinks.appendChild(a);
zip.file(partName, part);
});
// Add "Download All" button
document.getElementById('downloadAllButton').style.display = 'inline';
document.getElementById('downloadAllButton').onclick = function() {
zip.generateAsync({type:"blob"}).then(function(content) {
saveAs(content, `${fileNamePrefix}_all_parts.zip`);
});
}
};
reader.readAsText(file);
}
function preserveTokenize(text) {
let tokens = [];
let currentToken = '';
Array.from(text).forEach(char => {
if (char.match(/\s/)) {
if (currentToken) {
tokens.push(currentToken);
currentToken = '';
}
tokens.push(char);
} else {
currentToken += char;
}
});
if (currentToken) {
tokens.push(currentToken);
}
return tokens;
}
function chunkTokens(tokens, tokenSize) {
let chunks = [];
let currentChunk = '';
let count = 0;
tokens.forEach(token => {
if (count + token.length > tokenSize && (token === ' ' || token === '\n')) {
chunks.push(currentChunk);
currentChunk = '';
count = 0;
}
currentChunk += token;
count++;
});
if (currentChunk) {
chunks.push(currentChunk);
}
return chunks;
}
document.getElementById('fileInput').addEventListener('change', function() {
var fileName = this.files[0].name.split('.')[0];
document.getElementById('fileNamePrefix').placeholder = fileName;
});
</script>
</body>
</html>