-
Notifications
You must be signed in to change notification settings - Fork 32
Support order.yml file for optionally controlling article display order #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,20 +46,71 @@ 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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't use a callback. |
||
var order, | ||
map = {}, | ||
index = 0; | ||
|
||
try { | ||
order = yaml.load( grunt.file.read( orderFile ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be the only line in the |
||
order.forEach( function(chapter) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var article, title; | ||
|
||
if ( _.isObject( chapter ) ) { | ||
title = Object.keys( chapter )[ 0 ]; | ||
map[ title ] = ++index; | ||
|
||
chapter[ title ].forEach( function( article ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
map[ title + "/" + article ] = ++index; | ||
}); | ||
} else { | ||
map[ title ] = ++index; | ||
} | ||
}); | ||
return map; | ||
} catch( error ) { | ||
grunt.warn( "Invalid order file: " + orderFile ); | ||
taskDone(); | ||
return null; | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove blank lines. |
||
}); | ||
|
||
grunt.registerMultiTask( "build-pages", "Process html and markdown files as pages, include @partials and syntax higlight code snippets", function() { | ||
var content, | ||
orderMap, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move up to the previous line. |
||
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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should fail the task if this helper fails. All the logic for ending a task should be inside the task. |
||
} | ||
|
||
grunt.file.mkdir( targetDir ); | ||
|
||
grunt.utils.async.forEachSeries( files, function( fileName, fileDone ) { | ||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. menuOrder not menu_order. We're talking about the exposed API , not the underlying API. |
||
// 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 ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the declaration to the top of the function. |
||
if ( menuOrder ) { | ||
post.menuOrder = menuOrder; | ||
} else { | ||
post.postStatus = "draft"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
grunt.verbose.write( "Processing " + fileName + "..." ); | ||
delete post.content; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
grunt.utils._
instead.