diff --git a/plugins/tiddlywiki/tiddlyweb/plugin.bundle b/plugins/tiddlywiki/tiddlyweb/plugin.bundle new file mode 100644 index 000000000..882ecb543 --- /dev/null +++ b/plugins/tiddlywiki/tiddlyweb/plugin.bundle @@ -0,0 +1,7 @@ +{ + "title": "$:/plugins/tiddlywiki/tiddlyweb", + "description": "TiddlyWeb and TiddlySpace components", + "author": "JeremyRuston", + "version": "0.0.0", + "coreVersion": ">=5.0.0" +} diff --git a/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js new file mode 100644 index 000000000..a52220f54 --- /dev/null +++ b/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js @@ -0,0 +1,159 @@ +/*\ +title: $:/plugins/tiddlywiki/tiddlyweb/tiddlyweb.js +type: application/javascript +module-type: browser-startup + +Main TiddlyWeb integration module + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +$tw.plugins.tiddlyweb = { + titleIsLoggedIn: "$:/plugins/tiddlyweb/IsLoggedIn", + titleUserName: "$:/plugins/tiddlyweb/UserName" +}; + +/* +Startup function that sets up TiddlyWeb and logs the user in. After login, any tiddlyweb-startup modules are executed. +*/ +exports.startup = function() { + if(!$tw.browser) { + return; + } + // Mark us as not logged in + $tw.wiki.addTiddler({ + title: $tw.plugins.tiddlyweb.titleIsLoggedIn, + text: "no" + }); + // Get the login status + $tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) { + if(!isLoggedIn) { +// $tw.plugins.tiddlyweb.login(username,password); + } + }); +}; + +$tw.plugins.tiddlyweb.getStatus = function(callback) { + // Get status + $tw.plugins.tiddlyweb.httpRequest({ + url: "http://tw5tiddlyweb.tiddlyspace.com/status", + callback: function(err,data) { + var json = null; + try { + json = JSON.parse(data); + } catch (e) { + } + if(json) { + var isLoggedIn = json.username !== "GUEST"; + $tw.wiki.addTiddler({ + title: $tw.plugins.tiddlyweb.titleIsLoggedIn, + text: isLoggedIn ? "yes" : "no" + }); + if(isLoggedIn) { + $tw.wiki.addTiddler({ + title: $tw.plugins.tiddlyweb.titleUserName, + text: json.username + }); + } else { + $tw.wiki.deleteTiddler($tw.plugins.tiddlyweb.titleUserName); + } + } + if(callback) { + callback(isLoggedIn,json); + } + } + }); +}; + +/* +Error handling +*/ +$tw.plugins.tiddlyweb.showError = function(error) { + alert("TiddlyWeb error: " + error); + console.log("TiddlyWeb error: " + error); +}; + +/* +Invoke any tiddlyweb-startup modules +*/ +$tw.plugins.tiddlyweb.invokeTiddlyWebStartupModules = function(loggedIn) { + $tw.modules.forEachModuleOfType("tiddlyweb-startup",function(title,module) { + module.startup(loggedIn); + }); +}; + +/* +Attempt to login to TiddlyWeb +*/ +$tw.plugins.tiddlyweb.login = function(username,password,options) { + options = options || {}; + var httpRequest = $tw.plugins.tiddlyweb.httpRequest({ + url: "http://tw5tiddlyweb.tiddlyspace.com/challenge/tiddlywebplugins.tiddlyspace.cookie_form", + type: "POST", + data: { + user: username, + password: password, + tiddlyweb_redirect: "/status" // workaround to marginalize automatic subsequent GET + }, + callback: function(err,data) { + if(err) { + console.log("login error",err); + } else { + $tw.plugins.tiddlyweb.getStatus(function(isLoggedIn,json) { + console.log("isLoggedIn",isLoggedIn); + }); + console.log("Result of login",data,httpRequest); + } + } + }); +}; + +/* +Some quick and dirty HTTP functions; to be refactored later. Options are: + url: URL to retrieve + type: GET, PUT, POST etc + callback: function invoked with (err,data) +*/ +$tw.plugins.tiddlyweb.httpRequest = function(options) { + var type = options.type || "GET", + client = new XMLHttpRequest(), + data = "", + f,results; + if(options.data) { + if(typeof options.data === "string") { // Already a string + data = options.data; + } else { // A hashmap of strings + results = []; + for(f in options.data) { + if($tw.utils.hop(options.data,f)) { + results.push(f + "=" + encodeURIComponent(options.data[f])) + } + } + data = results.join("&") + } + } + client.onreadystatechange = function() { + if(this.readyState === 4) { +console.log("onreadystatechange",this.status,this.statusText,this.getAllResponseHeaders()); + if(this.status === 200) { + // success! + options.callback(null,this.responseText); + return; + } + // something went wrong + options.callback(new Error("XMLHttpRequest error: " + this.status)); + } + }; + client.open(type,options.url,true); + if(data) { + client.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8"); + } + client.send(data); + return client; +}; + +})(); diff --git a/tw5tiddlyweb/tiddlers/HelloThere.tid b/tw5tiddlyweb/tiddlers/HelloThere.tid new file mode 100644 index 000000000..9d10e61b3 --- /dev/null +++ b/tw5tiddlyweb/tiddlers/HelloThere.tid @@ -0,0 +1,7 @@ +title: HelloThere + +Experimenting with TiddlyWeb integration for TiddlyWiki5. + +TiddlyWeb login status: (($:/plugins/tiddlyweb/IsLoggedIn)) + +TiddlyWeb username: (($:/plugins/tiddlyweb/UserName)) diff --git a/tw5tiddlyweb/tiddlywiki.info b/tw5tiddlyweb/tiddlywiki.info new file mode 100644 index 000000000..aaea253fc --- /dev/null +++ b/tw5tiddlyweb/tiddlywiki.info @@ -0,0 +1,6 @@ +{ + "plugins": [ + "tiddlywiki/fullscreen", + "tiddlywiki/tiddlyweb" + ] +} \ No newline at end of file diff --git a/tw5tiddlyweb/wiki/DefaultTiddlers.tid b/tw5tiddlyweb/wiki/DefaultTiddlers.tid new file mode 100644 index 000000000..cc98f9dd6 --- /dev/null +++ b/tw5tiddlyweb/wiki/DefaultTiddlers.tid @@ -0,0 +1,3 @@ +title: $:/DefaultTiddlers + +HelloThere diff --git a/tw5tiddlyweb/wiki/SiteSubtitle.tid b/tw5tiddlyweb/wiki/SiteSubtitle.tid new file mode 100644 index 000000000..a4c5a861c --- /dev/null +++ b/tw5tiddlyweb/wiki/SiteSubtitle.tid @@ -0,0 +1,4 @@ +title: SiteSubtitle +type: application/x-tiddler + +for TiddlyWeb \ No newline at end of file diff --git a/tw5tiddlyweb/wiki/SiteTitle.tid b/tw5tiddlyweb/wiki/SiteTitle.tid new file mode 100644 index 000000000..5ef449a9a --- /dev/null +++ b/tw5tiddlyweb/wiki/SiteTitle.tid @@ -0,0 +1,4 @@ +title: SiteTitle +type: application/x-tiddler + +TiddlyWiki5 in the Sky \ No newline at end of file diff --git a/wbld.sh b/wbld.sh new file mode 100755 index 000000000..546e5c688 --- /dev/null +++ b/wbld.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# build the TiddlyWeb edition of TiddlyWiki5 and upload to TiddlySpace + +# usage: +# ./wbld.sh + +pushd tw5tiddlyweb > /dev/null + +node ../core/boot.js \ + --verbose \ + --savetiddler $:/core/templates/tiddlywiki5.template.html ../../jermolene.github.com/tiddlyweb.html text/plain [!is[shadow]]\ + || exit 1 + +popd > /dev/null + +mkdir -p tmp +echo "type: text/html" > tmp/tmp.txt +echo "" >> tmp/tmp.txt +cat ../jermolene.github.com/tiddlyweb.html >> tmp/tmp.txt + +curl -u $1:$2 -X PUT -H "content-type: text/plain" http://tw5tiddlyweb.tiddlyspace.com/bags/tw5tiddlyweb_public/tiddlers/tw5tiddlyweb --data-binary @tmp/tmp.txt