OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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 library yaml; |
| 6 |
| 7 import 'src/loader.dart'; |
| 8 import 'src/style.dart'; |
| 9 import 'src/yaml_document.dart'; |
| 10 import 'src/yaml_exception.dart'; |
| 11 import 'src/yaml_node.dart'; |
| 12 |
| 13 export 'src/style.dart'; |
| 14 export 'src/utils.dart' show YamlWarningCallback, yamlWarningCallback; |
| 15 export 'src/yaml_document.dart'; |
| 16 export 'src/yaml_exception.dart'; |
| 17 export 'src/yaml_node.dart' hide setSpan; |
| 18 |
| 19 /// Loads a single document from a YAML string. |
| 20 /// |
| 21 /// If the string contains more than one document, this throws a |
| 22 /// [YamlException]. In future releases, this will become an [ArgumentError]. |
| 23 /// |
| 24 /// The return value is mostly normal Dart objects. However, since YAML mappings |
| 25 /// support some key types that the default Dart map implementation doesn't |
| 26 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
| 27 /// These have a few small behavioral differences from the default Map |
| 28 /// implementation; for details, see the [YamlMap] class. |
| 29 /// |
| 30 /// In future versions, maps will instead be [HashMap]s with a custom equality |
| 31 /// operation. |
| 32 /// |
| 33 /// If [sourceUrl] is passed, it's used as the URL from which the YAML |
| 34 /// originated for error reporting. It can be a [String], a [Uri], or `null`. |
| 35 loadYaml(String yaml, {sourceUrl}) => |
| 36 loadYamlNode(yaml, sourceUrl: sourceUrl).value; |
| 37 |
| 38 /// Loads a single document from a YAML string as a [YamlNode]. |
| 39 /// |
| 40 /// This is just like [loadYaml], except that where [loadYaml] would return a |
| 41 /// normal Dart value this returns a [YamlNode] instead. This allows the caller |
| 42 /// to be confident that the return value will always be a [YamlNode]. |
| 43 YamlNode loadYamlNode(String yaml, {sourceUrl}) => |
| 44 loadYamlDocument(yaml, sourceUrl: sourceUrl).contents; |
| 45 |
| 46 /// Loads a single document from a YAML string as a [YamlDocument]. |
| 47 /// |
| 48 /// This is just like [loadYaml], except that where [loadYaml] would return a |
| 49 /// normal Dart value this returns a [YamlDocument] instead. This allows the |
| 50 /// caller to access document metadata. |
| 51 YamlDocument loadYamlDocument(String yaml, {sourceUrl}) { |
| 52 var loader = new Loader(yaml, sourceUrl: sourceUrl); |
| 53 var document = loader.load(); |
| 54 if (document == null) { |
| 55 return new YamlDocument.internal( |
| 56 new YamlScalar.internal(null, loader.span, ScalarStyle.ANY), |
| 57 loader.span, null, const []); |
| 58 } |
| 59 |
| 60 var nextDocument = loader.load(); |
| 61 if (nextDocument != null) { |
| 62 throw new YamlException("Only expected one document.", nextDocument.span); |
| 63 } |
| 64 |
| 65 return document; |
| 66 } |
| 67 |
| 68 /// Loads a stream of documents from a YAML string. |
| 69 /// |
| 70 /// The return value is mostly normal Dart objects. However, since YAML mappings |
| 71 /// support some key types that the default Dart map implementation doesn't |
| 72 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
| 73 /// These have a few small behavioral differences from the default Map |
| 74 /// implementation; for details, see the [YamlMap] class. |
| 75 /// |
| 76 /// In future versions, maps will instead be [HashMap]s with a custom equality |
| 77 /// operation. |
| 78 /// |
| 79 /// If [sourceUrl] is passed, it's used as the URL from which the YAML |
| 80 /// originated for error reporting. It can be a [String], a [Uri], or `null`. |
| 81 YamlList loadYamlStream(String yaml, {sourceUrl}) { |
| 82 var loader = new Loader(yaml, sourceUrl: sourceUrl); |
| 83 |
| 84 var documents = []; |
| 85 var document = loader.load(); |
| 86 while (document != null) { |
| 87 documents.add(document); |
| 88 document = loader.load(); |
| 89 } |
| 90 |
| 91 return new YamlList.internal( |
| 92 documents.map((document) => document.contents).toList(), |
| 93 loader.span, |
| 94 CollectionStyle.ANY); |
| 95 } |
| 96 |
| 97 /// Loads a stream of documents from a YAML string. |
| 98 /// |
| 99 /// This is like [loadYamlStream], except that it returns [YamlDocument]s with |
| 100 /// metadata wrapping the document contents. |
| 101 List<YamlDocument> loadYamlDocuments(String yaml, {sourceUrl}) { |
| 102 var loader = new Loader(yaml, sourceUrl: sourceUrl); |
| 103 |
| 104 var documents = []; |
| 105 var document = loader.load(); |
| 106 while (document != null) { |
| 107 documents.add(document); |
| 108 document = loader.load(); |
| 109 } |
| 110 |
| 111 return documents; |
| 112 } |
OLD | NEW |