|
| 1 | +var _ = require('lodash'); |
| 2 | +var log = require('dgeni').log; |
| 3 | +var path = require('canonical-path'); |
| 4 | + |
| 5 | +var typeTransform = { |
| 6 | + markdown: 'md' |
| 7 | +}; |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + name: 'demos', |
| 11 | + description: 'Output demos to their files on ionic-demo website', |
| 12 | + runAfter: ['files-read'], |
| 13 | + runBefore: ['processing-docs'], |
| 14 | + process: function(docs, config) { |
| 15 | + |
| 16 | + var contentsFolder = config.rendering.contentsFolder; |
| 17 | + var assetOutputPath = path.join(contentsFolder, '${component}/${name}/${fileName}'); |
| 18 | + |
| 19 | + var pages = []; |
| 20 | + |
| 21 | + var demos = _(docs) |
| 22 | + .filter('yaml') |
| 23 | + .groupBy(function(doc) { |
| 24 | + return doc.yaml.component + '-' + doc.yaml.name; |
| 25 | + }) |
| 26 | + .map(function(fragmentsForId, id) { |
| 27 | + |
| 28 | + var demoData = { |
| 29 | + files: [] |
| 30 | + }; |
| 31 | + fragmentsForId.forEach(function(fragment) { |
| 32 | + var doc = fragment.yaml; |
| 33 | + if (!doc.name || !doc.component) { |
| 34 | + log.error('Doc ' + fragment.filePath + |
| 35 | + ' expects yaml keys "name" and "component"!'); |
| 36 | + } |
| 37 | + |
| 38 | + doc.id = doc.component + '-' + doc.name; |
| 39 | + doc.fileType = typeTransform[fragment.fileType] || fragment.fileType; |
| 40 | + doc.fileName = fragment.fileName; |
| 41 | + doc.contents = fragment.contents; |
| 42 | + doc.extension = doc.fileType.replace(/^\./,''); |
| 43 | + |
| 44 | + doc.template = 'asset.contents.template', |
| 45 | + doc.outputPath = _.template(assetOutputPath, doc); |
| 46 | + |
| 47 | + demoData.files.push(doc); |
| 48 | + pages.push(doc); |
| 49 | + }); |
| 50 | + |
| 51 | + var firstDoc = demoData.files[0]; |
| 52 | + |
| 53 | + var indexOutputPath = _.template(assetOutputPath, _.assign({}, firstDoc, { |
| 54 | + fileName: 'index.html' |
| 55 | + })); |
| 56 | + var appOutputPath = _.template(assetOutputPath, _.assign({}, firstDoc, { |
| 57 | + fileName: 'index-ionic-demo-app.js' |
| 58 | + }, demoData)); |
| 59 | + |
| 60 | + |
| 61 | + demoData.files = _.groupBy(demoData.files, 'extension'); |
| 62 | + demoData.id = firstDoc.id; |
| 63 | + demoData.name = firstDoc.name; |
| 64 | + demoData.component = firstDoc.component; |
| 65 | + demoData.href = '/' + _.template(assetOutputPath, _.assign({}, firstDoc, { fileName: '' })); |
| 66 | + |
| 67 | + pages.push({ |
| 68 | + template: 'index.template.html', |
| 69 | + demoData: demoData, |
| 70 | + outputPath: indexOutputPath |
| 71 | + }); |
| 72 | + pages.push({ |
| 73 | + template: 'app.template.js', |
| 74 | + demoData: demoData, |
| 75 | + outputPath: appOutputPath |
| 76 | + }); |
| 77 | + |
| 78 | + return demoData; |
| 79 | + |
| 80 | + }) |
| 81 | + .value(); |
| 82 | + |
| 83 | + pages.push({ |
| 84 | + template: 'pages-data.template.js', |
| 85 | + demos: demos, |
| 86 | + outputPath: path.join(contentsFolder, 'pages-data.js') |
| 87 | + }); |
| 88 | + pages.push({ |
| 89 | + template: 'index.template.html', |
| 90 | + outputPath: path.join(contentsFolder, 'index.html') |
| 91 | + }); |
| 92 | + pages.push({ |
| 93 | + template: 'app.template.js', |
| 94 | + outputPath: path.join(contentsFolder, 'index-ionic-demo-app.js') |
| 95 | + }); |
| 96 | + |
| 97 | + return pages; |
| 98 | +} |
| 99 | +}; |
0 commit comments