1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Added preliminary TiddlyWeb/TiddlySpace plugin

Visit http://tw5tiddlyweb.tiddlyspace.com/tw5tiddlyweb to try it out
This commit is contained in:
Jeremy Ruston 2012-11-16 17:01:04 +00:00
parent db3a4651a2
commit e55db48bd5
8 changed files with 212 additions and 0 deletions

View File

@ -0,0 +1,7 @@
{
"title": "$:/plugins/tiddlywiki/tiddlyweb",
"description": "TiddlyWeb and TiddlySpace components",
"author": "JeremyRuston",
"version": "0.0.0",
"coreVersion": ">=5.0.0"
}

View File

@ -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;
};
})();

View File

@ -0,0 +1,7 @@
title: HelloThere
Experimenting with TiddlyWeb integration for TiddlyWiki5.
TiddlyWeb login status: (($:/plugins/tiddlyweb/IsLoggedIn))
TiddlyWeb username: (($:/plugins/tiddlyweb/UserName))

View File

@ -0,0 +1,6 @@
{
"plugins": [
"tiddlywiki/fullscreen",
"tiddlywiki/tiddlyweb"
]
}

View File

@ -0,0 +1,3 @@
title: $:/DefaultTiddlers
HelloThere

View File

@ -0,0 +1,4 @@
title: SiteSubtitle
type: application/x-tiddler
for TiddlyWeb

View File

@ -0,0 +1,4 @@
title: SiteTitle
type: application/x-tiddler
TiddlyWiki5 in the Sky

22
wbld.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
# build the TiddlyWeb edition of TiddlyWiki5 and upload to TiddlySpace
# usage:
# ./wbld.sh <tiddlyspace username> <tiddlyspace password>
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