@@ -5,9 +5,17 @@ var // modules
55 fs = require ( "fs" ) ,
66 path = require ( "path" ) ,
77 spawn = require ( "child_process" ) . spawn ,
8+ libxmljs = require ( "libxmljs" ) ,
89
910 // files
10- entryFiles = grunt . file . expandFiles ( "entries/*.xml" ) ;
11+ entryFiles = grunt . file . expandFiles ( "entries/*.xml" ) ,
12+ categoryFiles = grunt . file . expandFiles ( "categories/*.xml" ) ,
13+
14+ xmlFiles = [ ] . concat ( entryFiles , categoryFiles ) ;
15+
16+ function pathSlug ( fileName ) {
17+ return path . basename ( fileName , path . extname ( fileName ) ) ;
18+ }
1119
1220grunt . loadNpmTasks ( "grunt-wordpress" ) ;
1321
@@ -16,15 +24,15 @@ grunt.initConfig({
1624 grunt : "grunt.js"
1725 } ,
1826 xmllint : {
19- all : entryFiles
27+ all : xmlFiles
2028 } ,
2129 wordpress : grunt . file . readJSON ( "config.json" )
2230} ) ;
2331
2432grunt . registerTask ( "xmllint" , function ( ) {
2533 var task = this ,
2634 taskDone = task . async ( ) ;
27- grunt . utils . async . forEachSeries ( entryFiles , function ( fileName , fileDone ) {
35+ grunt . utils . async . forEachSeries ( xmlFiles , function ( fileName , fileDone ) {
2836 grunt . verbose . write ( "Linting " + fileName + "..." ) ;
2937 grunt . utils . spawn ( {
3038 cmd : "xmllint" ,
@@ -74,17 +82,8 @@ grunt.registerTask( "build-entries", function() {
7482 grunt . verbose . ok ( ) ;
7583 targetFileName = targetDir + path . basename ( fileName ) ;
7684 targetFileName = targetFileName . substr ( 0 , targetFileName . length - "xml" . length ) + "html" ;
77- grunt . verbose . write ( "Writing " + targetFileName + "..." ) ;
78- fs . writeFile ( targetFileName , result , function ( err ) {
79- if ( err ) {
80- grunt . verbose . error ( ) ;
81- grunt . log . error ( err ) ;
82- fileDone ( ) ;
83- return ;
84- }
85- grunt . verbose . ok ( ) ;
86- fileDone ( ) ;
87- } ) ;
85+ grunt . file . write ( targetFileName , result ) ;
86+ fileDone ( ) ;
8887 } ) ;
8988 } , function ( ) {
9089 if ( task . errorCount ) {
@@ -97,8 +96,110 @@ grunt.registerTask( "build-entries", function() {
9796 } ) ;
9897} ) ;
9998
99+ grunt . registerTask ( "build-categories" , function ( ) {
100+ var task = this ,
101+ taskDone = task . async ( ) ,
102+ categories = { } ,
103+ outFilename = "dist/taxonomies.json" ;
104+
105+ grunt . utils . async . forEachSeries ( categoryFiles , function ( fileName , fileDone ) {
106+ var xml = grunt . file . read ( fileName ) ,
107+ xmlDoc = libxmljs . parseXmlString ( xml ) ,
108+ id = xmlDoc . get ( "//category/@id" ) . text ( ) ,
109+ parent = xmlDoc . get ( "//category/@parent" ) . text ( ) ,
110+ name = xmlDoc . get ( "//category/@name" ) . text ( ) ,
111+ slug = xmlDoc . get ( "//category/@slug" ) . text ( ) ,
112+ desc = xmlDoc . get ( "//category/desc" ) . text ( ) ,
113+ fileNameSlug = pathSlug ( fileName ) ,
114+ errors = [ ] ;
115+
116+ grunt . verbose . write ( "Validating " + fileName + "..." ) ;
117+ if ( slug !== fileNameSlug ) {
118+ errors . push ( "slug attribute (" + slug + ") doesn't match filename slug (" + fileNameSlug + ")" ) ;
119+ }
120+ if ( errors . length ) {
121+ grunt . verbose . error ( ) ;
122+ errors . forEach ( function ( error ) {
123+ grunt . log . error ( error ) ;
124+ } ) ;
125+ fileDone ( ) ;
126+ return ;
127+ }
128+ grunt . verbose . ok ( ) ;
129+
130+ categories [ id ] = {
131+ id : id ,
132+ name : name ,
133+ parent : parent ,
134+ slug : slug ,
135+ desc : desc ,
136+ children : [ ]
137+ } ;
138+
139+ fileDone ( ) ;
140+ } , function ( ) {
141+ if ( task . errorCount ) {
142+ grunt . warn ( "Task \"" + task . name + "\" failed." ) ;
143+ taskDone ( ) ;
144+ return ;
145+ }
146+
147+ var id , category , parent ,
148+ taxonomies = {
149+ "category" : [
150+ { "name" : "Uncategorized" }
151+ ]
152+ } ;
153+
154+ function catTree ( id ) {
155+ var category = categories [ id ] ;
156+ var cat = {
157+ name : category . name
158+ } ;
159+ if ( category . slug . length ) {
160+ cat . slug = category . slug ;
161+ }
162+ if ( category . desc . length ) {
163+ cat . description = category . desc ;
164+ }
165+ if ( category . children . length ) {
166+ cat . children = [ ] ;
167+ category . children . forEach ( function ( childId ) {
168+ cat . children . push ( catTree ( childId ) ) ;
169+ } ) ;
170+ }
171+ return cat ;
172+ }
173+
174+ for ( id in categories ) {
175+ category = categories [ id ] ;
176+ parent = categories [ category . parent ] ;
177+ if ( parent ) {
178+ grunt . verbose . write ( "Making " + category . name + " a child category of " + parent . name + "..." ) ;
179+ parent . children . push ( id ) ;
180+ } else {
181+ grunt . verbose . write ( "Making " + category . name + " a top-level category..." ) ;
182+ category . parent = undefined ;
183+ }
184+ grunt . verbose . ok ( ) ;
185+ }
186+
187+ for ( id in categories ) {
188+ category = categories [ id ] ;
189+ if ( ! category . parent ) {
190+ taxonomies . category . push ( catTree ( id ) ) ;
191+ }
192+ }
193+
194+ grunt . file . write ( outFilename , JSON . stringify ( taxonomies ) ) ;
195+
196+ grunt . log . writeln ( "Built " + categoryFiles . length + " categories to " + outFilename + "." ) ;
197+ taskDone ( ) ;
198+ } ) ;
199+ } ) ;
200+
100201grunt . registerTask ( "default" , "build" ) ;
101- grunt . registerTask ( "build" , "lint xmllint build-entries" ) ;
202+ grunt . registerTask ( "build" , "lint xmllint build-entries build-categories " ) ;
102203grunt . registerTask ( "deploy" , "wordpress-deploy" ) ;
103204
104205} ;
0 commit comments