Skip to content

Commit

Permalink
Reorganise variable processing (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-zeyu committed May 20, 2020
1 parent 92d3881 commit ea2ab6b
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 254 deletions.
4 changes: 2 additions & 2 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ class Site {
const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths];
const uniquePaths = _.uniq(filePathArray);
logger.info('Rebuilding affected source files');
this.variablePreprocessor.resetVariables();
this.variablePreprocessor.resetPageVariables();
return new Promise((resolve, reject) => {
this.regenerateAffectedPages(uniquePaths)
.then(() => fs.removeAsync(this.tempPath))
Expand All @@ -706,7 +706,7 @@ class Site {
const uniqueUrls = _.uniq(normalizedUrlArray);
uniqueUrls.forEach(normalizedUrl => logger.info(
`Building ${normalizedUrl} as some of its dependencies were changed since the last visit`));
this.variablePreprocessor.resetVariables();
this.variablePreprocessor.resetPageVariables();

/*
Lazy loading only builds the page being viewed, but the user may be quick enough
Expand Down
5 changes: 0 additions & 5 deletions src/lib/markbind/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ module.exports = {

BOILERPLATE_FOLDER_NAME: '_markbind/boilerplates',

/* Imported global variables will be assigned a namespace.
* A prefix is appended to reduce clashes with other variables in the page.
*/
IMPORTED_VARIABLE_PREFIX: '$__MARKBIND__',

// src/lib/markbind/src/utils.js
markdownFileExts: ['md', 'mbd', 'mbdf'],
};
13 changes: 4 additions & 9 deletions src/lib/markbind/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,16 @@ class Parser {
decodeEntities: true,
});

const outerContentRendered = this.variablePreprocessor.renderOuterVariables(content, file,
additionalVariables);
this.variablePreprocessor.extractInnerVariables(outerContentRendered, context.cwf)
.forEach(src => this.staticIncludeSrc.push(src));
const innerContentRendered = this.variablePreprocessor.renderInnerVariables(outerContentRendered,
file, context.cwf,
additionalVariables);
this.variablePreprocessor.extractPageVariables(file, content);
const renderedContent = this.variablePreprocessor.renderPage(file, content, {}, additionalVariables);

const fileExt = utils.getExt(file);
if (utils.isMarkdownFileExt(fileExt)) {
context.source = 'md';
parser.parseComplete(innerContentRendered.toString());
parser.parseComplete(renderedContent.toString());
} else if (fileExt === 'html') {
context.source = 'html';
parser.parseComplete(innerContentRendered);
parser.parseComplete(renderedContent);
} else {
const error = new Error(`Unsupported File Extension: '${fileExt}'`);
reject(error);
Expand Down
15 changes: 12 additions & 3 deletions src/lib/markbind/src/preprocessors/componentPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ function _preprocessInclude(node, context, config, parser) {
const {
renderedContent,
childContext,
includedSrces,
} = variablePreprocessor.renderIncludeFile(actualFilePath, element, context, filePath);
includedSrces.forEach(src => parser.staticIncludeSrc.push(src));

_deleteIncludeAttributes(element);

Expand Down Expand Up @@ -329,6 +327,16 @@ function _preprocessVariables() {
return utils.createEmptyNode();
}

function _preprocessImports(node, parser) {
if (node.attribs.from) {
parser.staticIncludeSrc.push({
from: node.attribs.cwf,
to: path.resolve(node.attribs.cwf, node.attribs.from),
});
}

return utils.createEmptyNode();
}

/*
* Body
Expand All @@ -355,8 +363,9 @@ function preProcessComponent(node, context, config, parser) {
case 'panel':
return _preProcessPanel(element, context, config, parser);
case 'variable':
case 'import':
return _preprocessVariables();
case 'import':
return _preprocessImports(node, parser);
case 'include':
return _preprocessInclude(element, context, config, parser);
case 'body':
Expand Down
Loading

0 comments on commit ea2ab6b

Please sign in to comment.