﻿//* @public

/**
	Populates a string template with data values.

	Returns a copy of _inText_, with macros defined by _inPattern_ replaced by
	named values in _inMap_.

	_inPattern_ may be omitted, in which case the default macro pattern is used. 
	The default pattern matches macros of the form

		{$name}

	Example:

		// returns "My name is Barney."
		enyo.macroize("My name is {$name}.", {name: "Barney"});

	Dot notation is supported, like so:

		var info = {
			product_0: {
				name: "Gizmo"
				weight: 3
			}
		}
		// returns "Each Gizmo weighs 3 pounds."
		enyo.macroize("Each {$product_0.name} weighs {$product_0.weight} pounds.", info);
*/
enyo.macroize = function(inText, inMap, inPattern) {
	var v, working, result = inText, pattern = inPattern || enyo.macroize.pattern;
	var fn = function(macro, name) {
		working = true;
		v = enyo.getObject(name, false, inMap);
		//v = inMap[name];
		return (v === undefined || v === null) ? "{$" + name + "}" : v;
	};
	var prevent = 0;
	do {
		working = false;
		result = result.replace(pattern, fn);
	// if iterating more than 100 times, we assume a recursion (we should throw probably)
	} while (working && (prevent++ < 100));
	return result;
};

//* @protected

// matches macros of form {$name}
enyo.macroize.pattern = /{\$([^{}]*)}/g;

@@ -816,26 +816,8 @@
 ) %7B%0A
-%09%09working = true;%0A
 %09%09v 
@@ -860,35 +860,10 @@
 ;%0A%09%09
-//v = inMap%5Bname%5D;%0A%09%09return
+if
  (v 
@@ -895,9 +895,19 @@
 ll) 
-?
+%7B%0A%09%09%09return
  %22%7B$
@@ -924,10 +924,40 @@
  %22%7D%22
- :
+;%0A%09%09%7D%0A%09%09working = true;%0A%09%09return
  v;%0A
@@ -1043,16 +1043,17 @@
 n, fn);%0A
+%09
 %09// if i
@@ -1071,18 +1071,17 @@
 re than 
-10
+2
 0 times,
@@ -1118,62 +1118,519 @@
 uld 
-throw probably)%0A%09%7D while (working && (prevent++ %3C 100)
+probably throw)%0A%09%09if (++prevent %3E= 20) %7B%0A%09%09%09throw(%22enyo.macroize: recursion too deep%22);%0A%09%09%7D%0A%09%7D while (working);%0A%09return result;%0A%7D;%0A%0Aenyo.quickMacroize = function(inText, inMap, inPattern) %7B%0A%09var v, working, result = inText, pattern = inPattern %7C%7C enyo.macroize.pattern;%0A%09var fn = function(macro, name) %7B%0A%09%09if (name in inMap) %7B%0A%09%09%09v = inMap%5Bname%5D;%0A%09%09%7D else %7B%0A%09%09%09v = enyo.getObject(name, false, inMap);%0A%09%09%7D%0A%09%09return (v === undefined %7C%7C v === null) ? %22%7B$%22 + name + %22%7D%22 : v;%0A%09%7D;%0A%09result = result.replace(pattern, fn
 );%0A%09

fix bug that would cause unnecessary recursion in macroize; add exception throw when hitting recursion fail-safe; add quickMacroize