35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
function parseEntriesDb(dbFilePath) {
|
|
const raw = fs.readFileSync(dbFilePath, 'utf-8');
|
|
const entries = raw.split('ENTRY=');
|
|
const parsed = [];
|
|
for (const e of entries) {
|
|
if (!e.trim()) continue;
|
|
const lines = e.split('\n');
|
|
let data = {}, body = '', foundBody = false;
|
|
for (const line of lines) {
|
|
if (line.startsWith('body=')) {
|
|
foundBody = true;
|
|
body += line.substring(5) + '\n';
|
|
} else if (foundBody && line.trim() !== 'ENDENTRY') {
|
|
body += line + '\n';
|
|
} else if (line.trim() === 'ENDENTRY') {
|
|
foundBody = false;
|
|
} else if (!foundBody && line.includes('=')) {
|
|
const idx = line.indexOf('=');
|
|
let key = line.substring(0, idx).trim();
|
|
let val = line.substring(idx+1).trim();
|
|
data[key] = val;
|
|
}
|
|
}
|
|
parsed.push({
|
|
...data,
|
|
body: body.trim(),
|
|
});
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
module.exports = { parseEntriesDb }; |