From 9c2bff8f1f6c4dafa57cefe632808c51f3a0e679 Mon Sep 17 00:00:00 2001 From: "adam j. sontag" Date: Fri, 12 Oct 2012 03:51:25 -0400 Subject: [PATCH 1/3] Add support for assigning WordPress menu_order field based on an a YAML file of slugs to optionally control article display order --- package.json | 3 ++- tasks/build.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3fac3ea..c7500e5 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "cheerio": "0.8.3", "rimraf": "2.0.2", "marked": "0.2.5", - "js-yaml": "1.0.2" + "js-yaml": "1.0.2", + "lodash": "0.8.x" }, "keywords": [ "gruntplugin" diff --git a/tasks/build.js b/tasks/build.js index 46a38bc..9789934 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -18,6 +18,7 @@ var // modules cheerio = require( "cheerio" ), nsh = require( "node-syntaxhighlighter" ), path = require( "path" ), + _ = require( "lodash" ), yaml = require( "js-yaml" ); // Add a wrapper around wordpress-parse-post that supports YAML @@ -45,12 +46,50 @@ grunt.registerHelper( "wordpress-parse-post-flex", function( path ) { return grunt.helper( "wordpress-parse-post", path ); }); +//Process a YAML order file and return an object of page slugs and their ordinal indices +grunt.registerHelper( "read-order", function( orderFile, taskDone ) { + var order, + map = {}, + index = 0; + + try { + order = yaml.load( grunt.file.read( orderFile ) ); + order.forEach( function(chapter) { + var article, title; + + if ( _.isObject( chapter ) ) { + title = Object.keys( chapter )[ 0 ]; + map[ title ] = ++index; + + chapter[ title ].forEach( function( article ) { + map[ title + "/" + article ] = ++index; + }); + } else { + map[ title ] = ++index; + } + }); + return map; + } catch( error ) { + grunt.warn( "Invalid order file: " + orderFile ); + taskDone(); + return null; + } + + +}); + grunt.registerMultiTask( "build-pages", "Process html and markdown files as pages, include @partials and syntax higlight code snippets", function() { var content, + orderMap, task = this, taskDone = task.async(), files = this.data, - targetDir = grunt.config( "wordpress.dir" ) + "/posts/page/"; + targetDir = grunt.config( "wordpress.dir" ) + "/posts/page/", + orderFile = grunt.config( "wordpress.order" ); + + if ( orderFile ) { + orderMap = grunt.helper( "read-order", orderFile, taskDone ); + } grunt.file.mkdir( targetDir ); @@ -58,7 +97,20 @@ grunt.registerMultiTask( "build-pages", "Process html and markdown files as page var post = grunt.helper( "wordpress-parse-post-flex", fileName ), content = post.content, fileType = /\.(\w+)$/.exec( fileName )[ 1 ], - targetFileName = targetDir + fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ) + ".html"; + targetSlug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ), + targetFileName = targetDir + targetSlug + ".html"; + + // If an order file was specified, set the menu_order, + // unless the page being processed isn't in the order file, + // in which case it shouldn't be published + if ( orderMap ) { + var menuOrder = orderMap[ targetSlug ]; + if ( menuOrder ) { + post.menuOrder = menuOrder; + } else { + post.postStatus = "draft"; + } + } grunt.verbose.write( "Processing " + fileName + "..." ); delete post.content; From d490b6ed192eb2ebc24cb47cab47f8d69bd774a4 Mon Sep 17 00:00:00 2001 From: "adam j. sontag" Date: Fri, 12 Oct 2012 11:41:58 -0400 Subject: [PATCH 2/3] style/code fixes pursuant to code review --- package.json | 3 +-- tasks/build.js | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index c7500e5..3fac3ea 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,7 @@ "cheerio": "0.8.3", "rimraf": "2.0.2", "marked": "0.2.5", - "js-yaml": "1.0.2", - "lodash": "0.8.x" + "js-yaml": "1.0.2" }, "keywords": [ "gruntplugin" diff --git a/tasks/build.js b/tasks/build.js index 9789934..f860b5e 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -18,7 +18,6 @@ var // modules cheerio = require( "cheerio" ), nsh = require( "node-syntaxhighlighter" ), path = require( "path" ), - _ = require( "lodash" ), yaml = require( "js-yaml" ); // Add a wrapper around wordpress-parse-post that supports YAML @@ -47,40 +46,38 @@ grunt.registerHelper( "wordpress-parse-post-flex", function( path ) { }); //Process a YAML order file and return an object of page slugs and their ordinal indices -grunt.registerHelper( "read-order", function( orderFile, taskDone ) { +grunt.registerHelper( "read-order", function( orderFile ) { var order, map = {}, index = 0; try { order = yaml.load( grunt.file.read( orderFile ) ); - order.forEach( function(chapter) { - var article, title; - - if ( _.isObject( chapter ) ) { - title = Object.keys( chapter )[ 0 ]; - map[ title ] = ++index; - - chapter[ title ].forEach( function( article ) { - map[ title + "/" + article ] = ++index; - }); - } else { - map[ title ] = ++index; - } - }); - return map; } catch( error ) { grunt.warn( "Invalid order file: " + orderFile ); taskDone(); return null; } + order.forEach(function(chapter) { + var article, title; + if ( grunt.utils._.isObject( chapter ) ) { + title = Object.keys( chapter )[ 0 ]; + map[ title ] = ++index; + + chapter[ title ].forEach(function( article ) { + map[ title + "/" + article ] = ++index; + }); + } else { + map[ title ] = ++index; + } + }); + return map; }); grunt.registerMultiTask( "build-pages", "Process html and markdown files as pages, include @partials and syntax higlight code snippets", function() { - var content, - orderMap, + var content, orderMap, task = this, taskDone = task.async(), files = this.data, @@ -88,23 +85,28 @@ grunt.registerMultiTask( "build-pages", "Process html and markdown files as page orderFile = grunt.config( "wordpress.order" ); if ( orderFile ) { - orderMap = grunt.helper( "read-order", orderFile, taskDone ); + orderMap = grunt.helper( "read-order", orderFile ); + if ( !orderMap ) { + taskDone(); + return; + } } grunt.file.mkdir( targetDir ); grunt.utils.async.forEachSeries( files, function( fileName, fileDone ) { - var post = grunt.helper( "wordpress-parse-post-flex", fileName ), + var menuOrder, + post = grunt.helper( "wordpress-parse-post-flex", fileName ), content = post.content, fileType = /\.(\w+)$/.exec( fileName )[ 1 ], targetSlug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ), targetFileName = targetDir + targetSlug + ".html"; - // If an order file was specified, set the menu_order, + // If an order file was specified, set the menuOrder, // unless the page being processed isn't in the order file, // in which case it shouldn't be published if ( orderMap ) { - var menuOrder = orderMap[ targetSlug ]; + menuOrder = orderMap[ targetSlug ]; if ( menuOrder ) { post.menuOrder = menuOrder; } else { From 2e1aa284686abf87efcf2aaf73261cad07609dc9 Mon Sep 17 00:00:00 2001 From: "adam j. sontag" Date: Fri, 12 Oct 2012 11:47:35 -0400 Subject: [PATCH 3/3] set status instead of postStatus if an article is not in the order file --- tasks/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/build.js b/tasks/build.js index f860b5e..b12c04a 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -110,7 +110,7 @@ grunt.registerMultiTask( "build-pages", "Process html and markdown files as page if ( menuOrder ) { post.menuOrder = menuOrder; } else { - post.postStatus = "draft"; + post.status = "draft"; } }