//{{{ // Blog macro // v0.2 // 09th October 2007 // // Author: Craig Cook, craig [dot] cook [at] bt [dot] com // // Not quite sure how to do this at the moment // ============================================ // Adapted: 2008-11-22 W. Hibma, http://whitehat-marketer.com/blog // Change: Added the possibility to update a post. //Need to add the following into the main TiddlyWiki://config.commands.publishBlog: {}, - FIXED //and to the div 'ViewTemplate' in the 'toolbar' class add publishBlog - CHANGED: // blogViewTemplate created, active when TaggedTemplateTweak plugin is installed //Add the publishBlog option into TiddlyWiki // set some default values for blog variables if(!config.options.txtBlogID) config.options.txtBlogID = "your blog id"; if(!config.options.txtBlogUserName) config.options.txtBlogUserName = "your username"; if(!config.options.pasBlogPassWord) config.options.pasBlogPassWord = "password"; var _subject=""; var _method = ""; var _blogid = ""; config.commands.publishBlog = {}; merge(config.commands.publishBlog,{ text: "publish", tooltip: "This will publish to you online blog"}); config.commands.publishBlog.handler = function(event,src,title) { var pubTiddler = store.getTiddler(title); var blog = new Blog(); blog.setSubject(pubTiddler.title); _subject = pubTiddler.title; blog.setTags(pubTiddler.tags); var tiddlerElem = store.getTiddler(_subject); var theText = tiddlerElem.text; // Change the post if (theText.indexOf("[#")>0) { blog.method = "metaWeblog.editPost"; var index=0; var index2=0; index = theText.indexOf("[#", index+1); index2 = theText.indexOf("#]", index+1); _blogid = theText.substring(index+2,index2); blog.blogid = _blogid; theText = theText.replace("[#"+ _blogid +"#]",""); } // New post else { blog.method = "metaWeblog.newPost"; blog.blogid = config.options.txtBlogID; // get the full name of the blog, i.e. site.com } _method = blog.method; blog.setContent(theText); blog.publish(); }; function Blog() { this.appkey = "0123456789ABCDEF"; this.username = config.options.txtBlogUserName; this.password = config.options.pasBlogPassWord; this.blogid = config.options.txtBlogID; //i.e. site.com this.content = ""; this.subject = ""; this.tags = ""; this.blogURL = "http://"+config.options.txtBlogID+"/xmlrpc.php"; //This is for wordpress, will need to be changed for others } Blog.prototype.publish = function(){ var xmlMsg = new XMLRPCMessage(); xmlMsg.setMethod(this.method); xmlMsg.addStringXML(this.blogid); xmlMsg.addStringXML(this.username); xmlMsg.addStringXML(this.password); // Make real hyperlinks from the tiddly links, // [[Tiddly Link]] becomes Tiddly Link var m = this.content.match(/\[\[.*?\]\]/g); if (m != null && m.length>=1) { for(var i=0;i" + link + ""; this.content = this.content.replace(m[i],hyperlink); } } xmlMsg.addItemXML("description" + this.content.htmlEncode() + "title" + this.subject + "categories" + this.tags + ""); xmlMsg.addBoolXML(true); var xmlText = xmlMsg.xml(); this.makeAjaxCall(this.blogURL, xmlText); }; Blog.prototype.makeAjaxCall = function(url, postdata) { // with callback var loginCallback = function(status,params,responseText,url,xhr) { if (status) { displayMessage("completed..."); alert(responseText); var theId = ""; var tiddlerElem = store.getTiddler(_subject); // change post if (_method == "metaWeblog.editPost") { tiddlerElem.text.replace(/\[#.*?#\]/mg,"[#"+ _blogid + "#]"); } else // new post { var index=0; var index2=0; index = responseText.indexOf("", index+1); index2 = responseText.indexOf("", index+1); theId = responseText.substring(index+8,index2); if(tiddlerElem.text.contains("")) { tiddlerElem.text = tiddlerElem.text.replace("", "[#" + theId + "#]") } else { tiddlerElem.text = tiddlerElem.text + "[#" + theId + "#]"; } // if(tiddlerElem.text.contains("")) } // if (_method == "metaWeblog.editPost") store.savetiddlerElem(tiddlerElem.title,tiddlerElem.title,tiddlerElem.text,tiddlerElem.modifier, tiddlerElem.modified,tiddlerElem.tags,tiddlerElem.fields,true,tiddlerElem.created); //story.refreshTiddler(tiddlerElem.title,null,true); } else { displayMessage("status is false"); displayMessage(xhr.statusText); } }; var headers = { "Content-type":"text/xml", "Content-length":postdata.length, "Connection":"close" }; var ret = doHttp("POST",url,postdata,null,null,null,loginCallback,null,headers); if(typeof ret == "string") { displayMessage("Failed HTTP request - is BlogPlugin set up?","Error: " + ret); } else { displayMessage("posting..."); } /* old version without callback var xmlhttp = getXMLHttpRequest(); if(window.Components && window.netscape && window.netscape.security && document.location.protocol.indexOf("http") == -1) window.netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); xmlhttp.open("POST", url, false); xmlhttp.setRequestHeader("Content-type", "text/xml"); xmlhttp.setRequestHeader("Content-length", postdata.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(postdata); alert("You blog has been sent"); //TODO: add response to blog submission END: old version without callback */ }; Blog.prototype.setSubject = function(txt){ if (!txt) return; this.subject = txt; }; Blog.prototype.setContent = function(txt){ if (!txt) return; this.content = txt; }; Blog.prototype.setTags = function(tags){ for (var i=0;i"; } } if (!this.tags) { this.tags = "Uncategorized"; return; } }; function XMLRPCMessage() { this.method = ""; this.msgParams = []; } XMLRPCMessage.prototype.setMethod = function(methodName){ if (!methodName) return; this.method = methodName; }; XMLRPCMessage.prototype.xml = function(){ var method = this.method; // assemble the XML message header var xml = ""; xml += "\n"; xml += "\n"; xml += "" + method+ "\n"; xml += "\n"; // do individual parameters for (var i = 0; i < this.msgParams.length; i++){ xml += this.msgParams[i]; } xml += "\n"; xml += ""; return xml; // for now }; XMLRPCMessage.prototype.addBoolXML = function(data){ var value = (data==true)?1:0; this.msgParams[this.msgParams.length] = "\n" + value + "\n\n"; }; XMLRPCMessage.prototype.addStringXML = function(data){ this.msgParams[this.msgParams.length] = "\n" + data + "\n\n"; }; XMLRPCMessage.prototype.addItemXML = function(data){ this.msgParams[this.msgParams.length] = "\n" + data + "\n\n"; }; //}}}