forked from jquery/api.jquery.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrunt.js
132 lines (114 loc) · 3.66 KB
/
grunt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*jshint node:true */
module.exports = function( grunt ) {
var // files
pageFiles = grunt.file.expandFiles( "pages/*.html" ),
entryFiles = grunt.file.expandFiles( "entries/*.xml" ),
noteFiles = grunt.file.expandFiles( "notes/*.xml" ),
path = require( "path" ),
rimraf = require( "rimraf" ),
xmlFiles = [].concat( entryFiles, noteFiles, "cat2tax.xsl", "categories.xml", "entries2html.xsl", "xml2json.xsl" );
grunt.loadNpmTasks( "grunt-clean" );
grunt.loadNpmTasks( "grunt-wordpress" );
grunt.loadNpmTasks( "grunt-jquery-content" );
grunt.initConfig({
clean: {
folder: "dist"
},
lint: {
grunt: "grunt.js"
},
xmllint: {
all: xmlFiles
},
xmltidy: {
all: [].concat( entryFiles, noteFiles, "categories.xml" )
},
"build-pages": {
all: grunt.file.expandFiles( "pages/*" )
},
"build-xml-entries": {
all: entryFiles
},
"build-resources": {
all: grunt.file.expandFiles( "resources/*" )
},
wordpress: grunt.utils._.extend({
dir: "dist/wordpress"
}, grunt.file.readJSON( "config.json" ) )
});
grunt.registerMultiTask( "build-pages", "Process html files as pages", function() {
var task = this,
taskDone = task.async(),
files = this.data,
targetDir = grunt.config( "wordpress.dir" ) + "/posts/page/";
grunt.file.mkdir( targetDir );
grunt.utils.async.forEachSeries( files, function( fileName, fileDone ) {
var targetFileName = targetDir + fileName.replace( /^.+?\//, "" );
grunt.verbose.write( "Processing " + fileName + "..." );
grunt.file.copy( fileName, targetFileName );
fileDone();
}, function() {
if ( task.errorCount ) {
grunt.warn( "Task \"" + task.name + "\" failed." );
taskDone();
return;
}
grunt.log.writeln( "Built " + files.length + " pages." );
taskDone();
});
});
grunt.registerMultiTask( "build-xml-entries", "Process API xml files with xsl", function() {
var task = this,
taskDone = task.async(),
files = this.data,
// TODO make `entry` a custom post type instead of (ab)using `post`?
targetDir = grunt.config( "wordpress.dir" ) + "/posts/post/";
grunt.file.mkdir( targetDir );
grunt.utils.async.forEachSeries( files, function( fileName, fileDone ) {
grunt.verbose.write( "Transforming (pass 1: preproc-xinclude.xsl) " + fileName + "..." );
grunt.utils.spawn({
cmd: "xsltproc",
args: [ "preproc-xinclude.xsl", fileName ]
}, function( err, pass1result ) {
if ( err ) {
grunt.verbose.error();
grunt.log.error( err );
fileDone();
return;
}
grunt.verbose.ok();
var targetXMLFileName = "entries_tmp/" + path.basename( fileName );
grunt.file.write( targetXMLFileName, pass1result );
grunt.verbose.write( "Transforming (pass 2: entries2html.xsl) " + fileName + "..." );
grunt.utils.spawn({
cmd: "xsltproc",
args: [ "--xinclude", "entries2html.xsl", targetXMLFileName ]
}, function( err, pass2result ) {
if ( err ) {
grunt.verbose.error();
grunt.log.error( err );
fileDone();
return;
}
grunt.verbose.ok();
var targetHTMLFileName = targetDir + path.basename( fileName );
targetHTMLFileName = targetHTMLFileName.substr( 0, targetHTMLFileName.length - "xml".length ) + "html";
grunt.file.write( targetHTMLFileName, pass2result );
fileDone();
});
});
}, function() {
if ( task.errorCount ) {
grunt.warn( "Task \"" + task.name + "\" failed." );
taskDone();
return;
}
rimraf.sync( "entries_tmp" );
grunt.log.writeln( "Built " + files.length + " entries." );
taskDone();
});
});
grunt.registerTask( "default", "build-wordpress" );
grunt.registerTask( "build-wordpress", "clean lint xmllint build-pages build-xml-entries build-xml-categories build-resources" );
grunt.registerTask( "tidy", "xmllint xmltidy" );
};