"use strict"; /** * Copyright (c) 2022 Amorphous * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.genSceneObjects = void 0; const moo_1 = __importDefault(require("moo")); const eol_1 = __importDefault(require("eol")); const expose_meta_1 = require("../../tools/expose-meta"); const parse_toml_meta_1 = require("../../tools/parse-toml-meta"); const parse_inline_meta_1 = require("../../tools/parse-inline-meta"); const genSceneObjects = (preprocessedString) => { const scenes = []; const lexer = moo_1.default.states({ main: { sceneDefStart: { match: /\#[ \t]*\n/, lineBreaks: true, push: 'sceneDefinition' }, sceneWithTitleStart: { match: /\#:[ \t]*.*\n/, lineBreaks: true, push: 'sceneDefinition' }, text: { match: /[^]+?/, lineBreaks: true }, }, sceneDefinition: { tomlStartDash: { match: '---', push: 'sceneTomlDash' }, tomlStartPlus: { match: '+++', push: 'sceneTomlPlus' }, sceneDefEnd: { match: /\n/, lineBreaks: true, push: 'main' }, }, sceneTomlDash: { tomlEnd: { match: '---', push: 'sceneDefinition' }, toml: { match: /[^]+?(?=---)/, lineBreaks: true }, }, sceneTomlPlus: { tomlEnd: { match: '+++', push: 'sceneDefinition' }, toml: { match: /[^]+?(?=\+\+\+)/, lineBreaks: true }, } }); // first # can be omitted if we immediately start with toml frontmatter let lexerRaw = eol_1.default.lf(preprocessedString); if (lexerRaw.startsWith('---\n') || lexerRaw.startsWith('+++\n')) { lexerRaw = '#\n' + lexerRaw; } lexer.reset(eol_1.default.lf(lexerRaw)); let currentScene = { type: 'scene', raw: '', }; for (let token of Array.from(lexer)) { switch (token.type) { case 'sceneDefStart': if (!currentScene.raw.length) { continue; } scenes.push(currentScene); currentScene = { type: 'scene', raw: '', }; break; case 'sceneWithTitleStart': if (currentScene.raw.length) { scenes.push(currentScene); } const inlineMeta = token.value.substring(2).trim(); const inlineMetaObject = inlineMeta ? (0, parse_inline_meta_1.parseInlineMeta)(inlineMeta.trim()) : undefined; currentScene = { type: 'scene', raw: '', meta: inlineMetaObject, }; break; case 'toml': const tomlObj = (0, parse_toml_meta_1.parseTOMLMeta)(token.value); if (tomlObj === undefined) { throw new Error(lexer.formatError(token, 'empty TOML frontmatter in scene.')); } if (currentScene.meta) { currentScene.meta = Object.assign(Object.assign({}, currentScene.meta), tomlObj); } else { currentScene.meta = tomlObj; } (0, expose_meta_1.exposeMeta)(currentScene); break; case 'text': currentScene.raw += token.value; break; } } if (currentScene.raw.length) { scenes.push(currentScene); } return scenes; }; exports.genSceneObjects = genSceneObjects;