| 
									
										
										
										
											2015-08-10 11:17:02 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * dgProcessor shredMapProcessor | 
					
						
							|  |  |  |  * @description | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  | var path = require('canonical-path'); | 
					
						
							|  |  |  | var fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | module.exports = function shredMapProcessor(log, createDocMessage) { | 
					
						
							| 
									
										
										
										
											2015-08-10 11:17:02 -07:00
										 |  |  |   return { | 
					
						
							|  |  |  |     $runAfter: ['readFilesProcessor'], | 
					
						
							|  |  |  |     $runBefore: ['rendering-docs'], | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 11:17:02 -07:00
										 |  |  |     $process: function(docs) { | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |       var options = this.options; | 
					
						
							|  |  |  |       var jadeToFragMap = {}; | 
					
						
							|  |  |  |       var fragToJadeMap = {}; | 
					
						
							| 
									
										
										
										
											2015-08-11 11:55:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 11:17:02 -07:00
										 |  |  |       docs.forEach(function(doc) { | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |         var jadePath = path.join(options.jadeDir, doc.fileInfo.relativePath); | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |         var fragInfoSet = {}; | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |         doc.fragItems.forEach(function(fragItem) { | 
					
						
							|  |  |  |           var mixinPath = fragItem.mixinPath; | 
					
						
							|  |  |  |           var fullExamplePath; | 
					
						
							|  |  |  |           if ( mixinPath.indexOf('_api') >= 0) { | 
					
						
							|  |  |  |             var sourcePath = mixinPath.replace('_api/',''); | 
					
						
							|  |  |  |             fullExamplePath = path.join(options.apiExamplesDir, sourcePath); | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             fullExamplePath = path.join(options.devguideExamplesDir, mixinPath); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           var region = fragItem.region ? "-" + fragItem.region : ''; | 
					
						
							|  |  |  |           var extn = path.extname(mixinPath); | 
					
						
							|  |  |  |           var basename = path.basename(mixinPath, extn); | 
					
						
							|  |  |  |           var fragDir = path.dirname(mixinPath); | 
					
						
							|  |  |  |           var fragPath = path.join(fragDir, basename + region + extn) + '.md'; | 
					
						
							|  |  |  |           var fullFragPath =  path.join(options.fragmentsDir, fragPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |           var fragInfo = { fragPath: fullFragPath, examplePath: fullExamplePath, exists: fs.existsSync(fullFragPath) }; | 
					
						
							|  |  |  |           fragInfoSet[fragPath] = fragInfo; | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |           if (fragInfo.exists) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |             var jadePathsSet = fragToJadeMap[fragPath]; | 
					
						
							|  |  |  |             if (!jadePathsSet) { | 
					
						
							|  |  |  |               jadePathsSet = {}; | 
					
						
							|  |  |  |               fragToJadeMap[fragPath] = jadePathsSet; | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |             jadePathsSet[jadePath] = jadePath; | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |           } else { | 
					
						
							|  |  |  |             var relativePath = path.relative(".", fullFragPath); | 
					
						
							|  |  |  |             log.warn(createDocMessage('Invalid example (unable to locate fragment file: "' + relativePath + '")', doc)); | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |         jadeToFragMap[jadePath] = _.values(fragInfoSet); | 
					
						
							| 
									
										
										
										
											2015-08-10 11:17:02 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |       for (var key in fragToJadeMap) { | 
					
						
							|  |  |  |         fragToJadeMap[key] = _.keys(fragToJadeMap[key]); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |       var shredMap = { | 
					
						
							|  |  |  |         jadeToFragMap: jadeToFragMap | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!options.writeFilesEnabled) { | 
					
						
							|  |  |  |         return [ shredMap ]; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         var newDocs = [ { | 
					
						
							|  |  |  |           docType: 'xref-doc.json', | 
					
						
							|  |  |  |           json: JSON.stringify(shredMap, null, 2), | 
					
						
							|  |  |  |           outputPath: 'xref-jade.json' | 
					
						
							|  |  |  |         }, { | 
					
						
							|  |  |  |           docType: 'xref-jade.html', | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |           jadeToFragMap: jadeToFragMap, | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |           outputPath: 'xref-jade-to-frag.html' | 
					
						
							|  |  |  |         }, { | 
					
						
							|  |  |  |           docType: 'xref-frag.html', | 
					
						
							|  |  |  |           fragToJadeMap: fragToJadeMap, | 
					
						
							|  |  |  |           outputPath: 'xref-frag-to-jade.html' | 
					
						
							|  |  |  |         }]; | 
					
						
							|  |  |  |         return newDocs; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-10 11:17:02 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-11 11:55:22 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getExampleName(fragPath) { | 
					
						
							|  |  |  |   // pattern to isolate base fileName and extension from fragment name
 | 
					
						
							|  |  |  |   var rx = /(.*)\-(.*)\.(.s)/; | 
					
						
							|  |  |  |   var r = rx.exec(fragPath); | 
					
						
							|  |  |  |   if (r) { | 
					
						
							|  |  |  |     return r[1] + '.' + r[3]; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return fragPath; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |