Code coverage report for lib/read.js

Statements: 100% (37 / 37)      Branches: 100% (14 / 14)      Functions: 100% (5 / 5)      Lines: 100% (33 / 33)      Ignored: none     

All files » lib/ » read.js
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  1 1 1 1 1 1 1   1   1 138 135 135     138           138 138   137   137 137   136 136         135 135   135   135           1 136 136   136 136   1       1 8       136    
 
const fs = require('graceful-fs')
const path = require('path')
const page = require('./page')
const fm = require('front-matter')
const debug = require('debug')('haiku:read')
const extend = require('xtend')
const error = require('./formatted-error')
 
module.exports = read
 
function read(filename, basedir, baseurl, callback) {
  if (arguments.length === 3) {
    callback = baseurl
    baseurl = '/'
  }
 
  var p = page({
    filename: filename,
    basedir: basedir,
    baseurl: baseurl
  })
 
  fs.stat(filename, function(err, stats) {
    if (err) return callback(err)
 
    p.mtime = stats.mtime
 
    fs.readFile(filename, 'utf8', function(err, data) {
      if (err) return callback(err)
 
      ffffm(data, filename, function(err, extraction) {
        if (err) return callback(err)
 
        // TODO: move this into a method
        // page.meta = xtend(page.meta, extraction.attributes)
        // page.template = hogan.compile(extraction.body)
        p.meta = extend(page.meta, extraction.attributes)
        p.body = extraction.body || ''
 
        debug('extracted fm', extraction)
 
        callback(null, p)
      })
    })
  })
}
 
function ffffm(data, filename, callback) {
  var extraction
  var err
 
  try {
    extraction = fm(data)
  } catch (e) {
    err = error('error parsing %s', filename)
 
    // Map yaml-js parse error attributes unto the error for now
    // TODO: bake this into front-matter
    for (key in e) {
      if (e.hasOwnProperty(key) && key !== 'stack') err[key] = e[key]
    }
  }
 
  callback(err, extraction)
}