Code coverage report for lib/page.js

Statements: 100% (97 / 97)      Branches: 96.15% (25 / 26)      Functions: 100% (13 / 13)      Lines: 100% (95 / 95)      Ignored: none     

All files » lib/ » page.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 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1   1                                                 1 284   142 142   142   142                                       142 142 142 142   142   142 142   142     1 1672 1672 1672   1672     1 1554 1554   1554     1 111   111                           111     1 604 604 604 604 604 604   604     1 1553 1553 1553 1553 1553   1553 1161     1553 3     1553     1 117 117   117     1 4   4     1 115   115     1 1671 1671     1671 1388   283     1671   1671     1 112 112   112     1 222   222 42       1 1 1   1 1 1   1                
 
const fs = require('graceful-fs')
const path = require('path')
const error = require('./formatted-error')
const prr = require('prr')
const url = require('url')
const mime = require('mime')
const hogan = require('hogan.js')
const xtend = require('xtend')
const debug = require('debug')
const slug = require('to-slug-case')
const humanize = require('string-humanize')
const crypto = require('crypto')
const rfc822 = require('rfc822-date')
const assert = require('assert')
const now = require('date-now')
 
module.exports = Page
 
/*
 
Attributes:
 
* name: relative name for finding later (default title)
* basedir: the source dir to esolve against for urls, names, etc
* filename: the original file path
* url: the url of the page
* title:
* date
* draft:
* meta: the extracted front-matter
* last-modified: last-modified obvs.
* etag: unique id for the entity for proper http caching
* content-type: the content-type of the page, can be defined in the meta
 
* outfile: the destination of the rendered page (determined by haiku)
 
* context:
* render: template function
 
*/
 
function Page(options) {
  if (!(this instanceof Page)) return new Page(options)
 
  assert.ok(options.filename, 'options.filename is required')
  assert.ok(options.basedir, 'options.basedir is required')
 
  var page = this
 
  Object.defineProperties(page, {
    url: { get: getURL },
    slug: { get: getSlug },
    title: { get: getTitle },
    date: { get: getDate },
    draft: { get: getDraft },
    etag: {
      get: getEtag
    },
    'content-type': {
      get: getContentType
    },
    'last-modified': {
      get: getLastModified
    },
    index: {
      get: getIndex
    }
  }, { enumerable: true })
 
  page.filename = options.filename
  page.basedir = options.basedir
  page.name = path.relative(page.basedir, page.filename)
  page.baseurl = options.baseurl || '/'
 
  page.mtime = options.mtime || now()
 
  page.meta = options.meta || {}
  page.body = options.body || ''
 
  page.debug = debug('haiku:page - ' + page.name)
}
 
Page.prototype.is = function(string) {
  var page = this
  var _mime = mime.lookup(page.name)
  var match = !!_mime.match(string)
 
  return match
}
 
Page.prototype.wants = function(string) {
  var page = this
  var match = page['content-type'].match(string)
 
  return match
}
 
Page.prototype.toJSON = function() {
  var page = this
 
  var json = xtend(page.meta, {
    url: page.url,
    name: page.name,
    slug: page.slug,
    draft: page.draft,
    title: page.title,
    'last-modified': page['last-modified'],
    'content-type': page['content-type'],
    body: page.body,
    filename: page.filename,
    basedir: page.basedir,
    mtime: page.mtime
  })
 
  return json
}
 
function getSlug() {
  var page = this
  var extension = path.extname(page.url)
  var basename = path.basename(page.url)
  var cleaned = basename.replace(extension, '')
  var dirname = path.dirname(page.name)
  var _slug = slug(cleaned)
 
  return path.join(dirname, _slug)
}
 
function getURL() {
  var page = this
  var _url = url.resolve('/', page.name)
  var extension = path.extname(page.name)
  var translated = extension
  var debug = page.debug
 
  if (page.wants('html')) {
    _url = _url.replace(extension, '.html')
  }
 
  if (page.baseurl !== '/') {
    _url = path.join(page.baseurl, _url)
  }
 
  return _url
}
 
function getTitle() {
  var page = this
  var title = path.basename(page.slug)
 
  return page.meta.title || humanize(title)
}
 
function getDate() {
  var page = this
 
  return page.meta.date
}
 
function getDraft() {
  var page = this
 
  return !! page.meta.draft || false
}
 
function getContentType() {
  var page = this
  var override = page.meta['content-type']
 
 
  if (page.is('markdown')) {
    ct = mime.lookup('.html')
  } else {
    ct = mime.lookup(page.name)
  }
 
  if (override) ct = override
 
  return ct
}
 
function getLastModified() {
  var page = this
  var mtime = new Date(page.mtime)
 
  return rfc822(mtime)
}
 
function getIndex() {
  var page = this
 
  if (page.slug.match(/index$/)) {
    return path.resolve(page.baseurl, page.slug.replace('index', ''))
  }
}
 
function getEtag() {
  var page = this
  var debug = page.debug
 
  debug('page.url: %s', page.url)
  debug('page.mtime: %s', page.mtime.getTime())
  debug('page.body', page.body)
 
  return crypto
  .createHash('md5')
  .update(page.url)
  .update(page.mtime.toString())
  // TODO: change to rendered output
  .update(page.body || '')
  .digest('hex')
}