//-- //-- TiddlyWiki instance contains TiddlerS //-- function TiddlyWiki(params) { var tiddlers = {}; // Hashmap by name of tiddlers if(params && params.config) { this.config = config; } this.tiddlersUpdated = false; this.namedNotifications = []; // Array of {name:,notify:} of notification functions this.notificationLevel = 0; this.slices = {}; // map tiddlerName->(map sliceName->sliceValue). Lazy. this.clear = function() { tiddlers = {}; this.setDirty(false); }; this.fetchTiddler = function(title) { var t = tiddlers[title]; return t instanceof Tiddler ? t : null; }; this.deleteTiddler = function(title) { delete this.slices[title]; delete tiddlers[title]; }; this.addTiddler = function(tiddler) { delete this.slices[tiddler.title]; tiddlers[tiddler.title] = tiddler; }; this.forEachTiddler = function(callback) { var t; for(t in tiddlers) { var tiddler = tiddlers[t]; if(tiddler instanceof Tiddler) callback.call(this,t,tiddler); } }; } //# Set the dirty flag TiddlyWiki.prototype.setDirty = function(dirty) { this.dirty = dirty; }; TiddlyWiki.prototype.isDirty = function() { return this.dirty; }; TiddlyWiki.prototype.tiddlerExists = function(title) { var t = this.fetchTiddler(title); return t != undefined; }; TiddlyWiki.prototype.isShadowTiddler = function(title) { return config.shadowTiddlers[title] === undefined ? false : true; }; TiddlyWiki.prototype.createTiddler = function(title) { var tiddler = this.fetchTiddler(title); if(!tiddler) { tiddler = new Tiddler(title); this.addTiddler(tiddler); this.setDirty(true); } return tiddler; }; TiddlyWiki.prototype.getTiddler = function(title) { var t = this.fetchTiddler(title); if(t != undefined) return t; else return null; }; TiddlyWiki.prototype.getShadowTiddlerText = function(title) { if(typeof config.shadowTiddlers[title] == "string") return config.shadowTiddlers[title]; else return ""; }; // Retrieve tiddler contents //# Supports tiddler slices or sections, encoded in {{{title}}} argument using //# the respective separator characters ({{{::}}} or {{{##}}}). TiddlyWiki.prototype.getTiddlerText = function(title,defaultText) { if(!title) return defaultText; var pos = title.indexOf(config.textPrimitives.sectionSeparator); var section = null; if(pos != -1) { section = title.substr(pos + config.textPrimitives.sectionSeparator.length); title = title.substr(0,pos); } pos = title.indexOf(config.textPrimitives.sliceSeparator); if(pos != -1) { var slice = this.getTiddlerSlice(title.substr(0,pos),title.substr(pos + config.textPrimitives.sliceSeparator.length)); if(slice) return slice; } var tiddler = this.fetchTiddler(title); var text = tiddler ? tiddler.text : null; if(!tiddler && this.isShadowTiddler(title)) { text = this.getShadowTiddlerText(title); } if(text) { if(!section) return text; var re = new RegExp("(^!{1,6}[ \t]*" + section.escapeRegExp() + "[ \t]*\n)","mg"); re.lastIndex = 0; var match = re.exec(text); if(match) { var t = text.substr(match.index+match[1].length); var re2 = /^!/mg; re2.lastIndex = 0; match = re2.exec(t); //# search for the next heading if(match) t = t.substr(0,match.index-1);//# don't include final \n return t; } return defaultText; } if(defaultText != undefined) return defaultText; return null; }; TiddlyWiki.prototype.getRecursiveTiddlerText = function(title,defaultText,depth) { var bracketRegExp = new RegExp("(?:\\[\\[([^\\]]+)\\]\\])","mg"); var text = this.getTiddlerText(title,null); if(text == null) return defaultText; var textOut = []; var match,lastPos = 0; do { match = bracketRegExp.exec(text); if(match) { textOut.push(text.substr(lastPos,match.index-lastPos)); if(match[1]) { if(depth <= 0) textOut.push(match[1]); else textOut.push(this.getRecursiveTiddlerText(match[1],"",depth-1)); } lastPos = match.index + match[0].length; } else { textOut.push(text.substr(lastPos)); } } while(match); return textOut.join(""); }; //TiddlyWiki.prototype.slicesRE = /(?:^([\'\/]{0,2})~?([\.\w]+)\:\1[\t\x20]*([^\n]+)[\t\x20]*$)|(?:^\|([\'\/]{0,2})~?([\.\w]+)\:?\4\|[\t\x20]*([^\n]+)[\t\x20]*\|$)/gm; TiddlyWiki.prototype.slicesRE = /(?:^([\'\/]{0,2})~?([\.\w]+)\:\1[\t\x20]*([^\n]*)[\t\x20]*$)|(?:^\|([\'\/]{0,2})~?([\.\w]+)\:?\4\|[\t\x20]*([^\|\n]*)[\t\x20]*\|$)/gm; // @internal TiddlyWiki.prototype.calcAllSlices = function(title) { var slices = {}; var text = this.getTiddlerText(title,""); this.slicesRE.lastIndex = 0; var m = this.slicesRE.exec(text); while(m) { if(m[2]) slices[m[2]] = m[3]; else slices[m[5]] = m[6]; m = this.slicesRE.exec(text); } return slices; }; // Returns the slice of text of the given name //# //# A text slice is a substring in the tiddler's text that is defined //# either like this //# aName: textSlice //# or //# |aName:| textSlice | //# or //# |aName| textSlice | //# //# In the text the name (or name:) may be decorated with '' or // //# ie this would also a valid text slice: //# //# |''aName:''| textSlice | //# //# @param name should only contain "word characters" (i.e. "a-ZA-Z_0-9") //# @return [may be undefined] the (trimmed) text of the specified slice. TiddlyWiki.prototype.getTiddlerSlice = function(title,sliceName) { var slices = this.slices[title]; if(!slices) { slices = this.calcAllSlices(title); this.slices[title] = slices; } return slices[sliceName]; }; // Build an hashmap of the specified named slices of a tiddler TiddlyWiki.prototype.getTiddlerSlices = function(title,sliceNames) { var t,r = {}; for(t=0; t<" + "body>" + text.substring(posDiv[0],posDiv[1] + endSaveArea.length) + "<" + "/body><" + "/html>"; // Create the iframe var iframe = document.createElement("iframe"); iframe.style.display = "none"; document.body.appendChild(iframe); var doc = iframe.document; if(iframe.contentDocument) doc = iframe.contentDocument; // For NS6 else if(iframe.contentWindow) doc = iframe.contentWindow.document; // For IE5.5 and IE6 // Put the content in the iframe doc.open(); doc.writeln(content); doc.close(); // Load the content into a TiddlyWiki() object var storeArea = doc.getElementById("storeArea"); this.loadFromDiv(storeArea,"store"); // Get rid of the iframe iframe.parentNode.removeChild(iframe); return this; }; TiddlyWiki.prototype.updateTiddlers = function() { this.tiddlersUpdated = true; this.forEachTiddler(function(title,tiddler) { tiddler.changed(); }); }; // Return an array of tiddlers matching a search regular expression TiddlyWiki.prototype.search = function(searchRegExp,sortField,excludeTag,match) { var candidates = this.reverseLookup("tags",excludeTag,!!match); var t,results = []; for(t=0; t