Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use grunt.utils._ instead.

},
"keywords": [
"gruntplugin"
Expand Down
56 changes: 54 additions & 2 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ) {
Copy link
Member

Choose a reason for hiding this comment

The 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 ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the only line in the try block.

order.forEach( function(chapter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.forEach(function( chapter ) {

var article, title;

if ( _.isObject( chapter ) ) {
title = Object.keys( chapter )[ 0 ];
map[ title ] = ++index;

chapter[ title ].forEach( function( article ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.forEach(function(

map[ title + "/" + article ] = ++index;
});
} else {
map[ title ] = ++index;
}
});
return map;
} catch( error ) {
grunt.warn( "Invalid order file: " + orderFile );
taskDone();
return null;
}


Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The 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 );
Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The 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 ];
Copy link
Member

Choose a reason for hiding this comment

The 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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be post.status?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so... the field in the db is post_status, setting postStatus appears to be working:

see

}
}

grunt.verbose.write( "Processing " + fileName + "..." );
delete post.content;
Expand Down