Skip to content

Instantly share code, notes, and snippets.

@gialloporpora
Created December 1, 2008 13:23
Show Gist options
  • Save gialloporpora/30726 to your computer and use it in GitHub Desktop.
Save gialloporpora/30726 to your computer and use it in GitHub Desktop.
Ubiquity command to cite selected text in a web page
noun_type_formatting = new CmdUtils.NounType( "formatting",
["html","bbcode","plain text"]
);
CmdUtils.CreateCommand({
name: "cite",
takes: {"selected text": noun_arb_text},
icon: "http://i250.photobucket.com/albums/gg274/gialloporpora/icone/pencil.png",
homepage: "http://www.gialloporpora.netsons.org/ubiquity",
author: { name: "Sandro Della Giustina", email: "sandrodll@yahoo.it"},
license: "MPL,GPL",
description: "Generate the code to cite selected text in webpages",
help: "You can specify the format type using *in* modifier and the title description using *like* modifier. Default value for formatting is html.",
modifiers: {like: noun_arb_text, in: noun_type_formatting},
preview: function( previewBlock, directObject, modifiers ) {
var code_formatting =modifiers.in.text || "html";
if (directObject.text.length==0) {
previewBlock.innerHTML=CmdUtils.renderTemplate('<b>WARNING:</b> You must select a text in web page to use this command !');
return;
}
var html='Generate the <b>'+code_formatting+'</b> to make a citation of selected text i in Web page.<br/>';
previewBlock.innerHTML=CmdUtils.renderTemplate4(html);
},
execute: function(directObject,modifiers){
var template={
"html": '<a href="${url}" target="_blank">${title}</a><br/><blockquote cite="${url}">\n${cited_text}\n</blockquote>',
"bbcode": '[url=${url}]${title}[/url]\n[quote]\n${cited_text}\n[/quote]',
"plain text": '${url}\n? ${cited_text} ?'};
var doc = CmdUtils.getDocument();
var data={url: doc.location,"title": doc.title, "cited_text":directObject.text};
if (modifiers.like.text.length > 0) data.title=modifiers.like.text;
var style='"background-color: #272727; color:#fff;padding: 5px 7px;-moz-border-radius: 10px;width: 200px; height: 120px;font-size: 12px;font-family: Consolas, "Lucida Console", Courier, monospace;"';
var code_formatting =modifiers.in.text || "html";
var code=CmdUtils.renderTemplate(template[code_formatting],data);
CmdUtils.setSelection('<textarea style='+style+'>'+code+'</textarea>');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment