1 | cp = require 'child_process'
|
2 | fs = require 'fs'
|
3 | _ = require 'underscore'
|
4 | _.str = require 'underscore.string'
|
5 | async = require 'async'
|
6 |
|
7 | CONTENT_TYPES = require './content'
|
8 |
|
9 | class Finder
|
10 | constructor: ( @site, kind, cb ) ->
|
11 | site = @site
|
12 | kind = _.str.rtrim( kind, 's' )
|
13 | klass = _.str.capitalize(kind)
|
14 | @["_get#{klass}Paths"] ( err, filenames ) ->
|
15 |
|
16 | filtered = _.reject( filenames || [], ( f ) -> f[0] == '.' || f[0] == '_' )
|
17 |
|
18 | contentGenerator = ( path, done ) ->
|
19 | content = new CONTENT_TYPES[klass]( site, path )
|
20 | content.process( done )
|
21 | async.map( filtered, contentGenerator, cb )
|
22 | _getStaticPaths: ( cb ) ->
|
23 | fs.readdir "#{@site.root}/_static", ( err, statics ) ->
|
24 | cb( err, statics || [] )
|
25 | _getStylePaths: ( cb ) ->
|
26 | fs.readdir "#{@site.root}/_styles", ( err, styles ) ->
|
27 | cb( err, styles || [] )
|
28 | _getScriptPaths: ( cb ) ->
|
29 | fs.readdir "_scripts", ( err, scripts ) ->
|
30 | cb( err, scripts || [] )
|
31 | _getPostPaths: ( cb ) ->
|
32 | fs.readdir "#{@site.root}/_posts", ( err, listings ) ->
|
33 |
|
34 | cb( null, listings || [] )
|
35 | _getPagePaths: ( cb ) ->
|
36 | site = @site
|
37 | paths = []
|
38 | processor = ( listing, done ) ->
|
39 | if listing == null
|
40 | done()
|
41 | return
|
42 | fs.stat "#{site.root}/_pages/#{listing}", ( err, stat ) ->
|
43 | if err
|
44 | Logger.error( "Could not stat file #{listing}" )
|
45 | done()
|
46 | else if stat.isFile()
|
47 | paths.push listing
|
48 | done()
|
49 | else
|
50 | fs.readdir "#{site.root}/_pages/#{listing}", ( err2, sublistings ) ->
|
51 | sublistings.forEach ( sublisting ) ->
|
52 | q.push( "#{listing}/#{sublisting}" )
|
53 | done()
|
54 |
|
55 | q = async.queue processor, 1
|
56 |
|
57 | fs.readdir "#{@site.root}/_pages", ( err, listings ) ->
|
58 |
|
59 | (listings || []).forEach ( listing ) -> q.push( listing )
|
60 |
|
61 | q.push( null )
|
62 | q.drain = () -> cb( null, paths );
|
63 |
|
64 | module.exports = Finder
|