UNPKG

@parsify/converter

Version:

Parsify helper for converting files created by alternative software

63 lines (62 loc) 2.79 kB
import fs from 'node:fs/promises'; import xml2js from 'xml2js'; const parser = new xml2js.Parser(); const results = []; const comment = '# Converted by Parsify Converter (beta) — expect some syntax issues.'; const convert = async (path, format) => { const data = await fs.readFile(path, 'utf8'); switch (format) { case 'soulver': parser.parseString(data, async (_, result) => { for (const raw of result.data.lines) { raw.line.map((line, index) => { let full = line.expression[0]; if (Object.prototype.hasOwnProperty.call(line, 'answer')) { const matches = line.expression[0].match(/\(___rel_line\(-(\d)\)\)/g); if (matches?.length > 0) { for (const _match of matches) { const occurrences = _match.match(/\(___rel_line\(-(\d)\)\)/); if (occurrences?.length === 2) { full = full.replace(_match, raw.line[index - Number(occurrences[1])].answer[0]); } else { results.push(line.expression[0]); } } results.push(full); full = line.expression[0]; } else { results.push(line.expression[0]); } } else if (line.expression[0] === '') { results.push(''); } else { results.push(`# ${line.expression[0]}`); } }); } await fs.writeFile(path.replace(`.${format}`, '.txt'), `$${comment}\n{results.join('\n').replace(/×/g, '*')}`); }); break; case 'numi': let modified = ''; for (const line of data.split('\n')) { if (!line.startsWith('#') && !line.startsWith('// ') && line.includes('=')) { modified += `${line.replace(/=.+/g, '').trim()}\n`; } else { modified += `${line}\n`; } } await fs.writeFile(path.replace(`.${format}`, '.txt'), `${comment}\n${modified}`); break; default: throw new Error('Format not supported'); } }; export default convert;