Spintax Parser

input spintax, get a plaintext output

by Joel Kirchartz

HTML

<textarea id="dedupe"></textarea>
<button id="execute">Convert Spintax to Text</button>

CSS

#dedupe {
    height:600px;
    width:100%;
}

JavaScript

/*
This JS stolen from Usama Ejaz at: http://www.thedeveloper24.com/2014/10/parse-spintax-using-javascript.html (it's not 100%, but close enough for simple spintaxes)
*/
var SPINTAX_PATTERN = /\{[^"\r\n\}]*\}/;
var spin = function (spun) {
		var match;
		while (match = spun.match(SPINTAX_PATTERN)) {
			match = match[0];
			var candidates = match.substring(1, match.length - 1).split("|");
			spun = spun.replace(match, candidates[Math.floor(Math.random() * candidates.length)])
		}
		return spun;
	}
var spin_countVariations = function (spun) {
		spun = spun.replace(/[^{|}]+/g, '1');
		spun = spun.replace(/\{/g, '(');
		spun = spun.replace(/\|/g, '+');
		spun = spun.replace(/\}/g, ')');
		spun = spun.replace(/\)\(/g, ')*(');
		spun = spun.replace(/\)1/g, ')*1');
		spun = spun.replace(/1\(/g, '1*(');
    console.log(spun);
		return eval(spun);
	}

/* This JS makes it work, here: */

document.getElementById('execute').onclick = function() {
    var ct = document.getElementById('dedupe');
    ct.value = spin(ct.value);
}