From 6b611254972e43afc88222430db35f04f5104da8 Mon Sep 17 00:00:00 2001 From: rachael Date: Thu, 25 Jan 2018 14:16:10 +0800 Subject: [PATCH 1/5] Search for file in site/common directory if not found locally --- lib/parser.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 89e0fca6bb..3bcd3643ae 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -74,10 +74,15 @@ Parser.prototype._preprocess = function (element, context, config) { self._fileCache[filePath] = self._fileCache[filePath] ? self._fileCache[filePath] : fs.readFileSync(filePath, 'utf8'); } catch (e) { - // Read file fail - this._onError(e); - element.children = cheerio.parseHTML(utils.createErrorElement(e), true); - return element; + try { + filePath = path.resolve(path.join(process.cwd(), 'common'), includeSrcPath); + self._fileCache[filePath] = fs.readFileSync(filePath, 'utf8'); + } catch (e) { + // Read file fail + this._onError(e); + element.children = cheerio.parseHTML(utils.createErrorElement(e), true); + return element; + } } From a3103425ac6c1d1e880161dcc526938f110c1c39 Mon Sep 17 00:00:00 2001 From: rachael Date: Thu, 25 Jan 2018 14:21:24 +0800 Subject: [PATCH 2/5] Adjust indentation --- lib/parser.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 3bcd3643ae..166fedf9fd 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -78,11 +78,11 @@ Parser.prototype._preprocess = function (element, context, config) { filePath = path.resolve(path.join(process.cwd(), 'common'), includeSrcPath); self._fileCache[filePath] = fs.readFileSync(filePath, 'utf8'); } catch (e) { - // Read file fail - this._onError(e); - element.children = cheerio.parseHTML(utils.createErrorElement(e), true); - return element; - } + // Read file fail + this._onError(e); + element.children = cheerio.parseHTML(utils.createErrorElement(e), true); + return element; + } } From 96f65c59c3323ed1211ac7bc62f629d062d5eb57 Mon Sep 17 00:00:00 2001 From: rachael Date: Fri, 26 Jan 2018 17:06:36 +0800 Subject: [PATCH 3/5] Update boilerplate directory name --- lib/parser.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/parser.js b/lib/parser.js index 166fedf9fd..c420414302 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -19,6 +19,8 @@ const nunjucks = require('nunjucks'); const ATTRIB_INCLUDE_PATH = 'include-path'; const ATTRIB_CWF = 'cwf'; + +const BOILERPLATE_DIRECTORY_NAME = '_boilerplates'; /* * Utils */ @@ -74,8 +76,8 @@ Parser.prototype._preprocess = function (element, context, config) { self._fileCache[filePath] = self._fileCache[filePath] ? self._fileCache[filePath] : fs.readFileSync(filePath, 'utf8'); } catch (e) { + filePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath); try { - filePath = path.resolve(path.join(process.cwd(), 'common'), includeSrcPath); self._fileCache[filePath] = fs.readFileSync(filePath, 'utf8'); } catch (e) { // Read file fail From d5c3b5a4bf0cc46cb875b90fc0433be2f582bdfb Mon Sep 17 00:00:00 2001 From: rachael Date: Fri, 26 Jan 2018 21:02:31 +0800 Subject: [PATCH 4/5] Check for boilerplate file path for dynamic panels --- lib/parser.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index c420414302..ebdcbe4681 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -53,6 +53,11 @@ Parser.prototype._preprocess = function (element, context, config) { element.name = isInline ? 'span' : 'div'; element.attribs[ATTRIB_INCLUDE_PATH] = filePath; + let boilerplateFilePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath); + if (!isUrl && !fs.existsSync(filePath) && fs.existsSync(boilerplateFilePath)) { + filePath = boilerplateFilePath; + } + if (isDynamic) { element.name = 'dynamic-panel'; element.attribs.src = filePath; @@ -144,6 +149,10 @@ Parser.prototype._preprocess = function (element, context, config) { element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute } filePath = path.resolve(path.dirname(context.cwf), includeSrcPath); // updated path (no hash) + let boilerplateFilePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath); + if (!fs.existsSync(filePath) && fs.existsSync(boilerplateFilePath)) { + filePath = boilerplateFilePath; + } } element.attribs.src = filePath; this.dynamicIncludeSrc.push({from: context.cwf, to: filePath}); @@ -160,6 +169,10 @@ Parser.prototype._preprocess = function (element, context, config) { element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute } filePath = path.resolve(path.dirname(context.cwf), includeSrcPath); // updated path (no hash) + let boilerplateFilePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath); + if (!fs.existsSync(filePath) && fs.existsSync(boilerplateFilePath)) { + filePath = boilerplateFilePath; + } } element.attribs.src = filePath; this.dynamicIncludeSrc.push({from: context.cwf, to: filePath}); @@ -202,7 +215,7 @@ Parser.prototype._parse = function (element, context) { element.attribs.isOpen = hasIsOpen ? hasIsOpen : false; try { if (fs.statSync(element.attribs.src).isFile()) { - let resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), element.attribs.src))); + let resultDir = path.dirname(path.relative('{{hostBaseUrl}}', element.attribs.src)); element.attribs.src = path.join(resultDir, utils.setExtension(path.basename(element.attribs.src), '._include_.html')); if (element.attribs.fragment) { element.attribs.src = `${element.attribs.src}#${element.attribs.fragment}`; // add hash back to path @@ -222,7 +235,7 @@ Parser.prototype._parse = function (element, context) { } try { if (fs.statSync(element.attribs.src).isFile()) { - let resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), element.attribs.src))); + let resultDir = path.dirname(path.relative('{{hostBaseUrl}}', element.attribs.src)); element.attribs.src = path.join(resultDir, utils.setExtension(path.basename(element.attribs.src), '._include_.html')); if (element.attribs.fragment) { element.attribs.src = `${element.attribs.src}#${element.attribs.fragment}`; // add hash back to path @@ -239,7 +252,7 @@ Parser.prototype._parse = function (element, context) { } try { if (fs.statSync(element.attribs.src).isFile()) { - let resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), element.attribs.src))); + let resultDir = path.dirname(path.relative('{{hostBaseUrl}}', element.attribs.src)); element.attribs.src = path.join(resultDir, utils.setExtension(path.basename(element.attribs.src), '._include_.html')); if (element.attribs.fragment) { element.attribs.src = `${element.attribs.src}#${element.attribs.fragment}`; // add hash back to path From bf2d583b0b694ae646f003dee2daf6f1f52b5238 Mon Sep 17 00:00:00 2001 From: rachael Date: Fri, 26 Jan 2018 21:51:29 +0800 Subject: [PATCH 5/5] Remove nested try catch since filePath has been checked/replaced --- lib/parser.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index ebdcbe4681..068a84804c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -81,15 +81,10 @@ Parser.prototype._preprocess = function (element, context, config) { self._fileCache[filePath] = self._fileCache[filePath] ? self._fileCache[filePath] : fs.readFileSync(filePath, 'utf8'); } catch (e) { - filePath = path.resolve(path.join(process.cwd(), BOILERPLATE_DIRECTORY_NAME), includeSrcPath); - try { - self._fileCache[filePath] = fs.readFileSync(filePath, 'utf8'); - } catch (e) { - // Read file fail - this._onError(e); - element.children = cheerio.parseHTML(utils.createErrorElement(e), true); - return element; - } + // Read file fail + this._onError(e); + element.children = cheerio.parseHTML(utils.createErrorElement(e), true); + return element; }