Permalink
Browse files

Pulled #251, Fixed #269, #268

We now only clear the meta attributes from the document attributes on a
new parse. Instead of clearing everything. Other parsing optimisations
were also made.

File IDs are now also the relativePath instead of the relativeBase to
avoid collisions.
  • Loading branch information...
1 parent 469651c commit b52226cea12142f88679e78e15eaad0b04f97b9e @balupton balupton committed Jul 19, 2012
View
@@ -3,7 +3,6 @@
# Necessary
pathUtil = require('path')
-fsUtil = require('fs')
_ = require('underscore')
caterpillar = require('caterpillar')
CSON = require('cson')
@@ -131,31 +130,6 @@ class DocPad extends EventEmitterEnhanced
getEvents: ->
@events
- errors:
- '400': 'Bad Request'
- '401': 'Unauthorized'
- '402': 'Payment Required'
- '403': 'Forbidden'
- '404': 'Not Found'
- '405': 'Method Not Allowed'
- '406': 'Not Acceptable'
- '407': 'Proxy Authentication Required'
- '408': 'Request Timeout'
- '409': 'Conflict'
- '410': 'Gone'
- '411': 'Length Required'
- '412': 'Precondition Failed'
- '413': 'Request Entity Too Large'
- '414': 'Request-URI Too Long'
- '415': 'Unsupported Media Type'
- '416': 'Requested Range Not Satisfiable'
- '417': 'Expectation Failed'
- '500': 'Internal Server Error'
- '501': 'Not Implemented'
- '502': 'Bad Gateway'
- '503': 'Service Unavailable'
- '504': 'Gateway Timeout'
- '505': 'HTTP Version Not Supported'
# ---------------------------------
# Collections
@@ -522,10 +496,6 @@ class DocPad extends EventEmitterEnhanced
# Whether or not we should extend the server with extra middleware and routing
extendServer: true
- # Enable Custom Error Pages
- # A flag to provide an entry to handle custom error pages
- useCustomErrors: false
-
# Port
# The port that the server should use
port: process.env.PORT ? 9778
@@ -1322,6 +1292,7 @@ class DocPad extends EventEmitterEnhanced
{layoutId} = opts
layouts = docpad.getCollection('layouts')
layout = layouts.findOne(id: layoutId)
+ layout = layouts.findOne(relativePath: layoutId) unless layout
layout = layouts.findOne(relativeBase: layoutId) unless layout
next(null,{layout})
@@ -2560,6 +2531,54 @@ class DocPad extends EventEmitterEnhanced
# ---------------------------------
# Server
+ # Serve Document
+ serveDocument: (opts,next) =>
+ # Prepare
+ [opts,next] = balUtil.extractOptsAndCallback(opts,next)
+ {document,err,req,res} = opts
+ docpad = @
+
+ # If no document, then exit early
+ unless document
+ if opts.statusCode?
+ return res.send(opts.statusCode)
+ else
+ return next()
+
+ # Content Type
+ contentType = document.get('contentTypeRendered') or document.get('contentType')
+ res.contentType(contentType)
+
+ # Send
+ dynamic = document.get('dynamic')
+ if dynamic
+ templateData = docpad.getTemplateData({req,err})
+ document.render {templateData}, (err) ->
+ content = document.get('contentRendered') or document.get('content') or document.getData()
+ if err
+ docpad.error(err)
+ return next(err)
+ else
+ if opts.statusCode?
+ return res.send(content, opts.statusCode)
+ else
+ return res.send(content)
+ else
+ content = document.get('contentRendered') or document.get('content') or document.getData()
+ if content
+ if opts.statusCode?
+ return res.send(content, opts.statusCode)
+ else
+ return res.send(content)
+ else
+ if opts.statusCode?
+ return res.send(opts.statusCode)
+ else
+ return next()
+
+ # Chain
+ @
+
# Server
server: (opts,next) =>
# Require
@@ -2631,7 +2650,7 @@ class DocPad extends EventEmitterEnhanced
return next(err) if err
# Router Middleware
- server.use server.router
+ server.use(server.router)
# Routing
server.use (req,res,next) ->
@@ -2650,66 +2669,32 @@ class DocPad extends EventEmitterEnhanced
if url isnt pageUrl
return res.redirect(url,301)
- # Content Type
- contentTypeRendered = document.get('contentTypeRendered')
- if contentTypeRendered
- res.contentType(contentTypeRendered)
-
- # Send
- dynamic = document.get('dynamic')
- if dynamic
- templateData = docpad.getTemplateData(req:req)
- document.render {templateData}, (err) ->
- contentRendered = document.get('contentRendered')
- if err
- docpad.error(err)
- return next(err)
- else
- return res.send(contentRendered)
- else
- contentRendered = document.get('contentRendered')
- if contentRendered
- return res.send(contentRendered)
- else
- return next()
+ # Serve the document to the user
+ docpad.serveDocument({document,req,res,next})
# Static
if config.maxAge
- server.use(express.static config.outPath, maxAge: config.maxAge)
+ server.use(express.static(config.outPath,{maxAge:config.maxAge}))
else
- server.use(express.static config.outPath)
+ server.use(express.static(config.outPath))
# 404 Middleware
- server.use (req, res, next) ->
+ server.use (req,res,next) ->
database = docpad.getDatabase()
- return next() unless database
- notFound = 404
- if config.useCustomErrors
- file = database.findOne(relativePath: '404.html')
- if file
- data = file.get('contentRendered') or document.get('content')
- else
- data = notFound + ' ' + errorCodes[notFound]
-
- return res.send(data, notFound)
- else
- return res.send(notFound)
+ return res.send(500) unless database
+
+ # Serve the document to the user
+ document = database.findOne(relativePath: '404.html')
+ docpad.serveDocument({document,req,res,next,statusCode:404})
# 500 Middleware
- server.use (err, req, res, next) ->
+ server.error (err,req,res,next) ->
database = docpad.getDatabase()
- return next() unless database
- serverError = 500
- if config.useCustomErrors
- file = database.findOne(relativePath: '404.html')
- if file
- data = file.get('contentRendered') or document.get('content')
- else
- data = serverError + ' ' + errorCodes[serverError]
-
- return res.send(data, serverError)
- else
- res.send(serverError)
+ return res.send(500) unless database
+
+ # Serve the document to the user
+ document = database.findOne(relativePath: '404.html')
+ docpad.serveDocument({document,req,res,next,statusCode:500,err})
# Start the Server
startServer()
@@ -276,7 +276,7 @@ class ConsoleInterface
# Prepare filename
filename = commander.args[0] or null
- if !filename or consoleInterface.split('.').length <= 2 # [name,ext,ext] = 3 parts
+ if !filename or filename.split('.').length <= 2 # [name,ext,ext] = 3 parts
opts.renderSingleExtensions = true
opts.filename = filename
@@ -119,11 +119,22 @@ class DocumentModel extends FileModel
# Parses some data, and loads the meta data and content from it
# next(err)
parseData: (data,next) ->
- # Reset
+ # Prepare
+ meta = @getMeta()
+
+ # Wipe any meta attributes that we've copied over to our file
+ reset = {}
+ for own key,value of meta.attributes
+ reset[key] = @defaults[key]
+ reset = balUtil.dereference(reset)
+ @set(reset)
+
+ # Then wipe the layout and clear the meta attributes from the meta model
@layout = null
- @getMeta().clear()
+ meta.clear()
- # Super
+ # Reparse the data and extract the content
+ # With the content, fetch the new meta data, header, and body
super data, =>
# Content
content = @get('content')
@@ -146,7 +157,7 @@ class DocumentModel extends FileModel
# Language
try
switch parser
- when 'coffee', 'cson', 'coffeescript', 'coffee-script'
+ when 'cson', 'coffee', 'coffeescript', 'coffee-script'
CSON = require('cson') unless CSON
meta = CSON.parseSync(header)
@meta.set(meta)
@@ -297,47 +308,60 @@ class DocumentModel extends FileModel
# Check
return next(err) if err
+ # Prepare
+ changes = {}
+
# Fetch
meta = @getMeta()
- fullPath = @get('fullPath') or null
- basename = @get('basename') or null
- relativeBase = @get('relativeBase') or null
- relativeDirPath = @get('relativeDirPath') ? null
+ fullPath = @get('fullPath')
+ basename = @get('basename')
+ relativeDirPath = @get('relativeDirPath')
extensions = @get('extensions')
- extensionRendered = @get('extensionRendered') or null
- url = meta.get('url') or null
- name = meta.get('name') or null
- outPath = meta.get('outPath') or null
- contentTypeRendered = null
+ extensionRendered = @get('extensionRendered')
+ url = meta.get('url')
+ name = meta.get('name')
+ outPath = meta.get('outPath')
filenameRendered = null
- # Adjust
+ # Use our eve's rendered extension if it exists
if eve?
extensionRendered = eve.get('extensionRendered')
- if basename? and extensionRendered?
+
+ # Figure out the rendered filename
+ if basename and extensionRendered
if basename[0] is '.' and extensionRendered is extensions[0]
filenameRendered = basename
else
filenameRendered = "#{basename}.#{extensionRendered}"
- if relativeDirPath? and filenameRendered?
+ changes.filenameRendered = filenameRendered
+
+ # Figure out the rendered url
+ if !url and filenameRendered
if relativeDirPath
url = "/#{relativeDirPath}/#{filenameRendered}"
else
url = "/#{filenameRendered}"
- if filenameRendered?
- name or= filenameRendered
- if @outDirPath?
- outPath = pathUtil.join(@outDirPath,url)
- if url?
+ changes.url = url
+
+ # Set name if it doesn't exist already
+ if !name and filenameRendered?
+ changes.name = name = filenameRendered
+
+ # Create the outPath if we have a outpute directory
+ if @outDirPath
+ changes.outPath = outPath = pathUtil.join(@outDirPath,url)
+
+ # Update the URL
+ if url
@removeUrl(@get('url'))
@setUrl(url)
# Content Types
if outPath or fullPath
- contentTypeRendered = mime.lookup(outPath or fullPath)
+ changes.contentTypeRendered = contentTypeRendered = mime.lookup(outPath or fullPath)
# Apply
- @set({extensionRendered,filenameRendered,url,name,outPath,contentTypeRendered})
+ @set(changes)
# Forward
next()
Oops, something went wrong.

0 comments on commit b52226c

Please sign in to comment.