UNPKG

mush-format

Version:

A javascript library that minifies pretty formatted mushcode.

58 lines (52 loc) 2 kB
module.exports = (data, next) => { // remove comments const compressed = data.raw .replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, "") // remove comments .replace(/^#.*/gim, "") // remove unevaluated meta tags .split("\n") .filter(Boolean) .reduce((acc, curr) => { // if the first column isn't blank, it's a new line, else append it // to the line before. curr = curr.trimRight(); curr = curr.replace(/\t/g, " "); // replace tabs. O.o if (curr.match(/^[^\t\r\n\s]/)) curr = `\n${curr}`; curr = curr .replace(/\r/g, "") // remove returns .replace(/^\n-/g, "\n"); // replace dashes return (acc += curr); }, "") .replace(/\]\s+?\[/g, "][") // remove spaces between braackets. .replace(/\s+?=\s+?|=\s+?|\s+?=/g, "=") // remove spaces around '=' .replace(/\s?%([rst])\s?/gi, "%$1") // replace spaces around substitutions. .replace(/\t|\s\s+/g, " ") // Remove extra spaces (and tabs!) .replace(/\)\s|\)\t|\s+\)/g, ")") .replace(/([\S])\)/g, "$1 )") .replace(/\)\s\)/g, "))"); // Remove spaces on ending parens. // generate headers if (data.headers.length > 0) { data.output += "@@ ========================================================\n"; data.headers.forEach(header => { data.output += `@@ ${header.name.toUpperCase().padEnd(10)} ${ header.value }\n`; }); data.output += "@@ ========================================================\n"; } data.output += compressed; // generate footers if (data.footers.length > 0) { data.output += "@@ ========================================================\n"; data.footers.forEach(footer => { data.output += `@@ ${footer.name.toUpperCase().padEnd(10)} ${ footer.value }\n`; }); data.output += "@@ ========================================================\n"; } next(null, data); };