@@ -10,25 +10,18 @@ var _ = require('underscore');
1010
1111var defaults = require ( './config/defaults' ) ;
1212var types = require ( './config/resource-types' ) ;
13- var resursiveSources = require ( './config/recursive-sources' ) ;
13+ var recursiveSources = require ( './config/recursive-sources' ) ;
1414var utils = require ( './utils.js' ) ;
1515var request = require ( './request' ) ;
1616var Resource = require ( './resource' ) ;
17- var loadHtml = require ( './file-handlers/html' ) ;
18- var loadCss = require ( './file-handlers/css' ) ;
1917var compareUrls = require ( 'compare-urls' ) ;
2018
21- function getHandleFunction ( resource ) {
22- var type = resource . getType ( ) ;
23- switch ( type ) {
24- case types . css : return loadCss ;
25- case types . html : return function loadHtmlAndCss ( context , po ) {
26- return loadHtml ( context , po ) . then ( function ( loaded ) {
27- return loadCss ( context , loaded ) ;
28- } ) ;
29- } ;
30- default : return _ . noop ;
31- }
19+ var loadHtml = require ( './file-handlers/html' ) ;
20+ var loadCss = require ( './file-handlers/css' ) ;
21+ function loadHtmlAndCss ( context , po ) {
22+ return loadHtml ( context , po ) . then ( function ( loaded ) {
23+ return loadCss ( context , loaded ) ;
24+ } ) ;
3225}
3326
3427function Scraper ( options ) {
@@ -89,6 +82,20 @@ Scraper.prototype.getDirectoryByExtension = function getDirectoryByExtension (ex
8982 . value ( ) || '' ;
9083} ;
9184
85+ Scraper . prototype . getResourceHandler = function getHandler ( resource ) {
86+ var self = this ;
87+ var type = resource . getType ( ) ;
88+ var depth = resource . getDepth ( ) ;
89+ var depthGreaterThanMax = self . options . maxDepth && depth > self . options . maxDepth ;
90+
91+ switch ( true ) {
92+ case depthGreaterThanMax : return _ . noop ;
93+ case type == types . css : return loadCss ;
94+ case type == types . html : return loadHtmlAndCss ;
95+ default : return _ . noop ;
96+ }
97+ } ;
98+
9299Scraper . prototype . loadResource = function loadResource ( resource ) {
93100 var self = this ;
94101
@@ -108,7 +115,7 @@ Scraper.prototype.loadResource = function loadResource (resource) {
108115 return self . makeRequest ( url ) . then ( function requestCompleted ( data ) {
109116 resource . setUrl ( data . url ) ; // Url may be changed in redirects
110117 resource . setText ( data . body ) ;
111- handleFile = getHandleFunction ( resource ) ;
118+ handleFile = self . getResourceHandler ( resource ) ;
112119 return handleFile ( self , resource ) ;
113120 } ) . then ( function fileHandled ( ) {
114121 var filename = path . join ( self . options . directory , resource . getFilename ( ) ) ;
@@ -143,7 +150,7 @@ Scraper.prototype.prepare = function prepare () {
143150 } ) ;
144151
145152 if ( self . options . recursive ) {
146- self . options . sources = _ . union ( self . options . sources , resursiveSources ) ;
153+ self . options . sources = _ . union ( self . options . sources , recursiveSources ) ;
147154 }
148155
149156 return ensureDirAsync ( self . options . directory ) ;
0 commit comments