-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixJsonBible.mjs
More file actions
295 lines (242 loc) · 7.71 KB
/
fixJsonBible.mjs
File metadata and controls
295 lines (242 loc) · 7.71 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import fs from "fs";
import path from "path";
// ===== Configuration & Arguments =====
const args = process.argv.slice(2);
const FLAGS = {
DRY_RUN: args.includes("--dry-run"),
WRITE_LOG: args.includes("--log"),
HELP: args.includes("--help") || args.includes("-h")
};
// Paths
const INPUT_DIR = path.resolve("../Converted");
const OUTPUT_DIR = path.resolve("../Fixed");
const LOGS_DIR = path.resolve("./logs"); // Centralized logs folder
// ===== Help Menu =====
function showHelp(error) {
if (error) console.error(`❌ Error: ${error}\n`);
console.log(`
📖 JSON/FSB Bible Fixer for FreeShow
Usage:
node fixJsonBible.js [options]
Options:
--log Write a log file (in ./logs/)
--dry-run Analyze files without writing output
--help, -h Show this help message
Notes:
• Output is always compact (minified) JSON to save space.
• The script only outputs Bibles that actually need fixing.
`);
}
// ===== 🛡️ Argument Validation =====
const ALLOWED_FLAGS = ["--dry-run", "--log", "--help", "-h"];
for (const arg of args) {
// If it looks like a flag (starts with -) but isn't allowed
if (arg.startsWith("-") && !ALLOWED_FLAGS.includes(arg)) {
showHelp(`Unknown argument: "${arg}"`);
process.exit(1);
}
}
if (FLAGS.HELP) {
showHelp();
process.exit(0);
}
// ===== 📝 Logging System (DRY Principle) =====
const logLines = [];
const timestamp = () => new Date().toISOString().replace(/[:.]/g, "-");
const logFilePath = path.join(LOGS_DIR, `fix-log-${timestamp()}.txt`);
function writeLog(line) {
if (FLAGS.WRITE_LOG) logLines.push(line);
}
function logMessage(msg, level = "info") {
// Console output
switch (level) {
case "warn": console.warn(msg); break;
case "error": console.error(msg); break;
default: console.log(msg);
}
// File buffer
writeLog(msg);
}
function flushLogs() {
if (FLAGS.WRITE_LOG && logLines.length) {
if (!fs.existsSync(LOGS_DIR)) fs.mkdirSync(LOGS_DIR, { recursive: true });
fs.writeFileSync(logFilePath, logLines.join("\n") + "\n", "utf8");
console.log(`\n📝 Log written to ${logFilePath}`);
}
}
// ===== 📂 File Handling System =====
// Validate Input
if (!fs.existsSync(INPUT_DIR)) {
logMessage(`❌ Input folder not found: ${INPUT_DIR}`, "error");
logMessage(`👉 Please create "Converted" folder or check path.`, "warn");
process.exit(1);
}
// Prepare Output
if (!FLAGS.DRY_RUN && !fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
// Get Files
const inputFiles = fs.readdirSync(INPUT_DIR).filter(f => f.endsWith(".json") || f.endsWith(".fsb"));
if (!inputFiles.length) {
logMessage(`⚠️ No JSON or FSB files found in "${INPUT_DIR}"`, "warn");
process.exit(0);
}
/**
* Reads a file and determines if it is a standard JSON object
* or an FSB structure (Array: [ID, Data]).
*/
function readBibleFile(filePath) {
const content = fs.readFileSync(filePath, "utf8");
let raw;
try {
raw = JSON.parse(content);
} catch (e) {
throw new Error(`Invalid JSON syntax`);
}
// ROBUST DETECTION:
const isFsbStructure = Array.isArray(raw) &&
raw.length === 2 &&
typeof raw[0] === "string" &&
typeof raw[1] === "object" &&
raw[1] !== null;
if (isFsbStructure) {
return { type: "fsb", id: raw[0], data: raw[1] };
} else if (typeof raw === "object" && raw !== null && !Array.isArray(raw)) {
return { type: "json", id: null, data: raw };
} else {
throw new Error(`Unknown structure. Expected {object} or ["id", {object}].`);
}
}
/**
* Writes the file back to disk, preserving the original structure (JSON vs FSB).
* Always writes in COMPACT format.
*/
function writeBibleFile(filePath, bibleWrapper) {
let outputData;
// Reconstruct FSB array if necessary
if (bibleWrapper.type === "fsb") {
outputData = [bibleWrapper.id, bibleWrapper.data];
} else {
outputData = bibleWrapper.data;
}
// Always compact
const jsonString = JSON.stringify(outputData);
fs.writeFileSync(filePath, jsonString, "utf8");
}
// ===== 🧠 Logic: Bible Fixing =====
function processVerses(verses, context, verseLog) {
const result = [];
let lastVerse = null;
let expectedNumber = null;
let fixed = false;
for (const v of verses) {
if (expectedNumber === null) expectedNumber = v.number;
// 🔍 Missing verse numbers (Gaps)
if (v.number > expectedNumber && lastVerse) {
fixed = true;
lastVerse.endNumber = v.number - 1; // Extend previous verse to cover gap
verseLog.push({
type: "missing",
start: expectedNumber,
end: v.number - 1,
context
});
}
// 🔍 Empty verse text
if (!v.text || v.text.trim() === "") {
if (lastVerse) {
fixed = true;
lastVerse.endNumber = v.number; // Merge into previous verse
verseLog.push({
type: "empty",
start: v.number,
end: v.number,
context
});
}
} else {
result.push(v);
lastVerse = v;
}
expectedNumber = v.number + 1;
}
return { verses: result, fixed };
}
function logGroupedErrors(verseLog) {
const grouped = [];
// Group consecutive errors
for (const entry of verseLog) {
const last = grouped[grouped.length - 1];
if (last && last.type === entry.type && last.context === entry.context && last.end + 1 === entry.start) {
last.end = entry.end; // Extend range
} else {
grouped.push({ ...entry });
}
}
// Print errors
for (const g of grouped) {
const label = g.type === "empty" ? "Empty verse" : "Missing verses";
const range = g.start === g.end ? g.start : `${g.start}-${g.end}`;
logMessage(` 📌 ${label} ${range} at ${g.context}`);
}
}
// ===== 🚀 Main Execution =====
logMessage(`📂 Input: ${INPUT_DIR}`);
logMessage(`📂 Output: ${OUTPUT_DIR}`);
logMessage(FLAGS.DRY_RUN
? "🧪 DRY-RUN MODE: No files will be written.\n"
: "🛠 Fix mode enabled. Writing output files (Compact).\n");
let fixedCount = 0;
let invalidCount = 0;
for (const file of inputFiles) {
const filePath = path.join(INPUT_DIR, file);
let bibleWrapper;
// 1. Read
try {
bibleWrapper = readBibleFile(filePath);
} catch (err) {
invalidCount++;
logMessage(`❌ Skipped ${file}: ${err.message}`, "error");
continue;
}
// 2. Process
const bible = bibleWrapper.data; // Work on the inner data object
let bibleWasModified = false;
const fileLog = [];
for (const book of bible.books || []) {
for (const chapter of book.chapters || []) {
const context = `${book.name} ${chapter.number}`;
const result = processVerses(chapter.verses || [], context, fileLog);
if (result.fixed) {
chapter.verses = result.verses;
bibleWasModified = true;
}
}
}
// 3. Log & Write
if (bibleWasModified) {
fixedCount++;
const status = FLAGS.DRY_RUN ? "Needs fixing" : "Fixed";
logMessage(`📖 ${status}: ${file}`);
logGroupedErrors(fileLog);
if (!FLAGS.DRY_RUN) {
const outputName = `fixed_${file}`; // distinct prefix
const outputPath = path.join(OUTPUT_DIR, outputName);
writeBibleFile(outputPath, bibleWrapper);
}
logMessage(""); // Spacer
}
}
// ===== 🏁 Summary =====
const summary = `
Done.
📘 ${fixedCount} Bible(s) ${FLAGS.DRY_RUN ? "would need fixing" : "needed fixing"}
⚠️ ${invalidCount} file(s) had invalid JSON/FSB and were skipped.
🔎 Reminder: ${
FLAGS.DRY_RUN
? "These changes would affect verse ranges. Please double-check them against the original/reference Bible."
: "These changes affected verse ranges. Please double-check them against the original/reference Bible."
}
`;
logMessage(summary);
flushLogs();