* @copyright Author */ function makeNamespaced($input, $ns = 'Moo') { preg_match_all('/^(?:var|function) ([a-zA-Z$]*)/m', $input, $bits, PREG_PATTERN_ORDER); $replace = $bits[1]; usort($replace, 'length_cmp'); array_walk($replace, 'preg_quote'); $replace = implode('|', $replace); $input = preg_replace('/(function )(' . $replace . ')([^a-zA-Z\/])/', $ns .'.$2 = function$3', $input); /** * FIXME: Optimize regexp to don't ignore matched words with only 1 character bewteen ( $native(Element) ) */ for ($i = 0; $i < 2; $i++) { $input = preg_replace('/(?!<[^a-zA-Z0-9.^$_])(var )?(' . $replace . ')(?![a-zA-Z\/])/', $ns .'.$2', $input); } $input = preg_replace('/(\*\/)/', '$1 /* Object: '. $ns .' The root namespace object for all replaced classes and methods Contains: '. wordwrap(implode(', ', $bits[1]), 75, "\n ") .' */ var '. $ns .' = {}; ', $input, 1); return $input; } /** * Helper function */ function length_cmp($a, $b) { return strlen($b) - strlen($a); } header("Content-type: application/x-javascript"); echo makeNamespaced(implode('', file('mootools.js')), 'Moo'); ?>