Skip to content

Commit 95cf85a

Browse files
committed
Move receiving file processing function to separate function
1 parent 58839e0 commit 95cf85a

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

lib/load.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,38 @@ var Loader = function (data) {
5656
return (!dirObj) ? '' : dirObj.directory;
5757
}
5858

59+
function getFileProcessingFunction(url, filename) {
60+
var ext = path.extname(filename);
61+
var processingFunction;
62+
63+
switch (ext) {
64+
case '.css':
65+
processingFunction = loadCssSources.bind(null, url, filename);
66+
break;
67+
case '.html':
68+
processingFunction = loadHtml.bind(null, url, filename);
69+
break;
70+
default:
71+
processingFunction = function () {
72+
return arguments[0];
73+
};
74+
break;
75+
}
76+
return processingFunction;
77+
}
78+
5979
function loadFromUrl(url, desiredFilename) {
6080
var localFilename = getLoadedFilename(url);
61-
var ext, fileProcessingFunction;
81+
var fileProcessingFunction;
6282

6383
if (!localFilename) {
84+
// Get filename
6485
localFilename = getFilename(desiredFilename || path.basename(url));
65-
ext = path.extname(localFilename);
66-
67-
switch (ext) {
68-
case '.css':
69-
fileProcessingFunction = loadCssSources.bind(null, url, localFilename);
70-
break;
71-
case '.html':
72-
fileProcessingFunction = loadHtml.bind(null, url, localFilename);
73-
break;
74-
default:
75-
fileProcessingFunction = function () {
76-
return arguments[0];
77-
};
78-
break;
79-
}
80-
86+
// Get processing function for file
87+
fileProcessingFunction = getFileProcessingFunction(url, localFilename);
88+
// Set file as loaded
8189
setLoadedFilename(url, localFilename);
90+
// Request -> processing -> save to fs
8291
return utils.makeRequest(url).then(fileProcessingFunction).then(function saveFileToFS(text) {
8392
return fs.outputFileAsync(localFilename, text, {encoding: 'binary'});
8493
}).then(function fileSavedToFS() {
@@ -120,16 +129,16 @@ var Loader = function (data) {
120129
self.remove();
121130
});
122131

123-
// Update hrefs
124-
$('a[href]').each(function () {
125-
var self = $(this);
126-
var href = self.attr('href');
127-
var url = utils.getUrl(currentOptions.url, href);
128-
var localFileForUrl = _.findWhere(options.urlsToLoad, { url: url });
129-
if (localFileForUrl) {
130-
self.attr('href', localFileForUrl.filename);
131-
}
132-
});
132+
// // Update hrefs for loaded paths
133+
// $('a[href]').each(function () {
134+
// var self = $(this);
135+
// var href = self.attr('href');
136+
// var url = utils.getUrl(currentOptions.url, href);
137+
// var localFileForUrl = _.findWhere(options.urlsToLoad, { url: url });
138+
// if (localFileForUrl) {
139+
// self.attr('href', localFileForUrl.filename);
140+
// }
141+
// });
133142

134143
return Promise.resolve($.html());
135144
}
@@ -194,7 +203,7 @@ var Loader = function (data) {
194203
return _.map(options.paths, function (path) {
195204
return {
196205
filename: path.filename,
197-
url: url.resolve(path.url || options.url, path.path)
206+
url: url.resolve(path.url || options.url, path.path || '/')
198207
};
199208
});
200209
} else {
@@ -231,7 +240,7 @@ var Loader = function (data) {
231240

232241
function errorCleanup(error) {
233242
if (!_.isEmpty(getAllLoadedFilenames())) {
234-
fs.removeAsync(options.directory)
243+
fs.removeAsync(options.directory);
235244
}
236245
throw error;
237246
}
@@ -242,6 +251,7 @@ var Loader = function (data) {
242251
getAllLoadedFilenames: getAllLoadedFilenames,
243252
getFilename: getFilename,
244253
getDirectoryByExtension: getDirectoryByExtension,
254+
getFileProcessingFunction: getFileProcessingFunction,
245255
loadFromUrl: loadFromUrl,
246256
loadHtml: loadHtml,
247257
beforeHtmlProcessing: beforeHtmlProcessing,

0 commit comments

Comments
 (0)