From 25b97819b12b28dfceb71f15347854f4024c73b8 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 4 Feb 2018 20:03:00 +0200 Subject: [PATCH] Updated node optimization Abstracted into a utility function. --- scripts/util.js | 14 ++++++++++- scripts/web.js | 66 ++++++++++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/scripts/util.js b/scripts/util.js index 613e65ca3..fda5709a8 100644 --- a/scripts/util.js +++ b/scripts/util.js @@ -48,4 +48,16 @@ const readTags = () => { } return tagDbData; } -module.exports = {readSnippets, readTags}; +const optimizeNodes = (data, regexp, replacer) => { + let count = 0; + let output = data; + do { + output = output.replace(regexp, replacer); + count = 0; + while (regexp.exec(output) !== null) { + ++count; + } + } while (count > 0); + return output; +} +module.exports = {readSnippets, readTags, optimizeNodes}; diff --git a/scripts/web.js b/scripts/web.js index c3612f7f8..5ca861e22 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -124,39 +124,43 @@ try { // Add the ending static part output += `\n${endPart + '\n'}`; // Optimize punctuation nodes - let count = 0; - do { - const punctuationRegex = /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm; - output = output.replace(punctuationRegex, - (match, p1, p2, p3) => `${p1}${p2}${p3}` - ); - count = 0; - while (punctuationRegex.exec(output) !== null) { - ++count; - } - } while (count > 0); + output = util.optimizeNodes(output, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); // Optimize operator nodes - do { - const operatorRegex = /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm; - output = output.replace(operatorRegex, - (match, p1, p2, p3) => `${p1}${p2}${p3}` - ); - count = 0; - while (operatorRegex.exec(output) !== null) { - ++count; - } - } while (count > 0); + output = util.optimizeNodes(output, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); // Optimize keyword nodes - do { - const keyWordRegex = /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm; - output = output.replace(keyWordRegex, - (match, p1, p2, p3) => `${p1}${p2}${p3}` - ); - count = 0; - while (keyWordRegex.exec(output) !== null) { - ++count; - } - } while (count > 0); + output = util.optimizeNodes(output, /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `${p1}${p2}${p3}`); + // do { + // const punctuationRegex = /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm; + // output = output.replace(punctuationRegex, + // (match, p1, p2, p3) => `${p1}${p2}${p3}` + // ); + // count = 0; + // while (punctuationRegex.exec(output) !== null) { + // ++count; + // } + // } while (count > 0); + // // Optimize operator nodes + // do { + // const operatorRegex = /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm; + // output = output.replace(operatorRegex, + // (match, p1, p2, p3) => `${p1}${p2}${p3}` + // ); + // count = 0; + // while (operatorRegex.exec(output) !== null) { + // ++count; + // } + // } while (count > 0); + // // Optimize keyword nodes + // do { + // const keyWordRegex = /([^\0<]*?)<\/span>([\n\r\s]*)([^\0]*?)<\/span>/gm; + // output = output.replace(keyWordRegex, + // (match, p1, p2, p3) => `${p1}${p2}${p3}` + // ); + // count = 0; + // while (keyWordRegex.exec(output) !== null) { + // ++count; + // } + // } while (count > 0); // Minify output output = minify(output, { collapseBooleanAttributes: true,