|
| 1 | +var Step = require("step"), |
| 2 | +cp = require( "child_process" ) |
| 3 | +exec = cp.exec, |
| 4 | +spawn = cp.spawn, |
| 5 | +fs = require("fs"), |
| 6 | +jsdom = require( "jsdom" ), |
| 7 | +_ = require( "underscore" ), |
| 8 | +config = require("./config"), |
| 9 | + |
| 10 | +OUTPUT_DIR = config.git_dir + "/output", |
| 11 | +META_REGEX = /<script id="nanoc_meta".*<\/script>(\\n)*/, |
| 12 | + |
| 13 | +site = { |
| 14 | + articles: [], |
| 15 | + categories: [] |
| 16 | +}; |
| 17 | + |
| 18 | +_.mixin(require('underscore.string').exports()); |
| 19 | + |
| 20 | +function compile( continuation ) { |
| 21 | + console.log( "Doing 'nanoc compile' in '" + config.git_dir + "'" ); |
| 22 | + exec( "nanoc compile", { cwd: config.git_dir }, function( error, stdout, stderr ) { |
| 23 | + console.log( stdout ); |
| 24 | + if ( error !== null ) { |
| 25 | + console.error( error ); |
| 26 | + process.exit( error.code ); |
| 27 | + } |
| 28 | + continuation( error, stdout ); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function findFiles( continuation ) { |
| 33 | + var finder = require( "findit" ).find( OUTPUT_DIR ); |
| 34 | + |
| 35 | + finder.on( "file", function( file ) { |
| 36 | + var locPath = file.replace( OUTPUT_DIR, "" ), |
| 37 | + chunkedPath = locPath.split("/"), |
| 38 | + isHome = chunkedPath.length == 2, |
| 39 | + isCategory = chunkedPath.length == 3; |
| 40 | + if ( isHome ) { |
| 41 | + // TODO: Deal with home page |
| 42 | + } else if ( isCategory ) { |
| 43 | + site.categories.push({ path: file }) |
| 44 | + } else if ( locPath.indexOf("/assets/") !=0 ) { |
| 45 | + site.articles.push({ path: file }); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + finder.on( "end", continuation ); |
| 50 | +} |
| 51 | + |
| 52 | +function processCategories( continuation ) { |
| 53 | + |
| 54 | + Step( |
| 55 | + function() { |
| 56 | + var group = this.group(); |
| 57 | + site.categories.forEach(function( cat ) { |
| 58 | + cat.contents = fs.readFileSync( cat.path, "utf8" ); |
| 59 | + jsdom.env( cat.contents, group() ); |
| 60 | + }); |
| 61 | + }, |
| 62 | + function ( err, windows ) { |
| 63 | + windows.forEach( function(win, index) { |
| 64 | + var meta = win.document.getElementById('nanoc_meta'), |
| 65 | + cat = site.categories[ index ]; |
| 66 | + if (meta) { |
| 67 | + _.extend( cat, JSON.parse(meta.textContent) ) |
| 68 | + cat.contents = _.trim(cat.contents.replace( META_REGEX, "")); |
| 69 | + } |
| 70 | + }); |
| 71 | + continuation(); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +function processArticles( continuation ) { |
| 76 | + Step( |
| 77 | + function readFiles() { |
| 78 | + var group = this.group(); |
| 79 | + site.articles.forEach(function( file ) { |
| 80 | + var filename_ext = file.path.split( "/" ).slice( -1 )[ 0 ]; |
| 81 | + |
| 82 | + file.filename = file.path.replace( OUTPUT_DIR, "" ), |
| 83 | + file.contents = fs.readFileSync( file.path, "utf8" ); |
| 84 | + |
| 85 | + jsdom.env( file.contents, group() ); |
| 86 | + }); |
| 87 | + }, |
| 88 | + function processMeta(err, windows) { |
| 89 | + var ts = new Date(); |
| 90 | + windows.forEach( function(win, index) { |
| 91 | + var meta = win.document.getElementById('nanoc_meta'), |
| 92 | + file = site.articles[ index ]; |
| 93 | + if (meta) { |
| 94 | + _.extend( file, JSON.parse(meta.textContent) ) |
| 95 | + file.contents = _.trim(file.contents.replace( META_REGEX, "")); |
| 96 | + file.slug = file.filename.replace( "/"+file.chapter+ "/", "").replace("/index.html", ""); |
| 97 | + file.date = ts; |
| 98 | + } |
| 99 | + }); |
| 100 | + continuation(); |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +var nanoc = module.exports = { |
| 105 | + compile: function( continuation ) { |
| 106 | + console.log( "Doing 'nanoc compile' in '" + config.git_dir + "'" ); |
| 107 | + exec( "nanoc compile", { cwd: config.git_dir }, function( error, stdout, stderr ) { |
| 108 | + console.log( stdout ); |
| 109 | + if ( error !== null ) { |
| 110 | + console.error( error ); |
| 111 | + process.exit( error.code ); |
| 112 | + } |
| 113 | + continuation( error, stdout ); |
| 114 | + }); |
| 115 | + }, |
| 116 | + process: function( continuation ) { |
| 117 | + Step( |
| 118 | + function() { |
| 119 | + findFiles( this ); |
| 120 | + }, |
| 121 | + function() { |
| 122 | + processCategories( this ); |
| 123 | + }, |
| 124 | + function() { |
| 125 | + processArticles( this ); |
| 126 | + }, |
| 127 | + function() { |
| 128 | + continuation( null, site ); |
| 129 | + }) |
| 130 | + } |
| 131 | +}; |
0 commit comments