Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Created May 4, 2010 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdlrobson/389638 to your computer and use it in GitHub Desktop.
Save jdlrobson/389638 to your computer and use it in GitHub Desktop.
tags: systemConfig
/***
|Name|PublishCommand|
|Version|0.21|
|Author|BenGillies|
|Description|Publish a tiddler by moving or copying it from one bag to another|
!Usage
Add {{{publishtiddler}}} to your ToolbarCommands tiddler.
You can override the default bag to publish to, and the default publishing mechanism in the PublishConfig shadow tiddler.
You can also override the defaults by setting the appropriate fields on the tiddler. These are the same as the options in PublishConfig.
Options for publishlevel are:
*copy
*move
!Requires
TiddlyWeb - http://tiddlyweb.com
chrjs - http://github.com/tiddlyweb/chrjs/raw/master/main.js
***/
//{{{
config.shadowTiddlers['PublishConfig'] = '|publishtobag|common|\n|publishlevel|copy|';
config.macros.publishtiddler= {
lingo:{
tooltip:"",
'text-copy':"copy tiddler",
'text-move':"move tiddler"
}
,handler: function(place,macroName,params,wikifier,paramString,tiddler){
var p = paramString.parseParams("anon",null,true);
var destination = getParam(p,"destination") ||params[0];
var level = getParam(p,"type") ||params[1] || "copy";
var action = function(){
config.commands.publishtiddler.publishToBag(tiddler,destination,level);
story.closeTiddler(tiddler.title);
story.displayTiddler(null,tiddler.title);
}
var btn = createTiddlyButton(place,this.lingo["text-"+level],this.lingo.tooltip,action,"publishTiddlerButton");
}
}
config.commands.publishtiddler = {
text: "publish",
tooltip: "publish this so everyone can see it",
confirmMsg: "Are you sure you want to publish this?",
saveFirstMsg: "Please save this first!",
lingo:{
publishSuccess: "%s successfully copied to %s.",
publishLevelFail: 'Incorrect publish level set. '
}
,publishToBag: function(tiddler,publishToBag,publishLevel,callback){
var t = tiddler;
var lingo = config.commands.publishtiddler.lingo;
if(publishLevel) {
var newTiddler = new tiddlyweb.Tiddler(t.title, new tiddlyweb.Bag(publishToBag, config.defaultCustomFields['server.host']));
newTiddler.tags = t.tags;
newTiddler.modified = t.modified.convertToYYYYMMDDHHMM() + '00';
newTiddler.created = t.created.convertToYYYYMMDDHHMM() + '00';
newTiddler.modifier = t.modifier;
newTiddler.fields = merge({}, t.fields);
newTiddler.text = t.text;
newTiddler.put(function() {
if (publishLevel === 'copy') {
if(callback)callback();
var msg = lingo.publishSuccess;
msg = msg.replace("%s",newTiddler.title);
msg = msg.replace("%s",newTiddler.bag.name);
displayMessage(msg);
return;
} else if (publishLevel === 'move') {
//delete the original tiddler
if (t.fields['server.bag']) {
newTiddler.bag = new tiddlyweb.Bag(t.fields['server.bag'], config.defaultCustomFields['server.host']);
} else {
var container = t.fields['server.workspace'].split('/');
if (container === 'recipes') {
newTiddler.bag = null;
newTiddler.recipe = new tiddlyweb.Recipe(container[1], config.defaultCustomFields['server.host']);
} else {
newTiddler.bag = new tiddlyweb.Bag(container[1], config.defaultCustomFields['server.host']);
}
}
newTiddler['delete'](function() {
var containerName = newTiddler['bag'] ? newTiddler.bag.name : newTiddler.recipe.name;
displayMessage(newTiddler.title + ' successfully moved to ' + publishToBag + '.');
newTiddler.bag = new tiddlyweb.Bag(publishToBag, config.defaultCustomFields['server.host']);
newTiddler.get(function(data) {
var newRevision = data.revision;
var newWorkspace = 'bags/' + publishToBag;
var newT = store.getTiddler(newTiddler.title);
var newFields = merge({}, newT.fields);
newFields['server.page.revision'] = newRevision;
newFields['server.workspace'] = newWorkspace;
newFields['server.bag'] = publishToBag;
delete newFields['server.recipe'];
newT.fields = merge({}, newFields);
var dirty = store.isDirty();
store.saveTiddler(newT.title, newT.title, newT.text, newT.modifier, newT.modified, newT.tags, newT.fields, true, newT.created, newT.creator);
story.setDirty(newT.title);
if (!dirty) store.setDirty(false);
if(callback)callback();
});
}, function() {
displayMessage('Error removing ' + newTiddler.title + ' from ' + containerName + '.');
displayMessage(newTiddler.title + ' was successfully copied to ' + publishToBag + '.');
});
} else {
alert(lingo.publishLevelFail);
}
}, function() {
displayMessage('Error publishing ' + newTiddler.title + '.');
});
} else {
alert("no publish level set!");
}
}
,handler: function(event,src,title) {
var t = store.getTiddler(title);
if(!t){
alert(this.saveFirstMsg);
}
if (!confirm(this.confirmMsg)) {
return false;
}
var publishToBag = (t.fields['publishtobag']) ? t.fields['publishtobag'] : store.getTiddlerSlice('PublishConfig', 'publishtobag');
var publishLevel = (t.fields['publishlevel']) ? t.fields['publishlevel'] : store.getTiddlerSlice('PublishConfig', 'publishlevel');
this.publishToBag(t,publishToBag,publishLevel);
}
};
//}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment