@@ -27,6 +27,9 @@ var git = require('gulp-git');
2727var bump = require ( 'gulp-bump' ) ;
2828var tagVersion = require ( 'gulp-tag-version' ) ;
2929var conventionalChangelog = require ( 'gulp-conventional-changelog' ) ;
30+ var ghPages = require ( 'gulp-gh-pages' ) ;
31+ var through = require ( 'through2' ) ;
32+ var tap = require ( 'gulp-tap' ) ;
3033
3134var SRC = 'src/' ;
3235var BUILD = 'build/' ;
@@ -40,6 +43,10 @@ gulp.task('clean', function() {
4043 return del ( [ BUILD + '**' , BUILD_DOCS + '**' , DIST + '**' ] ) ;
4144} ) ;
4245
46+ gulp . task ( 'clean:docs' , function ( ) {
47+ return del ( [ BUILD_DOCS + '**' ] ) ;
48+ } ) ;
49+
4350gulp . task ( 'copy' , function ( ) {
4451 return gulp . src ( [ SRC + '**/*.+(html|js|woff)' , '!' + SRC + '**/example.html' ] )
4552 . pipe ( changed ( BUILD ) )
@@ -158,9 +165,123 @@ gulp.task('docs', function() {
158165 . pipe ( marked ( ) . on ( 'error' , console . log ) )
159166 . pipe ( wrap ( { src :"./docs/article_template.html" } , { } , { engine :"hogan" } ) . on ( 'error' , console . log ) )
160167 . pipe ( gulp . dest ( BUILD_DOCS ) ) ;
168+
169+ // return merge(moduleStream, articleStream);
161170 return articles ;
162171} ) ;
163172
173+ gulp . task ( 'docs:modules' , function ( ) {
174+ function mergeDocArray ( doc , behavior ) {
175+ var p = { } ;
176+ if ( ! behavior ) {
177+ return doc ;
178+ }
179+ if ( ! doc && behavior ) {
180+ return behavior ;
181+ }
182+ behavior . forEach ( function ( obj ) {
183+ if ( obj . name ) p [ obj . name ] = obj ;
184+ else if ( obj . type ) p [ obj . type ] = obj ;
185+ } ) ;
186+ doc . forEach ( function ( obj ) {
187+ if ( obj . name ) p [ obj . name ] = obj ;
188+ else if ( obj . type ) p [ obj . type ] = obj ;
189+ } ) ;
190+ return Object . keys ( p ) . map ( function ( key ) {
191+ return p [ key ] ;
192+ } ) ;
193+ }
194+
195+ // Ad-hoc plugin for injecting behaviors
196+ function injectBehaviorDocs ( behaviorsMap ) {
197+ return through . obj ( function ( file , enc , cb ) {
198+ if ( ! file . isBuffer ( ) ) this . emit ( 'error' , new gutil . PluginError ( 'Buffer required' ) ) ;
199+
200+ var moduleDoc = JSON . parse ( file . contents ) ,
201+ moduleBehaviors = moduleDoc . behaviors ;
202+
203+ // Merge behaviors docs with component docs
204+ moduleBehaviors . forEach ( function ( key ) {
205+ var behavior = behaviorsMap [ key ] ;
206+ if ( moduleDoc && behavior ) {
207+ moduleDoc . attributes = mergeDocArray ( moduleDoc . attributes , behavior . attributes ) ;
208+ moduleDoc . methods = mergeDocArray ( moduleDoc . methods , behavior . methods ) ;
209+ moduleDoc . events = mergeDocArray ( moduleDoc . events , behavior . events ) ;
210+ }
211+ } ) ;
212+
213+ file . contents = new Buffer ( JSON . stringify ( moduleDoc ) , enc ) ;
214+ this . push ( file ) ;
215+ cb ( ) ;
216+ } ) ;
217+ }
218+
219+ // Ad-hoc plugin for injecting everything else
220+ function injectDocsMeta ( pkg , moduleList , articleList , articleMap ) {
221+ return through . obj ( function ( file , enc , cb ) {
222+ var moduleDoc = JSON . parse ( file . contents ) ;
223+
224+ // Inject metadata
225+ moduleDoc . revision = pkg . version ;
226+ moduleDoc . modules = moduleList ;
227+ moduleDoc . articleList = articleList ;
228+ moduleDoc . articleMap = articleMap ;
229+
230+ file . contents = new Buffer ( JSON . stringify ( moduleDoc ) , enc ) ;
231+ this . push ( file ) ;
232+ cb ( ) ;
233+ } ) ;
234+ }
235+
236+ var pkg = getPkgInfo ( ) ;
237+
238+ // Create moduleList
239+ var moduleList = [ ] ;
240+
241+ // Create behaviorsMap
242+ var behaviors = glob . sync ( SRC + 'shared/behaviors/*.json' ) ;
243+ var behaviorsMap = { } ;
244+ behaviors . forEach ( function ( behavior ) {
245+ var behaviorKey = behavior . replace ( SRC + 'shared/behaviors/' , '' )
246+ . replace ( '.json' , '' )
247+ . toLowerCase ( ) ;
248+ behaviorsMap [ behaviorKey ] = JSON . parse ( fs . readFileSync ( behavior ) ) ;
249+ } ) ;
250+
251+ // Create articleList and articleMap
252+ var articleList = [ ] ;
253+ var articleMap = { } ;
254+
255+ // Finally open the stream
256+ gulp . src ( SRC + 'mm-*/doc.json' )
257+ . pipe ( injectBehaviorDocs ( behaviorsMap ) )
258+ . pipe ( injectDocsMeta ( pkg , moduleList , articleList , articleMap ) )
259+ . pipe ( tap ( function ( file , t ) {
260+ var moduleDoc = JSON . parse ( file . contents ) ;
261+ var curried = wrap . bind (
262+ { src :"./docs/component_template.html" } ,
263+ moduleDoc ,
264+ { engine :"hogan" }
265+ ) ;
266+ return t . through ( curried , [ ] ) ;
267+ } ) )
268+ . pipe ( rename ( function ( path ) {
269+ path . basename = path . dirname ;
270+ path . dirname = '' ;
271+ path . extname = '.html' ;
272+ } ) )
273+ . pipe ( gulp . dest ( BUILD_DOCS ) )
274+ . on ( 'error' , console . log ) ;
275+ } ) ;
276+
277+ gulp . task ( 'gh-pages' , function ( ) {
278+ var pkg = getPkgInfo ( ) ;
279+ return gulp . src ( BUILD_DOCS + '**/*' )
280+ . pipe ( ghPages ( {
281+ message : 'docs updates v' + pkg . version
282+ } ) ) ;
283+ } ) ;
284+
164285/** LIVE **/
165286
166287gulp . task ( 'watch' , function ( ) {
0 commit comments