MCPcopy Create free account
hub / github.com/corbanbrook/dsp.js / preprocessCode

Function preprocessCode

examples/js/processing.js:12112–12155  ·  view source on GitHub ↗
(aCode, sketch)

Source from the content-addressed store, hash-verified

12110 }// Parser ends
12111
12112 function preprocessCode(aCode, sketch) {
12113 // Parse out @pjs directive, if any.
12114 var dm = /\/\*\s*@pjs\s+((?:[^\*]|\*+[^\*\/])*)\*\//g.exec(aCode);
12115 if (dm && dm.length === 2) {
12116 var directives = dm.splice(1, 2)[0].replace('\n', '').replace('\r', '').split(';');
12117
12118 // We'll L/RTrim, and also remove any surrounding double quotes (e.g., just take string contents)
12119 var clean = function(s) {
12120 return s.replace(/^\s*\"?/, '').replace(/\"?\s*$/, '');
12121 };
12122
12123 for (var i = 0, dl = directives.length; i < dl; i++) {
12124 var pair = directives[i].split('=');
12125 if (pair && pair.length === 2) {
12126 var key = clean(pair[0]);
12127 var value = clean(pair[1]);
12128 // A few directives require work beyond storying key/value pairings
12129 if (key === "preload") {
12130 var list = value.split(',');
12131 // All pre-loaded images will get put in imageCache, keyed on filename
12132 for (var j = 0, ll = list.length; j < ll; j++) {
12133 var imageName = clean(list[j]);
12134 sketch.imageCache.add(imageName);
12135 }
12136 } else if (key === "opaque") {
12137 sketch.options.isOpaque = value === "true";
12138 } else if (key === "crisp") {
12139 sketch.options.crispLines = value === "true";
12140 } else if (key === "pauseOnBlur") {
12141 sketch.options.pauseOnBlur = value === "true";
12142 } else {
12143 sketch.options[key] = value;
12144 }
12145 }
12146 }
12147 }
12148
12149 // Check if 3D context is invoked -- this is not the best way to do this.
12150 var codeWoStrings = aCode.replace(/("(?:[^"\\\n]|\\.)*")|('(?:[^'\\\n]|\\.)*')|(([\[\(=|&!\^:?]\s*)(\/(?![*\/])(?:[^\/\\\n]|\\.)*\/[gim]*)\b)|(\/\/[^\n]*\n)|(\/\*(?:(?!\*\/)(?:.|\n))*\*\/)/g, "");
12151 if (codeWoStrings.match(/\bsize\((?:.+),(?:.+),\s*(OPENGL|P3D)\s*\);/)) {
12152 sketch.use3DContext = true;
12153 }
12154 return aCode;
12155 }
12156
12157 // Parse/compiles Processing (Java-like) syntax to JavaScript syntax
12158 Processing.compile = function(pdeCode) {

Callers 1

processing.jsFile · 0.85

Calls 1

cleanFunction · 0.85

Tested by

no test coverage detected