Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ Parser.prototype._trimNodes = function (node) {
}
};

Parser.prototype.includeFile = function (file, config, cb) {
cb = cb || function () {}; // create empty callback

Parser.prototype.includeFile = function (file, config) {
let context = {};
context.cwf = file; // current working file
context.mode = 'include';
Expand All @@ -287,14 +285,12 @@ Parser.prototype.includeFile = function (file, config, cb) {
let handler = new htmlparser.DomHandler((error, dom) => {
if (error) {
reject(error);
cb(error);
return;
}
let nodes = dom.map(d => {
return this._preprocess(d, context, config);
});
resolve(cheerio.html(nodes));
cb(null, cheerio.html(nodes));
});

let parser = new htmlparser.Parser(handler, {
Expand All @@ -306,7 +302,6 @@ Parser.prototype.includeFile = function (file, config, cb) {
fs.readFile(file, (err, data) => {
if (err) {
reject(err);
cb(err);
return;
}
let fileExt = utils.getExtName(file);
Expand All @@ -318,24 +313,20 @@ Parser.prototype.includeFile = function (file, config, cb) {
parser.parseComplete(data);
} else {
let err = new Error(`Unsupported File Extension: '${fileExt}'`);
cb(err);
reject(err);
}
});
});
};

Parser.prototype.renderFile = function (file, cb) {
cb = cb || function () {}; // create empty callback

Parser.prototype.renderFile = function (file) {
let context = {};
context.cwf = file; // current working file
context.mode = 'render';
return new Promise((resolve, reject) => {
let handler = new htmlparser.DomHandler((error, dom) => {
if (error) {
reject(error);
cb(error);
return;
}
let nodes = dom.map(d => {
Expand All @@ -346,7 +337,6 @@ Parser.prototype.renderFile = function (file, cb) {
});
cheerio.prototype.options.xmlMode = false;
resolve(cheerio.html(nodes));
cb(null, cheerio.html(nodes));
cheerio.prototype.options.xmlMode = true;
});

Expand All @@ -359,7 +349,6 @@ Parser.prototype.renderFile = function (file, cb) {
fs.readFile(file, (err, data) => {
if (err) {
reject(err);
cb(err);
return;
}
let inputData = data;
Expand All @@ -373,27 +362,24 @@ Parser.prototype.renderFile = function (file, cb) {
parser.parseComplete(data);
} else {
let err = new Error(`Unsupported File Extension: '${fileExt}'`);
cb(err);
reject(err);
}
});
});
};

Parser.prototype.resolveBaseUrl = function (pageData, config, cb) {
Parser.prototype.resolveBaseUrl = function (pageData, config) {
let {baseUrlMap, rootPath, isDynamic} = config;
this.baseUrlMap = baseUrlMap;
this.rootPath = rootPath;
this.isDynamic = isDynamic || false;
if (this.isDynamic) {
this.dynamicSource = config.dynamicSource;
}
cb = cb || function () {}; // create empty callback
return new Promise((resolve, reject) => {
let handler = new htmlparser.DomHandler((error, dom) => {
if (error) {
reject(error);
cb(error);
return;
}
dom.forEach(d => {
Expand All @@ -406,7 +392,6 @@ Parser.prototype.resolveBaseUrl = function (pageData, config, cb) {
});
cheerio.prototype.options.xmlMode = false;
resolve(cheerio.html(dom));
cb(null, cheerio.html(dom));
cheerio.prototype.options.xmlMode = true;
});

Expand Down