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
Changes from all commits
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
62 changes: 58 additions & 4 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,74 @@ 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 ) {
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.

} catch( error ) {
grunt.warn( "Invalid order file: " + orderFile );
taskDone();
return null;
}

order.forEach(function(chapter) {
var article, title;

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.

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,
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 );
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 ],
targetFileName = targetDir + fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ) + ".html";
targetSlug = fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ),
targetFileName = targetDir + targetSlug + ".html";

// 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 ) {
menuOrder = orderMap[ targetSlug ];
if ( menuOrder ) {
post.menuOrder = menuOrder;
} else {
post.status = "draft";
}
}

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