Skip to content

Commit

Permalink
Refactor page level variable functions (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-zeyu committed Apr 10, 2020
1 parent 5832670 commit d7da85d
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 139 deletions.
3 changes: 2 additions & 1 deletion src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Site {
this.siteConfigPath = siteConfigPath;

// Site wide variable preprocessor
this.variablePreprocessor = new VariablePreprocessor();
this.variablePreprocessor = undefined;

// Lazy reload properties
this.onePagePath = onePagePath;
Expand Down Expand Up @@ -515,6 +515,7 @@ class Site {
.map(x => path.resolve(this.rootPath, x));

this.baseUrlMap = new Set(candidates.map(candidate => path.dirname(candidate)));
this.variablePreprocessor = new VariablePreprocessor(this.rootPath, this.baseUrlMap);

return Promise.resolve();
}
Expand Down
75 changes: 22 additions & 53 deletions src/lib/markbind/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,31 +246,21 @@ class Parser {
reject(err);
return;
}
const parentSitePath = urlUtils.getParentSiteAbsolutePath(file, config.rootPath, config.baseUrlMap);
const userDefinedVariables = config.variablePreprocessor.userDefinedVariablesMap[parentSitePath];
const pageVariables = config.variablePreprocessor.extractPageVariables(file, data,
userDefinedVariables, {});
let fileContent = njUtil.renderRaw(data, {
...pageVariables,
...userDefinedVariables,
}, {
path: actualFilePath,
});
config.variablePreprocessor
.extractInnerVariables(fileContent, context, config)
const { variablePreprocessor } = config;

const outerFileContentRendered = variablePreprocessor.renderOuterVariables(data, file);
variablePreprocessor.extractInnerVariables(outerFileContentRendered, context.cwf)
.forEach(src => this.staticIncludeSrc.push(src));
const innerFileContentRendered = variablePreprocessor.renderInnerVariables(outerFileContentRendered,
file, context.cwf);

const innerVariables = config.variablePreprocessor.getImportedVariableMap(context.cwf);
fileContent = njUtil.renderRaw(fileContent, {
...userDefinedVariables, ...innerVariables,
});
const fileExt = utils.getExt(file);
if (utils.isMarkdownFileExt(fileExt)) {
context.source = 'md';
parser.parseComplete(fileContent.toString());
parser.parseComplete(innerFileContentRendered.toString());
} else if (fileExt === 'html') {
context.source = 'html';
parser.parseComplete(fileContent);
parser.parseComplete(innerFileContentRendered);
} else {
const error = new Error(`Unsupported File Extension: '${fileExt}'`);
reject(error);
Expand All @@ -284,14 +274,6 @@ class Parser {
context.cwf = config.cwf || file; // current working file

return new Promise((resolve, reject) => {
let actualFilePath = file;
if (!utils.fileExists(file)) {
const boilerplateFilePath = urlUtils.calculateBoilerplateFilePath(path.basename(file), file, config);
if (utils.fileExists(boilerplateFilePath)) {
actualFilePath = boilerplateFilePath;
}
}

const currentContext = context;
currentContext.mode = 'include';
currentContext.callStack = [];
Expand All @@ -306,7 +288,7 @@ class Parser {
try {
processed = componentPreprocessor.preProcessComponent(d, currentContext, config, this);
} catch (err) {
err.message += `\nError while preprocessing '${actualFilePath}'`;
err.message += `\nError while preprocessing '${file}'`;
this._onError(err);
processed = utils.createErrorNode(d, err);
}
Expand All @@ -315,41 +297,28 @@ class Parser {
resolve(cheerio.html(nodes));
});

const { additionalVariables, variablePreprocessor } = config;

const outerFileContentRendered = variablePreprocessor.renderOuterVariables(pageData, file,
additionalVariables);
variablePreprocessor.extractInnerVariables(outerFileContentRendered, currentContext.cwf)
.forEach(src => this.staticIncludeSrc.push(src));
const innerFileContentRendered = variablePreprocessor.renderInnerVariables(outerFileContentRendered,
file, currentContext.cwf,
additionalVariables);

const parser = new htmlparser.Parser(handler, {
xmlMode: true,
decodeEntities: true,
});

const parentSitePath = urlUtils.getParentSiteAbsolutePath(file, config.rootPath, config.baseUrlMap);
const userDefinedVariables = config.variablePreprocessor.userDefinedVariablesMap[parentSitePath];
const { additionalVariables } = config;
const pageVariables = config.variablePreprocessor.extractPageVariables(actualFilePath, pageData,
userDefinedVariables, {});

let fileContent = njUtil.renderRaw(pageData, {
...pageVariables,
...userDefinedVariables,
...additionalVariables,
}, {
path: actualFilePath,
});
config.variablePreprocessor
.extractInnerVariables(fileContent, currentContext, config)
.forEach(src => this.staticIncludeSrc.push(src));

const innerVariables = config.variablePreprocessor.getImportedVariableMap(currentContext.cwf);
fileContent = njUtil.renderRaw(fileContent, {
...userDefinedVariables,
...additionalVariables,
...innerVariables,
});
const fileExt = utils.getExt(actualFilePath);
const fileExt = utils.getExt(file);
if (utils.isMarkdownFileExt(fileExt)) {
currentContext.source = 'md';
parser.parseComplete(fileContent.toString());
parser.parseComplete(innerFileContentRendered.toString());
} else if (fileExt === 'html') {
currentContext.source = 'html';
parser.parseComplete(fileContent);
parser.parseComplete(innerFileContentRendered);
} else {
const error = new Error(`Unsupported File Extension: '${fileExt}'`);
reject(error);
Expand Down
24 changes: 10 additions & 14 deletions src/lib/markbind/src/preprocessors/componentPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const CyclicReferenceError = require('../handlers/cyclicReferenceError.js');
const md = require('../lib/markdown-it');
const utils = require('../utils');
const urlUtils = require('../utils/urls');
const njUtil = require('../utils/nunjuckUtils');

const _ = {};
_.has = require('lodash/has');
Expand Down Expand Up @@ -278,18 +277,12 @@ function _preprocessInclude(node, context, config, parser) {

const isIncludeSrcMd = _isHtmlIncludingMarkdown(element, context, filePath);

const { content, childContext, userDefinedVariables }
= config.variablePreprocessor.renderIncludeFile(actualFilePath, element, context, config, filePath);
childContext.source = isIncludeSrcMd ? 'md' : 'html';
childContext.callStack.push(context.cwf);
config.variablePreprocessor
.extractInnerVariablesIfNotProcessed(content, childContext, config, filePath)
const { variablePreprocessor } = config;
const { content, childContext } = variablePreprocessor.renderIncludeFile(actualFilePath, element,
context, filePath);
variablePreprocessor.extractInnerVariablesIfNotProcessed(content, childContext.cwf, filePath)
.forEach(src => parser.staticIncludeSrc.push(src));

const innerVariables = config.variablePreprocessor.getImportedVariableMap(filePath);
const fileContent = njUtil.renderRaw(content, {
...userDefinedVariables, ...innerVariables,
});
const innerContentRendered = variablePreprocessor.renderInnerVariables(content, filePath, filePath);

_deleteIncludeAttributes(element);

Expand All @@ -300,7 +293,7 @@ function _preprocessInclude(node, context, config, parser) {

if (hash) {
// Keep scripts in the fileContent
const src = cheerio.parseHTML(fileContent, true);
const src = cheerio.parseHTML(innerContentRendered, true);
const $ = cheerio.load(src);
const hashContent = $(hash).html();

Expand All @@ -321,7 +314,7 @@ function _preprocessInclude(node, context, config, parser) {
// optional includes of segments have now been handled, so delete the attribute
if (isOptional) delete element.attribs.optional;
} else {
actualContent = (fileContent && isTrim) ? fileContent.trim() : fileContent;
actualContent = (innerContentRendered && isTrim) ? innerContentRendered.trim() : innerContentRendered;
}

if (isIncludeSrcMd) {
Expand All @@ -335,6 +328,9 @@ function _preprocessInclude(node, context, config, parser) {
element.children = cheerio.parseHTML(childrenHtml, true);

if (element.children && element.children.length > 0) {
childContext.source = isIncludeSrcMd ? 'md' : 'html';
childContext.callStack.push(context.cwf);

if (childContext.callStack.length > CyclicReferenceError.MAX_RECURSIVE_DEPTH) {
const error = new CyclicReferenceError(childContext.callStack);
parser._onError(error);
Expand Down
Loading

0 comments on commit d7da85d

Please sign in to comment.