| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:barback/barback.dart'; | 5 import 'package:barback/barback.dart'; |
| 6 import 'package:path/path.dart' as p; | 6 import 'package:path/path.dart' as p; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 class MakeBook extends AggregateTransformer { | 10 class MakeBook extends AggregateTransformer { |
| 11 // All transformers need to implement "asPlugin" to let Pub know that they | 11 // All transformers need to implement "asPlugin" to let Pub know that they |
| 12 // are transformers. | 12 // are transformers. |
| 13 MakeBook.asPlugin(); | 13 MakeBook.asPlugin(); |
| 14 | 14 |
| 15 // Implement the classifyPrimary method to claim any assets that you want | 15 // Implement the classifyPrimary method to claim any assets that you want |
| 16 // to handle. Return a value for the assets you want to handle, | 16 // to handle. Return a value for the assets you want to handle, |
| 17 // or null for those that you do not want to handle. | 17 // or null for those that you do not want to handle. |
| 18 classifyPrimary(AssetId id) { | 18 classifyPrimary(AssetId id) { |
| 19 | |
| 20 // Only process assets where the filename ends with "recipe.html". | 19 // Only process assets where the filename ends with "recipe.html". |
| 21 if (!id.path.endsWith('recipe.html')) return null; | 20 if (!id.path.endsWith('recipe.html')) return null; |
| 22 | 21 |
| 23 // Return the path string, minus the recipe itself. | 22 // Return the path string, minus the recipe itself. |
| 24 // This is where the output asset will be written. | 23 // This is where the output asset will be written. |
| 25 return p.url.dirname(id.path); | 24 return p.url.dirname(id.path); |
| 26 } | 25 } |
| 27 | 26 |
| 28 // Implement the apply method to process the assets and create the | 27 // Implement the apply method to process the assets and create the |
| 29 // output asset. | 28 // output asset. |
| 30 Future apply(AggregateTransform transform) async { | 29 Future apply(AggregateTransform transform) async { |
| 31 var buffer = new StringBuffer()..write('<html><body>'); | 30 var buffer = new StringBuffer()..write('<html><body>'); |
| 32 | 31 |
| 33 var assets = await transform.primaryInputs.toList(); | 32 var assets = await transform.primaryInputs.toList(); |
| 34 assets.sort((x, y) => x.id.compareTo(y.id)); | 33 assets.sort((x, y) => x.id.compareTo(y.id)); |
| 35 for (var asset in assets) { | 34 for (var asset in assets) { |
| 36 var content = await asset.readAsString(); | 35 var content = await asset.readAsString(); |
| 37 buffer.write(content); | 36 buffer.write(content); |
| 38 buffer.write('<hr>'); | 37 buffer.write('<hr>'); |
| 39 } | 38 } |
| 40 buffer.write('</body></html>'); | 39 buffer.write('</body></html>'); |
| 41 // Write the output back to the same directory, | 40 // Write the output back to the same directory, |
| 42 // in a file named recipes.html. | 41 // in a file named recipes.html. |
| 43 var id = new AssetId( | 42 var id = new AssetId( |
| 44 transform.package, p.url.join(transform.key, "recipes.html")); | 43 transform.package, p.url.join(transform.key, "recipes.html")); |
| 45 transform.addOutput(new Asset.fromString(id, buffer.toString())); | 44 transform.addOutput(new Asset.fromString(id, buffer.toString())); |
| 46 } | 45 } |
| 47 } | 46 } |
| OLD | NEW |