1 | 'use strict';
|
---|
2 |
|
---|
3 | var parseCst = require('./parse-cst.js');
|
---|
4 | var Document$1 = require('./Document-9b4560a1.js');
|
---|
5 | var Schema = require('./Schema-88e323a7.js');
|
---|
6 | var PlainValue = require('./PlainValue-ec8e588e.js');
|
---|
7 | var warnings = require('./warnings-1000a372.js');
|
---|
8 | require('./resolveSeq-d03cb037.js');
|
---|
9 |
|
---|
10 | function createNode(value, wrapScalars = true, tag) {
|
---|
11 | if (tag === undefined && typeof wrapScalars === 'string') {
|
---|
12 | tag = wrapScalars;
|
---|
13 | wrapScalars = true;
|
---|
14 | }
|
---|
15 |
|
---|
16 | const options = Object.assign({}, Document$1.Document.defaults[Document$1.defaultOptions.version], Document$1.defaultOptions);
|
---|
17 | const schema = new Schema.Schema(options);
|
---|
18 | return schema.createNode(value, wrapScalars, tag);
|
---|
19 | }
|
---|
20 |
|
---|
21 | class Document extends Document$1.Document {
|
---|
22 | constructor(options) {
|
---|
23 | super(Object.assign({}, Document$1.defaultOptions, options));
|
---|
24 | }
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 | function parseAllDocuments(src, options) {
|
---|
29 | const stream = [];
|
---|
30 | let prev;
|
---|
31 |
|
---|
32 | for (const cstDoc of parseCst.parse(src)) {
|
---|
33 | const doc = new Document(options);
|
---|
34 | doc.parse(cstDoc, prev);
|
---|
35 | stream.push(doc);
|
---|
36 | prev = doc;
|
---|
37 | }
|
---|
38 |
|
---|
39 | return stream;
|
---|
40 | }
|
---|
41 |
|
---|
42 | function parseDocument(src, options) {
|
---|
43 | const cst = parseCst.parse(src);
|
---|
44 | const doc = new Document(options).parse(cst[0]);
|
---|
45 |
|
---|
46 | if (cst.length > 1) {
|
---|
47 | const errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
|
---|
48 | doc.errors.unshift(new PlainValue.YAMLSemanticError(cst[1], errMsg));
|
---|
49 | }
|
---|
50 |
|
---|
51 | return doc;
|
---|
52 | }
|
---|
53 |
|
---|
54 | function parse(src, options) {
|
---|
55 | const doc = parseDocument(src, options);
|
---|
56 | doc.warnings.forEach(warning => warnings.warn(warning));
|
---|
57 | if (doc.errors.length > 0) throw doc.errors[0];
|
---|
58 | return doc.toJSON();
|
---|
59 | }
|
---|
60 |
|
---|
61 | function stringify(value, options) {
|
---|
62 | const doc = new Document(options);
|
---|
63 | doc.contents = value;
|
---|
64 | return String(doc);
|
---|
65 | }
|
---|
66 |
|
---|
67 | const YAML = {
|
---|
68 | createNode,
|
---|
69 | defaultOptions: Document$1.defaultOptions,
|
---|
70 | Document,
|
---|
71 | parse,
|
---|
72 | parseAllDocuments,
|
---|
73 | parseCST: parseCst.parse,
|
---|
74 | parseDocument,
|
---|
75 | scalarOptions: Document$1.scalarOptions,
|
---|
76 | stringify
|
---|
77 | };
|
---|
78 |
|
---|
79 | exports.YAML = YAML;
|
---|