var fs = require('fs-extra')
  , path = require('path')
  , mkdirp = require('mkdirp')
  , Handlebars = require('handlebars')
  , natural = require('natural')
  , inflector = new natural.NounInflector()
  , site = require('./site')
  , File = require('./file')
  , Permalink = require('./permalink')
  , _ = require('lodash')


function Page(file, type) {
  // assign all file data properties to the page for convenience
  _.assign(this, file.data)

  this.file = file
  this.type = type
  this.typePlural = type ? inflector.pluralize(type) : null
  this.filename = file.filename
  this.content = file.content
  this.extension = path.extname(this.filename)
  this.permalink = Permalink.getFromPage(this).toString()
}

Page.prototype.render = function() {
  var file = this.file
    , data = { site: site.templateData, page: this, content: this.content }

  site.emit("beforeRenderFile", this)

  // The first time through we compile from the page content
  // (rather than file.content) because a plugin may have altered it
  this.content = Handlebars.compile(this.content)(data)

  site.emit("afterRenderFile", this)

  // looks like marked is converting `'` to `&#39;` which is messing
  // up handlebars so we have to do the handlebars processing of the
  // template first, then the markdown transformations, then the
  // handlebars processing of the rendered page
  site.emit("transformFile", this)

  site.emit("beforeRenderPage")

  while (file = file.parent) {
    data.content = this.content
    this.content = Handlebars.compile(file.content)(data)
  }

  site.emit("afterRenderPage", this)
  console.log("Rendering " + this.filename)
}

Page.prototype.write = function() {

  var destination = path.join(site.config.destination, this.permalink)

  site.emit("beforeWrite", this)

  // if destination is a folder, create an index.html
  if (!path.extname(destination)) {
    destination = path.join(destination, "index.html")
  }

  fs.outputFileSync(destination, this.content)

  console.log("Writing " + this.filename + " to " + destination)
  site.emit("afterWrite", this)
}

module.exports = Page

var fs = require('fs-extra')
  , path = require('path')
  , mkdirp = require('mkdirp')
  , Handlebars = require('handlebars')
  , natural = require('natural')
  , inflector = new natural.NounInflector()
  , site = require('./site')
  , File = require('./file')
  , Permalink = require('./permalink')
  , _ = require('lodash')


var nonTemplateProperties = [
  "extension",
  "file",
  "filename",
  "layout",
  "type",
]


function Page(file, type) {
  // assign all file data properties to the page for convenience
  _.assign(this, file.data)

  this.file = file
  this.type = type
  this.typePlural = type ? inflector.pluralize(type) : null
  this.filename = file.filename
  this.content = file.content
  this.extension = path.extname(this.filename)
  this.permalink = Permalink.getFromPage(this).toString()

  // store this page on the site object
  if (type) {
    site._posts[this.typePlural].push(this)
  } else {
    site._pages.push(this)
  }
}

Page.prototype.render = function() {
  var file = this.file
    , data = { site: site.templateData, page: this, content: this.content }

  site.emit("beforeRenderFile", this)

  // The first time through we compile from the page content
  // (rather than file.content) because a plugin may have altered it
  this.content = Handlebars.compile(this.content)(data)

  site.emit("afterRenderFile", this)

  // looks like marked is converting `'` to `&#39;` which is messing
  // up handlebars so we have to do the handlebars processing of the
  // template first, then the markdown transformations, then the
  // handlebars processing of the rendered page
  site.emit("transformFile", this)

  site.emit("beforeRenderPage")

  while (file = file.parent) {
    data.content = this.content
    this.content = Handlebars.compile(file.content)(data)
  }

  site.emit("afterRenderPage", this)
  console.log("Rendering " + this.filename)
}

Page.prototype.write = function() {

  var destination = path.join(site.config.destination, this.permalink)

  site.emit("beforeWrite", this)

  // if destination is a folder, create an index.html
  if (!path.extname(destination)) {
    destination = path.join(destination, "index.html")
  }

  fs.outputFileSync(destination, this.content)

  console.log("Writing " + this.filename + " to " + destination)
  site.emit("afterWrite", this)
}

Page.prototype.toTemplateObject = function() {
  return _(this)
    .omit(nonTemplateProperties)
    .omit(_.isFunction)
    .value()
}

module.exports = Page

Add a Page method that returns its data as a template friendly object.