 	# The unique document identifier
 	id: null
 	
-	# The filename without the extension
+	# The file's name without the extension
 	basename: null
 
-	# The filename with the extension
+	# The file's last extension
+	# "hello.md.eco" -> "eco"
+	extension: null
+
+	# The file's extensions as an array, in reverse order
+	# "hello.md.eco" -> ["eco","md"]
+	extensions: []
+
+	# The final extension used for our rendered file
+	# Takes into accounts layouts
+	# "layout.html", "post.md.eco" -> "html"
+	extensionRendered: null
+
+	# The file's name with the extension
 	filename: null
 
-	# The extensions the filename has, in reverse order (.md.eco -> [eco,md])
-	extensions: []
-
-	# The original first extension (.md.eco -> eco)
-	extension: null
-
-	# The final extension used on our rendered file (takes into accounts layouts)
-	extensionRendered: null
-
-	# The full path of our file, only necessary is calling @load
+	# The file's name with the rendered extension
+	filenameRendered: null
+
+	# The full path of our file, only necessary if called by @load
 	fullPath: null
 
-	# The final rendered path of our file 
+	# The final rendered path of our file
 	outPath: null
 
 	# The relative path of our source file (with extensions)

	# The relative base of our source file (no extension)
	relativeBase: null

	content: null
	contentSrc: null
	contentRaw: null
	contentRendered: null
	fileMeta: {}
	fileHead: null
	fileBody: null
	fileHeadParser: null


	# ---------------------------------

	# Render this file
	# next(err,finalExtension)
	render: (templateData,next) ->
		# Prepare
		docpad = @docpad
		logger = @logger
		file = @

		# Log
		logger.log 'debug', "Rendering the file #{@relativePath}"

		# Prepare reset
		reset = ->
			file.rendered = false
			file.content = file.body
			file.contentRendered = file.body
			file.contentRenderedWithoutLayouts = file.body
			file.contentRenderings = [{
				content: file.content
				extension: file.extension
			}]
			file.contentRenderingsByLayout = {
				none: [
					content: file.content
					extension: file.extension
				]
			}

		# Prepare complete
		finish = (err) ->
			# Reset the content to it's original value
			file.content = file.body

			# Error
			return next?(err)  if err
			
			# Log
			logger.log 'debug', "Rendering completed for #{file.relativePath}"
			
			# Success
			file.rendered = true
			return next?()
		
		# Prepare render layouts
		# next(err)
		renderLayouts = (next) ->
			# Store the content without any layout rendering
			file.contentRenderedWithoutLayouts = file.content

			# Skip ahead if we don't have a layout
			return next?()  unless file.layout
			
			# Grab the layout
			file.getLayout (err,layout) ->
				# Error
				return next?(err)  if err

				# Assign the rendered file content to the templateData.content
				templateData.content = file.contentRendered

				# Render the layout with the templateData
				layout.render templateData, (err) ->
					# Error
					return next?(err)  if err

					# Apply the rendering
					file.contentRendered = layout.contentRendered
					for own key,value of layout.contentRenderingsByLayout
						if key is 'none'
							file.contentRenderingsByLayout[layout.relativeBase] = value
						else
							file.contentRenderingsByLayout[key] = value

					# Success
					return next?()
		
		# Prepare render extensions
		# next(err)
		renderExtensions = (next) =>
			# If we only have one extension, then skip ahead to rendering layouts
			return next?()  if file.extensions.length <= 1

			# Prepare the tasks
			tasks = new util.Group next

			# Clone extensions
			extensions = []
			for extension in file.extensions
				extensions.unshift extension

			# Cycle through all the extension groups
			previousExtension = null
			for extension in extensions
				# Has a previous extension
				if previousExtension
					# Event data
					eventData = 
						inExtension: previousExtension
						outExtension: extension
						templateData: templateData
						file: file
					
					# Create a task to run
					tasks.push ((file,eventData) -> ->
						# Render through plugins
						docpad.triggerPluginEvent 'render', eventData, (err) ->
							# Error?
							if err
								logger.log 'warn', 'Something went wrong while rendering:', file.relativePath
								return tasks.exit(err)

							# Update rendered content
							file.contentRendered = file.content
							file.contentRenderings.push(
								content: file.contentRendered
								extension: eventData.outExtension
							)
							file.contentRenderingsByLayout.none.push(
								content: file.contentRendered
								extension: eventData.outExtension
							)

							# Complete
							return tasks.complete(err)
					
					)(file,eventData)

				# Cycle
				previousExtension = extension
			
			# Run tasks synchronously
			return tasks.sync()

		# Reset everything
		reset()

		# Render the extensions, then the layouts, then finish
		renderExtensions (err) ->
			return finish(err)  if err
			renderLayouts (err) ->
				return finish(err)

		# Chain
		@
