@@ -48,77 +48,75 @@ function sanitize(string) {
48
48
}
49
49
50
50
function processBlock ( block , options , json ) {
51
- var blockPromise = Q . defer ( ) ;
52
-
53
- kss . parse ( block . kss , options , function ( err , styleguide ) {
54
- var section ,
55
- blockStyles ;
56
- if ( err ) {
57
- new PluginError ( PLUGIN_NAME , 'Error parsing' , err ) ;
58
- blockPromise . resolve ( ) ;
59
- return false ;
60
- } else {
61
- section = jsonSections ( styleguide . section ( ) ) ;
62
-
63
- if ( section . length > 0 ) {
64
- if ( section . length > 1 ) {
65
- console . warn ( 'Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.' ) ;
51
+ return Q . Promise ( function ( resolve , reject ) {
52
+ kss . parse ( block . kss , options , function ( err , styleguide ) {
53
+ var section ,
54
+ blockStyles ;
55
+ if ( err ) {
56
+ console . error ( ' error processing kss block' , err ) ;
57
+ reject ( err ) ;
58
+ return false ;
59
+ } else {
60
+ section = jsonSections ( styleguide . section ( ) ) ;
61
+
62
+ if ( section . length > 0 ) {
63
+ if ( section . length > 1 ) {
64
+ console . warn ( 'Warning: KSS splitter returned more than 1 KSS block. Styleguide might not be properly generated.' ) ;
65
+ }
66
+ blockStyles = trimLinebreaks ( block . code ) ;
67
+
68
+ // Add related CSS to section
69
+ if ( blockStyles && blockStyles !== '' ) {
70
+ section [ 0 ] . css = blockStyles ;
71
+ }
72
+ json . sections = json . sections . concat ( section ) ;
66
73
}
67
- blockStyles = trimLinebreaks ( block . code ) ;
68
-
69
- // Add related CSS to section
70
- if ( blockStyles && blockStyles !== '' ) {
71
- section [ 0 ] . css = blockStyles ;
72
- }
73
- json . sections = json . sections . concat ( section ) ;
74
+ resolve ( ) ;
74
75
}
75
- blockPromise . resolve ( ) ;
76
- }
76
+ } ) ;
77
77
} ) ;
78
- return blockPromise ;
79
78
}
80
79
81
80
function processFile ( contents , syntax , options , json ) {
82
- var filePromise = Q . defer ( ) ,
83
- blockPromises = [ ] ,
84
- splittedFile = kssSplitter . getBlocks ( contents , syntax ) ;
85
-
86
- // Process every block in the current file
87
- splittedFile . forEach ( function ( block ) {
88
- blockPromises . push ( processBlock ( block , options , json ) ) ;
89
- } ) ;
90
-
91
- Q . all ( blockPromises ) . then ( function ( ) {
92
- // All blocks are processed, resolve file promise
93
- filePromise . resolve ( ) ;
81
+ return Q . Promise ( function ( resolve , reject ) {
82
+ try {
83
+ var blockPromises = [ ] ,
84
+ blocks = kssSplitter . getBlocks ( contents , syntax ) ;
85
+
86
+ // Process every block in the current file
87
+ blocks . forEach ( function ( block ) {
88
+ blockPromises . push ( processBlock ( block , options , json ) ) ;
89
+ } ) ;
90
+ } catch ( err ) {
91
+ reject ( err ) ;
92
+ }
93
+ Q . all ( blockPromises ) . then ( resolve ) ;
94
94
} ) ;
95
-
96
- return filePromise ;
97
95
}
98
96
99
97
module . exports = {
100
98
// Parse node-kss object ( {'file.path': 'file.contents.toString('utf8'}' )
101
99
parseKSS : function ( files , options ) {
102
- var parsePromise = Q . defer ( ) ,
103
- json = {
104
- sections : [ ]
105
- } ,
106
- filePromises = [ ] ,
107
- fileKeys = Object . keys ( files ) ;
108
-
109
- fileKeys . forEach ( function ( filePath ) {
110
- var contents = files [ filePath ] ,
111
- syntax = path . extname ( filePath ) . substring ( 1 ) ;
112
- filePromises . push ( processFile ( contents , syntax , options , json ) ) ;
113
- } ) ;
114
-
115
- Q . all ( filePromises ) . then ( function ( ) {
116
- // All files are processed. Sort results and call main promise
117
- json . sections = _ . sortBy ( json . sections , function ( section ) {
118
- return section . reference ;
100
+ return Q . Promise ( function ( resolve , reject ) {
101
+ var json = {
102
+ sections : [ ]
103
+ } ,
104
+ filePromises = [ ] ,
105
+ fileKeys = Object . keys ( files ) ;
106
+
107
+ fileKeys . forEach ( function ( filePath ) {
108
+ var contents = files [ filePath ] ,
109
+ syntax = path . extname ( filePath ) . substring ( 1 ) ;
110
+ filePromises . push ( processFile ( contents , syntax , options , json ) ) ;
119
111
} ) ;
120
- parsePromise . resolve ( json ) ;
112
+
113
+ Q . all ( filePromises ) . then ( function ( ) {
114
+ // All files are processed. Sort results and call main promise
115
+ json . sections = _ . sortBy ( json . sections , function ( section ) {
116
+ return section . reference ;
117
+ } ) ;
118
+ resolve ( json ) ;
119
+ } ) . catch ( reject ) ;
121
120
} ) ;
122
- return parsePromise . promise ;
123
121
}
124
- }
122
+ } ;
0 commit comments