-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
231 lines (207 loc) · 8.71 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
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
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>SoftProjector -> Holyrics</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css">
</head>
<body translate="no">
<div class="container">
<div class="row">
<div class="col-12 pt-3">
<h1>
SoftProjector -> Holyrics
</h1>
<details class="mb-3 pt-3">
<summary>Инструкция</summary>
<p>
Экспортируй песенник в файл. Затем загрузи .sps-файл сюда.
</p>
<p>
Полученый архив распакуй и импортируй через пункт
<i>Песни</i> ->
<i>Импортировать</i> ->
<i>Другое</i> ->
<i>TXT File (UTF-8)</i>
</p>
<p>
<img src="screenshot_holyrics-import.png" class="mw-100 rounded shadow-sm">
</p>
</details>
<p class="alert alert-secondary">
Файл обрабатывается локально в браузере. Никакие данные не отправляются на сервер.
</p>
<input type="file" accept=".sps" class="form-control mb-3 js-file">
<div class="js-loader d-none">
<span class="spinner-border"></span>
</div>
<div class="js-process d-none">
<p>
Найдено <b class="js-processed-count">0</b> песен.
</p>
<p>
<a class="btn btn-primary w-100 js-download">
Скачать архив
</a>
</p>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
const fileInput = document.querySelector(".js-file");
const loaderContainer = document.querySelector(".js-loader");
const processContainer = document.querySelector(".js-process");
const countContainer = document.querySelector(".js-processed-count");
const downloadButton = document.querySelector(".js-download");
const blockKeywords = ["Verse", "Verse", "Куплет", "Strophe", "Verš", "Chorus", "Chorus", "Sbor", "Refrain", "Refrain", "Припев", "Приспів", "Refrén", "Slide", "Insert", "Intro", "Ending", "Слайд", "Вставка", "Вступление", "Окончание", "Закінчення", "Dia", "Einfügung", "Einleitung", "Ende", "Snímek", "Vložka", "Úvod", "Závěr", "Announce", "Slide", "Объявление", "Оголошення", "Ankündigung", "Oznámení"];
const getFileContentAsString = (file) =>
new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsText(file);
});
const getFileContentAsUint = (file) =>
new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(new Uint8Array(reader.result));
reader.readAsArrayBuffer(file);
});
const sanitizeAndTransformSongText = text => {
return text.split("\n")
.map((row) => row.trim())
.join("\n")
.replace(/\n +/g, "\n")
.replace(new RegExp(`(^|\n)((${blockKeywords.join("|")})[^\n]*)`, "ig"), "$1##($2)")
.replace(/\n{2,}/g, "\n\n")
.replace(/^\n+/g, "")
.replace(/ +/g, " ")
.replace(/ +/g, " ")
.replace(/^ */g, "")
.replace(/\n */g, "\n")
.replace(/ *\n/g, "\n")
.replace(/^d+. */g, "")
.replace(/\nd+. */g, "\n");
};
const handleFileSelect = async () => {
const file = fileInput.files[0];
if (!file) {
return;
}
loaderContainer.classList.remove("d-none");
processContainer.classList.add("d-none");
const fileContent = await getFileContentAsString(file);
let songs = [];
if (fileContent.indexOf("SQLite format 3") === 0) {
// sqlite backup or database
const fileBytes = await getFileContentAsUint(file);
songs = await parseSQLiteFile(fileBytes);
} else if (fileContent.indexOf("##") === 0) {
songs = await parsePlaintextFile(fileContent);
// softprojector v1 text backup
} else {
alert("Не удалось распознать файл");
loaderContainer.classList.add("d-none");
}
countContainer.innerText = songs.length;
if (songs.length) {
const zip = new JSZip();
for (const song of songs) {
const firstRow = (
song.text
.split("\n")
.filter((row) => !row.includes("#") && row.length > 5)
.shift() || ""
).replace(/[.,!?-]$/g, "").trim();
const useFirstRow = firstRow && firstRow.toLowerCase().trim() !== song.title.toLowerCase().trim();
const fileName = `${song.songbook}/${song.title}${useFirstRow ? ` (${firstRow})` : ""}.txt`;
zip.file(fileName, song.text);
}
const zipBlob = await zip.generateAsync({ type: "blob" });
downloadButton.classList.remove("disabled");
downloadButton.href = URL.createObjectURL(zipBlob);
downloadButton.download = `${file.name.replace(/\.[a-z]+$/i, "").replace(/\s/ig, "_")}_SoftProjector2Holyrics_${Date.now()}.zip`;
} else {
downloadButton.classList.add("disabled");
}
processContainer.classList.remove("d-none");
loaderContainer.classList.add("d-none");
};
fileInput.addEventListener("change", handleFileSelect);
const parseSQLiteFile = async fileContentBytes => {
const SQL = await initSqlJs({
locateFile: filename => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/${filename}`,
});
const db = new SQL.Database(fileContentBytes);
const beautifyDatabaseResult = ({ columns, values }) => {
return values.map(value => Object.fromEntries(columns.map((key, index) => [key, value[index]])));
};
const query = query => beautifyDatabaseResult(db.exec(query)[0]);
const tables = query(`SELECT name
FROM sqlite_master
WHERE type = 'table';`)
.map(row => row.name);
if (tables.includes("SongBook")) {
// Table has 'SongBook' table. This is SoftProjector v2 export file
return query(`SELECT "Songs".*, "SongBook"."title" as "songbook_name"
FROM "Songs"
LEFT JOIN "SongBook" on "SongBook"."_rowid_" = 1
ORDER BY "_rowid_"`)
.map(song => {
const text = sanitizeAndTransformSongText(song.song_text || "");
return {
number: song.number || null,
title: song.title,
songbook: song.songbook_name.trim() || null,
key: song.tune || null,
wordsBy: song.words || null,
musicBy: song.music || null,
notes: song.notes || null,
text: text,
};
});
}
// const songbookNamesById = Object.fromEntries(
// query(`SELECT "_rowid_", *
// FROM "${tables.includes("SongBook") ? "SongBook" : "Songbooks"}"
// ORDER BY "_rowid_"`)
// .map(songbook => [songbook.id, songbook.name]));
// if (tables.includes("SongLink")) {
// console.log("Table has 'SongLink' table. This is SoftProjector v1 database");
// query(`SELECT "Songs".*, "Songbooks"."name" as "songbook_name", "SongLink"."song_number" as "song_number"
// FROM "Songs"
// LEFT JOIN "SongLink" on "SongLink"."song_id" = "Songs"."id"
// LEFT JOIN "Songbooks" on "SongLink"."songbook_id" = "Songbooks"."id"
// ORDER BY "_rowid_"`)
// .forEach(song => {
// songsToBeSaved.push(song);
// });
// } else
return [];
};
const parsePlaintextFile = async fileContentString => {
const fileRows = fileContentString.split("\n");
fileRows.shift(); // songbook number
const songbook = fileRows.shift().substring(2);
fileRows.shift(); // songbook description
return fileRows.map(row => {
const [number, title, _categoryId, key, wordsBy, musicBy, textCompressed] = row.split("#$#");
const textUncompressed = textCompressed.replace(/@%/ig, "\n").replace(/@\$/ig, "\n\n");
const text = sanitizeAndTransformSongText(textUncompressed || "");
return {
number: number || null,
title: title,
songbook: songbook || null,
key: key || null,
wordsBy: wordsBy || null,
musicBy: musicBy || null,
notes: null,
text: text,
};
}).filter(Boolean);
};
</script>
</body>
</html>