define(function() {

  'use strict';
  
  function Terp() {
  }
  
  var validTerpScripts = new WeakMap();
  
  // modified version of JSON.stringify with specialized whitespace rules
  function stringifyStepOrBlock(stepOrBlock, indent) {
    if (typeof stepOrBlock[0] === 'string') {
      // step mode
      function stringifyElement(element) {
        if (typeof element === 'object' && element !== null) {
          if (!Array.isArray(element)) {
            throw new SyntaxError('Non-Array objects are not currently supported in TerpScript');
          }
          return stringifyStepOrBlock(element, indent);
        }
        return JSON.stringify(element);
      }
      return '[' + stepOrBlock.map(stringifyElement).join(', ') + ']';
    }
    if (stepOrBlock.length === 0) return '[ ]';
    indent = indent || '';
    if (stepOrBlock.length === 1) {
      var single = stringifyStepOrBlock(stepOrBlock[0], indent);
      if (!/\n/.test(single)) {
        return '[ ' + single + ' ]';
      }
      return '[\n' + indent + single + '\n' + indent + ']';
    }
    var newIndent = indent + '  ';
    function stringifyStep(step) {
      return stringifyStepOrBlock(step, newIndent);
    }
    if (indent === '') {
      return '[\n\n' + stepOrBlock.map(stringifyStep).join(',\n') + '\n\n]';
    }
    return '[\n' + newIndent + stepOrBlock.map(stringifyStep).join(',\n' + newIndent) + '\n' + indent + ']';
  }
  
  function toTerpScript(stepOrBlock, okToModify) {
    if (!okToModify) {
      stepOrBlock = stepOrBlock.slice();
    }
    for (var i = 0; i < stepOrBlock.length; i++) {
      if (typeof stepOrBlock[i] === 'object' && stepOrBlock[i] !== null) {
        if (validTerpScripts.has(stepOrBlock[i])) {
          continue;
        }
        if (!Array.isArray(stepOrBlock(i))) {
          throw new SyntaxError('Non-Array objects are not currently supported in TerpScript');
        }
        stepOrBlock[i] = toTerpScript(stepOrBlock[i], okToModify);
      }
    }
    Object.freeze(stepOrBlock);
    validTerpScripts.set(stepOrBlock, true);
    return stepOrBlock;
  }
  
  function script(v, isTheOnlyReference) {
    if (validTerpScripts.has(v)) return v;
    if (typeof v === 'string') {
      v = JSON.parse(v);
      if (!Array.isArray(v)) {
        throw new SyntaxError('TerpScripts must be contained in an Array');
      }
      return toTerpScript(JSON.parse(v), true);
    }
    if (!Array.isArray(v)) {
      if (typeof v.toJSON !== 'function' || !Array.isArray(v = v.toJSON())) {
        throw new SyntaxError('TerpScripts must be contained in an Array');
      }
      return toTerpScript(v, true);
    }
    return toTerpScript(v, isTheOnlyReference);
  }
  
  Object.assign(Terp, {
    script: Object.assign(script, {
      stringify: function(script) {
        if (typeof script === 'string') {
          script = JSON.parse(script);
          if (Array.isArray(script)) {
            return stringifyStepOrBlock(script);
          }
        }
        else {
          if (Array.isArray(script)) {
            return stringifyStepOrBlock(script);
          }
          if (typeof script.toJSON === 'function' && Array.isArray(script = script.toJSON())) {
            return stringifyStepOrBlock(script);
          }
        }
        throw new SyntaxError('TerpScript must be an contained in an Array');
      },
    }),
  });
  
  return Terp;

});

@@ -797,35 +797,8 @@
 %5D';%0A
-    indent = indent %7C%7C '';%0A
     
@@ -825,24 +825,53 @@
 th === 1) %7B%0A
+      indent = indent %7C%7C '';%0A
       var si
@@ -1062,24 +1062,293 @@
  '%5D';%0A    %7D%0A
+    if (typeof indent !== 'string') %7B%0A      indent = '';%0A      function stringifyStepTop(step) %7B%0A        return stringifyStepOrBlock(step, indent);%0A      %7D%0A      return '%5B%5Cn%5Cn' + stepOrBlock.map(stringifyStepTop).join(',%5Cn') + '%5Cn%5Cn%5D';%0A    %7D%0A    indent = indent %7C%7C '';%0A
     var newI
@@ -1467,116 +1467,8 @@
   %7D%0A
-    if (indent === '') %7B%0A      return '%5B%5Cn%5Cn' + stepOrBlock.map(stringifyStep).join(',%5Cn') + '%5Cn%5Cn%5D';%0A    %7D%0A
     

Fix top-level indent