From 3bec8c4afc5d286aef2d0427b7f05bac6273232d Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Fri, 4 May 2012 20:29:15 +1000 Subject: [PATCH] Pushing the versions of the cachr and partials plugins that work with docpad 5.2. I suspect cachr is just whitespace, partials did have to get some changes done. --- plugins/cachr/cachr.plugin.coffee | 14 +++++--- plugins/partials/partials.plugin.coffee | 48 +++++++++++++++---------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/plugins/cachr/cachr.plugin.coffee b/plugins/cachr/cachr.plugin.coffee index 3730bca..4a22f6d 100644 --- a/plugins/cachr/cachr.plugin.coffee +++ b/plugins/cachr/cachr.plugin.coffee @@ -110,7 +110,7 @@ module.exports = (BasePlugin) -> cachr = @ @urlsToCache = {} @urlsToCacheLength = 0 - + # Apply templateData.cachr = (sourceUrl) -> return cachr.queueRemoteUrlSync(sourceUrl) @@ -128,6 +128,7 @@ module.exports = (BasePlugin) -> # Prepare cachr = @ docpad = @docpad + logger = @docpad.logger config = @config urlsToCache = @urlsToCache urlsToCacheLength = @urlsToCacheLength @@ -138,6 +139,9 @@ module.exports = (BasePlugin) -> unless urlsToCacheLength return next?() + # Log + logger.log 'info', 'Cachr started caching...', (if failures then "with #{failures} failures" else '') + # Ensure Path balUtil.ensurePath cachrPath, (err) -> # Check @@ -145,18 +149,18 @@ module.exports = (BasePlugin) -> # Async tasks = new balUtil.Group (err) => - docpad.logger.log (if failures then 'warn' else 'info'), 'Cachr finished caching everything', (if failures then "with #{failures} failures" else '') - + logger.log (if failures then 'warn' else 'info'), 'Cachr finished caching', (if failures then "with #{failures} failures" else '') + # Store all our files to be cached balUtil.each urlsToCache, (details,sourceUrl) -> tasks.push (complete) -> cachr.cacheRemoteUrl details, (err) -> if err - docpad.logger.log 'warn', "Cachr failed to fetch [#{sourceUrl}]" + docpad.logger.log 'warn', "Cachr failed to fetch: #{sourceUrl}" docpad.error(err) ++failures return complete() - + # Fire the tasks together tasks.async() diff --git a/plugins/partials/partials.plugin.coffee b/plugins/partials/partials.plugin.coffee index 0413d05..7b804c6 100644 --- a/plugins/partials/partials.plugin.coffee +++ b/plugins/partials/partials.plugin.coffee @@ -13,8 +13,8 @@ module.exports = (BasePlugin) -> config: partialsPath: 'partials' - # A list of all the partials to render - partialsToRender: null # Object + # A list of all the partials we've discovered + foundPartials: null # Object # Prepare our Configuration constructor: -> @@ -33,7 +33,7 @@ module.exports = (BasePlugin) -> # Render Partial Sync # Mapped to templateData.partial # Takes in a partialId and it's data and returns a temporary container - # which will be replaced later when we've finished rendering our partial + # which will be replaced later when we've finished rendering our partial renderPartialSync: (name,data) -> # Prepare config = @config @@ -48,7 +48,7 @@ module.exports = (BasePlugin) -> container: "[partial:#{id}]" # Store it for later - @partialsToRender[id] = partial + @foundPartials[id] = partial # Return the partial's container return partial.container @@ -60,10 +60,11 @@ module.exports = (BasePlugin) -> renderPartial: (partial,next) -> # Prepare docpad = @docpad - + # Render - document = docpad.createPartial() + document = docpad.createDocument() document.set( + partialId: partial.id filename: partial.name fullPath: partial.path ) @@ -83,8 +84,8 @@ module.exports = (BasePlugin) -> renderBefore: ({templateData}, next) -> # Prepare me = @ - @partialsToRender = {} - + @foundPartials = {} + # Apply templateData.partial = (name,data) -> return me.renderPartialSync(name,data) @@ -105,11 +106,9 @@ module.exports = (BasePlugin) -> # Prepare me = @ docpad = @docpad + logger = @docpad.logger config = @config - partialsToRender = @partialsToRender - - # Ensure we are a document - return next?() if file.type is 'partial' + foundPartials = @foundPartials # Async tasks = new balUtil.Group (err) -> @@ -117,22 +116,33 @@ module.exports = (BasePlugin) -> return next(err) # Store all our files to be cached - balUtil.each partialsToRender, (partial) -> + balUtil.each foundPartials, (partial) -> tasks.push (complete) -> - docpad.logger.log 'debug', "Partials rendering [#{partial.name}]" + # Check if we use this partial + # if we don't, then skip this partial + return complete() if opts.content.indexOf(partial.container) is -1 + + # Log + logger.log 'debug', "Rendering partial: #{partial.name}" + + # Render me.renderPartial partial, (err,contentRendered) -> # Check if err - docpad.logger.log 'warn', "Partials failed to render [#{partial.name}]" - docpad.error(err) - + # Warn + docpad.warn("Rendering partial failed: #{partial.name}. The error follows:", err) + # Replace container with the rendered content else + # Log + logger.log 'debug', "Rendered partial: #{partial.name}" + + # Apply opts.content = opts.content.replace(partial.container,contentRendered) - + # Done return complete() - + # Fire the tasks together tasks.async()