recital / exporters / fountain / index.js
index.js
Raw
const parse = require('@a-morphous/recital').default
const genFlattenedObject = require('@a-morphous/recital').genFlattenedObject

const splitStringIntoWords = (string) => {
	const splits = string.split(/\-|\_/gm)
	return splits.join(' ')
}

const capitalize = (string) => {
	return string[0].toUpperCase() + string.substring(1)
}

const sceneToFountain = (scene) => {
	let text = ''

	// scene meta
	text += '===' + '\n\n'
	const flatScene = genFlattenedObject([scene])
	for (let token of flatScene) {
		switch (token.type) {
			case 'scene':
				// scenes start with . and are in all caps
				let sceneTitle = token.primary || 'scene'
				if (token.meta) {
					sceneTitle = token.meta.title || token.meta.scene || sceneTitle
				}
				text += '.' + splitStringIntoWords(sceneTitle).toUpperCase() + '\n\n'
				break
			case 'paragraph':
				let isDialogue = false
				if (token.primary) {
					// it's a character
					isDialogue = true
					text += '\t\t\t' + splitStringIntoWords(token.primary).toUpperCase() + '\n'
				}

				if (token.classes && token.classes.length > 1 && isDialogue) {
					// the remainder of the classes becomes the parenthetical
					const parenClasses = token.classes.concat([])
					parenClasses.splice(0, 1)
					text += '\t\t(' + capitalize(splitStringIntoWords(parenClasses.join(' '))) + ')\n'
				}

				// get the rest of the text in there
				if (!isDialogue) {
					text += token.text + '\n\n'
				} else {
					text += '\t' + token.text + '\n\n'
				}
				break
		}
	}
	return text
}

const stageToFountain = (stageScriptString) => {
	let finalString = ''
	const scenes = parse(stageScriptString)

	for (let scene of scenes) {
		finalString += sceneToFountain(scene)
	}

	return finalString
}

module.exports = stageToFountain