Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created January 21, 2011 18:57
Show Gist options
  • Save phiggins42/790196 to your computer and use it in GitHub Desktop.
Save phiggins42/790196 to your computer and use it in GitHub Desktop.
Index: lib/parser2/dojo2.inc
===================================================================
--- lib/parser2/dojo2.inc (revision 23594)
+++ lib/parser2/dojo2.inc (working copy)
@@ -143,6 +143,61 @@
return $files;
}
+function _slashtoname($thing){
+ return str_replace("/", ".", $thing);
+}
+
+// COULD BE BETTER. The AMD wrapper define(...) removal is a bit fragile.
+function _unwrap_amd_format($text){
+
+ // text: The original fulltext of the file.
+ // returns unwrapped amd with added provide/require statements
+
+ $re = '/^define\(([^\]]+)\]\s*\,[\s\n]*function.+/';
+ preg_match($re, $text, $matches);
+
+ if(!empty($matches[0])){
+
+ $target = $matches[1];
+ $prefixtext = "";
+
+ preg_match('/^[\'"]([_a-zA-Z\/]+)[\'"]\s*,\s*\[(.*)$/', $target, $parts);
+
+ $moduleid = $parts[1];
+ if(empty($moduleid)){
+ // unable to process unnamed modules.
+ return $text;
+ }
+ $prefixtext .= 'dojo.provide("' . _slashtoname($moduleid) . '");';
+ $prefixtext .= "\n";
+
+ preg_match_all('/[\'"]([\w\/-_\!]+)[\'"],?/', $parts[2], $deps);
+ if(is_array($deps[1])){
+ foreach($deps[1] as $dep){
+ $firstbit = substr($dep, 0, 5);
+ if($firstbit == "text!"){
+ // do nothing
+ }else if($firstbit == "i18n!"){
+ preg_match('/i18n\!(.+)\/nls\/(\w+)/', $dep, $bundlebits);
+ $prefixtext .= 'dojo.requireLocalization("' . $bundlebits[1] . '", "' . $bundlebits[2] . '");';
+ $prefixtext .= "\n";
+ }else if($dep != "dojo" && $dep != "dijit" && $dep != "dojox"){
+ $prefixtext .= 'dojo.require("' . _slashtoname($dep) . '");';
+ $prefixtext .= "\n";
+ }
+ }
+ }
+
+ // strip out "AMD-result" comments? not sure what these are.
+
+ $text = preg_replace($re, "", $text);
+ $text = preg_replace('/\s*return\s+[_a-zA-Z\.0-9]+\s*;\s(\/\/.+)?\s*\}\);\s*$/', "", $text);
+ $text = $prefixtext . $text;
+ }
+
+ return $text;
+}
+
function dojo_get_contents($namespace, $file_name) {
if (function_exists($namespace . '_code_location')) {
$location = _dojo_ensure_directory(call_user_func($namespace . '_code_location'));
@@ -152,7 +207,10 @@
$location = $_dojo_properties_modules[$namespace]['location'];
}
- $lines = preg_replace('%/\*={3,}|={3,}\*/%', '', file_get_contents($location . '/' . $file_name));
+ $filedata = file_get_contents($location . '/' . $file_name);
+ $filedata = _unwrap_amd_format($filedata);
+
+ $lines = preg_replace('%/\*={3,}|={3,}\*/%', '', $filedata);
$parser = new JavaScriptParser(JavaScriptLanguage::tokenize($lines));
// print '<pre>';
// $statements = $parser->statements();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment