OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'package:barback/barback.dart'; | |
6 import 'package:path/path.dart' as p; | |
7 | |
8 import 'dart:async'; | |
9 | |
10 class MakeBook extends AggregateTransformer { | |
11 // All transformers need to implement "asPlugin" to let Pub know that they | |
12 // are transformers. | |
13 MakeBook.asPlugin(); | |
14 | |
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, | |
17 // or null for those that you do not want to handle. | |
18 classifyPrimary(AssetId id) { | |
19 // Only process assets where the filename ends with "recipe.html". | |
20 if (!id.path.endsWith('recipe.html')) return null; | |
21 | |
22 // Return the path string, minus the recipe itself. | |
23 // This is where the output asset will be written. | |
24 return p.url.dirname(id.path); | |
25 } | |
26 | |
27 // Implement the apply method to process the assets and create the | |
28 // output asset. | |
29 Future apply(AggregateTransform transform) { | |
30 var buffer = new StringBuffer()..write('<html><body>'); | |
31 | |
32 return transform.primaryInputs.toList().then((assets) { | |
33 // The order of [transform.primaryInputs] is not guaranteed | |
34 // to be stable across multiple runs of the transformer. | |
35 // Therefore, we alphabetically sort the assets by ID string. | |
36 assets.sort((x, y) => x.id.compareTo(y.id)); | |
37 return Future.wait(assets.map((asset) { | |
38 return asset.readAsString().then((content) { | |
39 buffer.write(content); | |
40 buffer.write('<hr>'); | |
41 }); | |
42 })); | |
43 }).then((_) { | |
44 buffer.write('</body></html>'); | |
45 // Write the output back to the same directory, | |
46 // in a file named recipes.html. | |
47 var id = new AssetId(transform.package, | |
48 p.url.join(transform.key, ".recipes.html")); | |
49 transform.addOutput(new Asset.fromString(id, buffer.toString())); | |
50 }); | |
51 } | |
52 } | |
53 | |
OLD | NEW |