import robertpenner.Tweens; import spider.Motion; import spider.Dictionary; class CIDESimulator extends MovieClip { var back, texts; var wSourceCode, wSyntax, fSourceCode; function CIDESimulator() { back._width = _width; back._height = _height; _xscale = _yscale = 100; setMask(back); // if (wSyntax != undefined) syntax = wSyntax; if (wSourceCode != undefined) sourceCode = wSourceCode; } private var wDict, dDict; private var iScroll; private var bookmarks; function get sourceCode() { return wSourceCode; } function set sourceCode(newSourceCode) { wSourceCode = newSourceCode; fSourceCode = wSourceCode.split('\n'); // create bookmarks bookmarks = [ ]; var j, k; for (var i = 0; i < fSourceCode.length; i++) { if (fSourceCode[i].slice(0, 3) != '//:') continue; bookmarks[i] = fSourceCode[i].slice(3); fSourceCode.splice(i, 1); i--; } bookmarks.current = 0; fSourceCode = fSourceCode.join('\n'); // fSourceCode = applySyntax(fSourceCode, wSyntax); fSourceCode = fSourceCode.split('\n'); // createEmptyMovieClip('texts', 0); texts.motion = new Motion(texts, Tweens.easeOutCubic, 12); // create TextFields for (var i = 0; i < fSourceCode.length; i++) { texts.createTextField('T'+i, i, 0, i*20, back._width, 20); texts['T'+i].embedFonts = true; texts['T'+i].selectable = false; texts['T'+i].multiline = true; texts['T'+i].background = true; texts['T'+i].html = true; texts['T'+i].initialHtmlText = fSourceCode[i]; highlight(i); } texts.length = fSourceCode.length; iScroll = 0; } var onSyntaxLoad; function get syntax() { return wSyntax; } function set syntax(newSyntax) { if (newSyntax instanceof XML) { XML.prototype.ignoreWhite = true; wSyntax = new XML(newSyntax.toString()); // create Dictionary wDict = new Dictionary(); dDict = new Dictionary(); var i = wSyntax.firstChild.firstChild, j, s, c; while (i) { switch (i.nodeName) { case 'quotation': case 'line-comments': case 'keywords': { for (j = i.firstChild; j; j = j.nextSibling) { wDict.setWord(j.firstChild.toString(), j); } }; break; case 'block-comments': { for (j = i.firstChild; j; j = j.nextSibling) { wDict.setWord(j.firstChild.firstChild.toString(), j); } }; break; case 'digits': case 'delimiters': { s = i.firstChild.toString(); for (j=0; j < s.length; j++) { c = s.charAt(j); if (c == '&') do { c += s.charAt(++j); } while (s.charAt(j) != ';'); dDict.setWord(c, i); } }; break; } i = i.nextSibling; } // onSyntaxLoad(); } else { var newSyntaxXML = new XML(); newSyntaxXML._parent = this; newSyntaxXML.onLoad = function () { this._parent.onSyntaxLoaded(); this._parent.syntax = this; } newSyntaxXML.load(newSyntax); } } var aColor; function applySyntax(wSourceCode, wSyntax) { if (!(wSyntax instanceof XML)) return; // prepare string wSourceCode = wSourceCode.split('&').join('&'); wSourceCode = wSourceCode.split('<').join('<'); wSourceCode = wSourceCode.split('>').join('>'); wSourceCode = wSourceCode.split("'").join('''); wSourceCode = wSourceCode.split('"').join('"'); // var safetyBreak = 0, safetyBreakLimit = 1000; var iNode, jNode; var i, j, s; var ijString; var cEscape; // escape character // initialize aColor = [ ]; // color stack for (iNode = wSyntax.firstChild.firstChild; iNode; iNode = iNode.nextSibling) { switch (iNode.nodeName) { case 'escape': { cEscape = iNode.firstChild.nodeValue; }; break; case 'highlights': { colSet(numColor(iNode.firstChild.attributes.color)); }; break; } } // creating result for (i=0; safetyBreak++ < safetyBreakLimit && i < wSourceCode.length; wSourceCode = wSourceCode.slice(0, i) + ijString + wSourceCode.slice(j), i += ijString.length) { // looking for whitespace while ((' \n\r\t').indexOf(wSourceCode.charAt(i)) > -1 && i < wSourceCode.length) i++; ijString = ''; j = i; // looking through words, comments, quotations, etc. iNode = wDict.getLargestWordOf(wSourceCode, i); if (iNode != undefined) { s = iNode.firstChild.toString(); colSet(numColor(iNode.parentNode.attributes.color)); colSet(numColor(iNode.attributes.color)); switch (iNode.parentNode.nodeName) { case 'quotation': { for (j = wSourceCode.indexOf(s, i + s.length); wSourceCode.charAt(j-1) == cEscape; j = wSourceCode.indexOf(s, j + s.length)); j += s.length; }; break; case 'line-comments': { j = wSourceCode.indexOf('\n', i + s.length); }; break; case 'block-comments': { j = wSourceCode.indexOf(iNode.lastChild.firstChild.toString(), i + s.length); j += iNode.firstChild.firstChild.toString().length + iNode.lastChild.firstChild.toString().length; }; break; case 'keywords': { j = i + s.length; }; break; } ijString += '\u00FF'; ijString += wSourceCode.slice(i, j); ijString += ''; switch (iNode.parentNode.nodeName) { case 'block-comments': { ijString = ijString.split('\n').join('\n\u00FF'); ijString = ijString.split('\r').join('\n\u00FF'); }; break; } colRemove(); colRemove(); if (!(iNode.parentNode.nodeName == 'keywords' && (dDict.getLargestWordOf(wSourceCode, j).nodeName != 'delimiters' && (' \n\t').indexOf(wSourceCode.charAt(j))<0))) continue; } ijString = ''; // skipping irrelevant code jNode = iNode != undefined; // impose request of a separator, as there have been letters before for (; ((iNode = dDict.getLargestWordOf(wSourceCode, i)) == undefined || iNode.nodeName == 'digits' && jNode) && (' \n\t').indexOf(wSourceCode.charAt(i))<0 && i < wSourceCode.length; i++, jNode = true); j = i; // looking for digits and delimiters if (iNode != undefined) { s = iNode.nodeName; for (j = i; dDict.getLargestWordOf(wSourceCode, j).nodeName == s; ) if (wSourceCode.charAt(j) == '&') while (wSourceCode.charAt(j++) != ';'); else j++; colSet(numColor(iNode.attributes.color)); ijString += '\u00FF'; ijString += wSourceCode.slice(i, j); ijString += ''; colRemove(); continue; } } // if (safetyBreak == safetyBreakLimit) trace('Warning!!! Safety break limit reached!'); // return wSourceCode; } function colSet(wColor) { aColor.unshift(wColor == undefined || isNaN(wColor) ? aColor[0] : wColor); } function colGet() { return aColor[0]; } function colRemove() { return aColor.shift(); } static function numColor(wColor) { return Number('0x'+wColor.substr(1)); } function highlight(wNum, wHighlight) { if (wHighlight != undefined) wHighlight = wHighlight.toLowerCase(); // var iNode = wSyntax.firstChild.firstChild, sNode; while (iNode.nodeName != 'highlights') iNode = iNode.nextSibling; // iNode = iNode.firstChild; sNode = iNode; if (wHighlight != undefined) { while (iNode.attributes.title.toLowerCase() != wHighlight) iNode = iNode.nextSibling; if (iNode == null) iNode = sNode; } // texts['T'+wNum].backgroundColor = numColor(iNode.attributes.background); if (iNode.attributes.syntax == true || iNode.attributes.syntax == 'true') { texts['T'+wNum].htmlText = '

' + texts['T'+wNum].initialHtmlText + '

'; } else { texts['T'+wNum].htmlText = texts['T'+wNum].initialHtmlText; var newHtmlText = texts['T'+wNum].text newHtmlText = newHtmlText.split('&').join('&'); newHtmlText = newHtmlText.split('<').join('<'); newHtmlText = newHtmlText.split('>').join('>'); newHtmlText = newHtmlText.split("'").join('''); newHtmlText = newHtmlText.split('"').join('"'); texts['T'+wNum].htmlText = '

' + newHtmlText + '

'; } } function get scroll() { return iScroll; } function set scroll(wScroll) { if (wScroll < 0) wScroll = 0; if (wScroll > maxscroll) wScroll = maxscroll; texts.motion.trans({ _y: - wScroll * 20 }); iScroll = wScroll; } function get bookmark() { return bookmarks.current; } function set bookmark(wBookmark) { if (typeof wBookmark == 'string') { for (var i = 0, j = 0; i < bookmarks.length; i++) { if (bookmarks[i] != undefined && i) j++; if (bookmarks[i] == wBookmark) break; } if (i == bookmarks.length) return; bookmarks.current = j; scroll = i; } else if (wBookmark < bookmarks.current) { for (; wBookmark < bookmarks.current && bookmarks.current > 0 && iScroll > 0; bookmarks.current--) { while (bookmarks[--iScroll] == undefined && iScroll > 0); } scroll = iScroll; } else if (wBookmark > bookmarks.current) { for (; wBookmark > bookmarks.current && bookmarks.current < bookmarks.length && iScroll < maxscroll; bookmarks.current++) { while (bookmarks[++iScroll] == undefined && iScroll < maxscroll); } scroll = iScroll; } } function getBookmark(wBookmark) { if (typeof wBookmark == 'string') { for (var i = 0, j = 0; i < bookmarks.length; i++) { if (bookmarks[i] != undefined && i) j++; if (bookmarks[i] == wBookmark) break; } if (i == bookmarks.length) return false; return i; } else { wBookmark = Number(wBookmark); for (var i = 0, j = 0; i < bookmarks.length; i++) { if (bookmarks[i] != undefined && i) j++; if (j == wBookmark) break; } if (i == bookmarks.length) return false; return i; } } function get maxscroll() { return texts.length - Math.floor(back._height/20); } }