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