Skip to content

Commit 38c4bf1

Browse files
committed
Tasks: Change build-pages to build-posts and handle all post types
Changes preprocessPost() export to postPreprocessors hash. Allows specifying the target file name via a preprocessor. This was needed by meetings.jquery.org to support the custom directory structure. Fixes gh-35
1 parent c33520a commit 38c4bf1

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ grunt.registerTask( "lint", [ "xmllint" ] );
2323
This is a task list that must be defined per site, containing all of the build steps. A simple site would have the following task list:
2424

2525
```
26-
grunt.registerTask( "build", [ "build-pages", "build-resources" ] );
26+
grunt.registerTask( "build", [ "build-posts", "build-resources" ] );
2727
```
2828

29-
### build-pages
29+
### build-posts
3030

31-
This multi-task takes a list of html or markdown files, copies them to `[wordpress.dir]/posts/page/`, processes `@partial` entries and highlights the syntax in each.
31+
This multi-task takes a list of html or markdown files, copies them to `[wordpress.dir]/posts/[post-type]/`, processes `@partial` entries and highlights the syntax in each. The keys are the post types for each set of posts.
3232

33-
See the [`preprocessPost()` export](#preprocesspost-post-filename-callback-) for a hook to implement custom processing.
33+
See the [`postPreprocessors` export](#postpreprocessors) for a hook to implement custom processing.
3434

3535
#### markdown
3636

@@ -124,12 +124,21 @@ Syntax highlights content.
124124

125125
* `content` String: The string the highlight.
126126

127-
### preprocessPost( post, fileName, callback )
127+
### postPreprocessors
128128

129-
Hook for modifying the posts before they're processed in the [`build-pages`](#build-pages) task.
129+
Hooks for modifying the posts before they're processed in the [`build-posts`](#build-posts) task.
130+
131+
`postPreprocessors` is a hash of preprocessors, where the key is the post type and the value is a function which modifies the post.
132+
133+
The functions must be in the form of:
134+
`function( post, fileName, callback )`
130135

131136
* `post` Object: The post being processed.
132137
* `fileName` String: The name of the file used to generate the post object.
133138
* `callback` function( error, post ): Callback to invoke after modifying the post.
134139
* `error`: An `Error` instance, if there was an error while modifying the post.
135140
* `post` The modified post.
141+
142+
By default, posts are placed in the `[wordpress.dir]/[post-type]` directory using the same relative path and file name as the source file. The relative path can be changed by setting the `fileName` property on the post.
143+
144+
If a preprocessor is not defined for the given post type, then the `_default` preprocessor will be used.

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var syntaxHighlight = require( "./lib/highlight" );
22

33
exports.syntaxHighlight = syntaxHighlight;
44

5-
exports.preprocessPost = function( post, fileName, callback ) {
6-
callback( null, post );
5+
exports.postPreprocessors = {
6+
_default: function( post, fileName, callback ) {
7+
callback( null, post );
8+
}
79
};

tasks/build.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ grunt.registerTask( "lint", [] );
2020

2121
grunt.registerTask( "build-wordpress", [ "check-modules", "lint", "clean-dist", "build" ] );
2222

23-
grunt.registerMultiTask( "build-pages", "Process html and markdown files as pages, include @partials and syntax higlight code snippets", function() {
23+
grunt.registerMultiTask( "build-posts", "Process html and markdown files as posts", function() {
2424
var task = this,
2525
taskDone = task.async(),
2626
wordpressClient = wordpress.createClient( grunt.config( "wordpress" ) ),
27-
targetDir = grunt.config( "wordpress.dir" ) + "/posts/page/";
27+
postType = this.target,
28+
preprocessor = mainExports.postPreprocessors[ postType ] ||
29+
mainExports.postPreprocessors._default,
30+
targetDir = grunt.config( "wordpress.dir" ) + "/posts/" + postType + "/";
2831

2932
grunt.file.mkdir( targetDir );
3033

@@ -34,7 +37,7 @@ grunt.registerMultiTask( "build-pages", "Process html and markdown files as page
3437
return callback( error );
3538
}
3639

37-
mainExports.preprocessPost( post, fileName, callback );
40+
preprocessor( post, fileName, callback );
3841
});
3942
}
4043

@@ -49,9 +52,10 @@ grunt.registerMultiTask( "build-pages", "Process html and markdown files as page
4952
var content = post.content,
5053
fileType = /\.(\w+)$/.exec( fileName )[ 1 ],
5154
targetFileName = targetDir +
52-
fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ) + ".html";
55+
( post.fileName || fileName.replace( /^.+?\/(.+)\.\w+$/, "$1" ) + ".html" );
5356

5457
delete post.content;
58+
delete post.fileName;
5559

5660
// Convert markdown to HTML
5761
if ( fileType === "md" ) {

0 commit comments

Comments
 (0)