Skip to content
This repository
tag: v0.0.7
Fetching contributors…

Cannot retrieve contributors at this time

executable file 129 lines (112 sloc) 3.657 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#!/usr/bin/env node

// docpack - documentation for take-away
// disgusting ... but works for now ...

var path = require('path')
  , fs = require('fs')
  , child_process = require('child_process')
  , slide = require('slide')
  , mkdirp = require('mkdirp')
  , glob = require('glob')
  , ejs = require('ejs')
  , less = require('less')
  , ncp = require('ncp').ncp
  , view = require('./lib/view')
  , obj = require('./lib/obj')
  , j = path.join
  , chain = slide.chain
  , compiler = require('./lib/compiler')
  ;

var rootd = path.resolve(path.dirname(process.argv[1]), '..')
  , docd = rootd + '/doc'
  , outd = rootd + '/out/pages'
  , package = JSON.parse(fs.readFileSync(rootd + '/package.json'))
  , layout = fs.readFileSync(docd + '/layout.ejs').toString()
  ;

var ejs2textile = compiler.compilerSync(ejs.render, 'ejs', {package: package})
  , ejs2html = compiler.compiler(render_page, 'redcloth', {dst: compiler.dst, outputDir: outd})
  , less2css = compiler.compiler(less.render, 'less')
  ;

var prepare = [ [ mkdirp, outd ]
              ]
  , tasks = [ [ ejs2textile, docd + '/README.ejs', rootd + '/README.textile']
              , [ render_pages, docd + '/pages/*.ejs']
              , [ render_build_status_pages ]
              , [ render_stylesheets, docd + '/pages/stylesheets/*.less']
              , [ ncp, docd + '/pages/images', outd + '/images']
              , [ ncp, docd + '/pages/scripts', outd + '/scripts']
              , [ ncp, docd + '/pages/jqplot', outd + '/jqplot']
              ]
              ;

chain( [ [ run_all, prepare]
       , [ run_all, tasks]
       ]
     , function(error) { if (error) console.log(error) }
     );



function render_pages(pattern, cb) {
  glob(pattern, function(error, files) {
    var tasks = files.map(function(f) {
      var dst = path.join(outd, path.basename(path.basename(f), '.ejs'))
          + '.html';
      return [ejs2html, f, dst]
    });
    run_all(tasks, cb);
  });
}

function render_stylesheets(pattern, cb) {
  glob(pattern, function(error, files) {
    var tasks = files.map(function(f) {
      var dst = outd + '/stylesheets/'
          + path.basename(path.basename(f), '.less') + '.css';
      return [less2css, f, dst, {}]
    });
    run_all(tasks, cb);
  });
}

function render_page(source, options, cb) {
  var metadata = { scripts: [], stylesheets: ['/stylesheets/mdns.css']};
  function layout_page(error, html) {
    metadata.body = html;
    cb(error, ejs.render(layout, obj.union(metadata, view.helpers(metadata, options))))
  }
  var locals = obj.union({package: package}, view.helpers(metadata, options));
  piped_comand('redcloth', ejs.render(source, locals), layout_page);
}

function render_build_status_pages(cb) {
  cb()
}

function piped_comand(comand, input, args, cb) {
  if ( ! cb ) {
    cb = args;
    args = undefined;
  }
  var cmd = child_process.spawn(comand, args)
    , out = ''
    ;
  cmd.stdout.on('data', function(chunk) { out += chunk })
  cmd.stderr.on('data', function(chunk) { console.log(chunk)})
  cmd.on('exit', function(code) {
    if (code) {
      cb(new Error("command '" + command + "' exited with code " + code))
    } else {
      cb(null, out)
    }
  });
  cmd.stdin.write(input);
  cmd.stdin.destroySoon();
  
}


function run_all(tasks, cb) {
  var done_count = 0, first_error;
  function on_done(error) {
    if (error) {
      first_error = error;
      cb(error);
    }
    if ( ! first_error && ++done_count === tasks.length) cb();
  }
  tasks.forEach(function(t) {
    var f = t.shift();
    t.push(on_done)
    f.apply(null, t);
  });
}

// vim: filetype=javascript :
Something went wrong with that request. Please try again.