recital / exporters / html / cli.js
cli.js
Raw
#!/usr/bin/env node
/**
 * 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/.
 */


const stageToHTML = require('./dist/stage-html').stageToHTML
const argv = require('minimist')(process.argv.slice(2))
const fs = require('fs')
const path = require('path')

const pkg = require('./package.json')
const { resolveIncludes } = require('@a-morphous/recital-ext-common-commands')

const main = () => {
	if (argv.v || argv.version) {
		console.log(pkg.version)
		return
	}

	if (!argv._ || argv._.length === 0) {
		console.log(`Usage: stage-html input-file.stage optional-file-2.stage -o outputfile.html
		Options:
			*TAG OPTIONS*
			--sceneTag tagName - Tag (without brackets) that wraps around scenes. Defaults to 'section'
			--blockTag tagName - Tag (without brackets) that wraps around fragments. Defaults to 'div'
			--separator '<hr />' - HTML that goes between scenes. Defaults to nothing.
	
			*CONVERSION OPTIONS*
			--useIncludes - if set, will accept the $include command to have HTML templates. Defaults to false.
			--pureMarkdown - if set, will not use any of the common extensions (e.g. wikilinks). Defaults to false.
	`)
		return
	}

	let opts = {}

	if (argv.sceneTag !== undefined) {
		opts.sceneTag = argv.sceneTag
	}

	if (argv.blockTag !== undefined) {
		opts.blockTag = argv.blockTag
	}

	if (argv.separator !== undefined) {
		opts.sceneSeparator = argv.separator
	}

	if (argv.useIncludes !== undefined) {
		opts.useIncludes = true
	}

	if (argv.pureMarkdown !== undefined) {
		opts.pureMarkdown = true
	}

	let str = ''
	for (let file of argv._) {
		let fileString = fs.readFileSync(file, 'utf-8')
		if (opts.useIncludes) {
			fileString = resolveIncludes(fs, path, file, fileString)
		}
		const htmlString = stageToHTML(fileString, opts)

		str += htmlString + '\n\n'
	}

	// make sure we end with one newline at the end.
	str = str.trim() + '\n'

	if (argv.o) {
		fs.writeFileSync(argv.o, str)
	} else {
		console.log(str)
	}
}

main()