Skip to content

Instantly share code, notes, and snippets.

  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save josue/147517 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
names: ["php"],
icon: "http://static.php.net/www.php.net/favicon.ico",
description: "Check PHP function specs and comments. (v0.4)",
help: "PHP",
author: {name: "Josue Rodriguez", email: "josue@josuerodriguez.com"},
license: "MIT",
homepage: "http://josuerodriguez.com",
arguments: [
{role: "object", label: 'function', nountype: noun_arb_text}
],
preview: function preview(pblock, args) {
var action = '', keyword = '', content = '';
var tips = "Displays function &amp; examples: <b class='tip'>${FUNC}</b><br>";
tips += "Displays only description: <b class='tip'>${FUNC} desc</b> or <b class='tip'>${FUNC} description</b><br>";
tips += "Displays only parameters: <b class='tip'>${FUNC} params</b> or <b class='tip'>${FUNC} parameters</b><br>";
tips += "Displays only notes: <b class='tip'>${FUNC} notes</b><br>";
tips += "Displays only user contibuted notes: <b class='tip'>${FUNC} comments</b><br>";
tips += "Displays All: <b class='tip'>${FUNC} all</b>";
var lookup = jQuery.trim(args.object.html.toString().toLowerCase()).split(' ');
if(lookup.length>1) {
keyword = jQuery.trim(lookup[0]);
var other = [];
for(i=1;i<lookup.length;i++){ other.push(lookup[i]); }
action = jQuery.trim(other.join(' '));
}
else {
keyword = jQuery.trim(lookup[0]);
action = '';
}
// start PHP lookup...
if(keyword!='') {
var search_keyword = keyword.replace(/\_/gi,'-');
var remove_chrome = "chrome:\/\/ubiquity\/content\/";
var domain = "http://us.php.net/";
var default_url = domain + "manual/en/";
var url = default_url+"function."+search_keyword+".php";
var ajax_loading_image = "http://www.ajaxload.info/cache/FF/FF/FF/00/00/00/37-1.gif";
var failed_image = "http://fc01.deviantart.com/fs31/f/2008/218/4/f/facepalm_by_Oldish_Acoustic.gif";
// Display ajax waiting while retrieving ajax response ...
switch(action) {
case "desc": case "description":
pre_text = "Searching <i>parameters</i> for <b>"+keyword+"</b>";
break;
case "params": case "parameters":
pre_text = "Searching <i>parameters</i> for <b>"+keyword+"</b>";
break;
case "notes":
pre_text = "Searching <i>notes</i> for <b>"+keyword+"</b>";
break;
case "comments":
pre_text = "Searching <i>comments</i> for <b>"+keyword+"</b>";
break;
case "example": case "examples":
pre_text = "Searching <i>examples</i> for <b>"+keyword+"</b>";
break;
case "code": case "function": default:
pre_text = "Searching for function <b>"+keyword+"</b>";
}
pblock.innerHTML = pre_text+" ... <br><br><img src='"+ajax_loading_image+"' />";
// Check for the Ajax response and continue ...
jQuery.ajax({url:url,
success:function(html){
html = "<div id='full_view_php'>" + html + "</div>";
// Fix a[href] and img[src] links ...
html = jQuery(html).find("a,img").each(function(e){
if(this.src) {
$(this).attr('border',0);
this.src = this.src.replace(/^\//,domain);
}
else {
link = this.href.replace(/^\//,default_url).replace(remove_chrome,default_url);
this.href = (link.match(/^http:\/\//gi) ? link : "javascript:void(0)");
}
}).parents("#full_view_php").html();
var action_non1 = "<div id='php_action_non' style='display:inline-block;border:1px dotted #eee;padding:2px 5px 2px 5px'>";
action_non1 += "Sorry, this function does not have ";
var action_non2 = ".</div>";
// Lets get the actual content we really want now ...
switch(action) {
case "desc": case "description":
pre_title = "<h4>Desription for <span style='color:yellow'>&quot;"+keyword+"&quot;</span></h4>";
post_text = jQuery(html).find(".description").html();
content = pre_title + (post_text!=null ? post_text : action_non1 + "description" + action_non2);
break;
case "params": case "parameters":
pre_title = "<h4>Parameters for <span style='color:yellow'>&quot;"+keyword+"&quot;</span></h4>";
post_text = jQuery(html).find(".parameters").html();
content = pre_title + (post_text!=null ? post_text : action_non1 + "parameters" + action_non2);
break;
case "example": case "examples":
pre_title = "<h4>Examples for <span style='color:yellow'>&quot;"+keyword+"&quot;</span></h4>";
post_text = jQuery(html).find(".examples").html();
content = pre_title + (post_text!=null ? post_text : action_non1 + "examples" + action_non2);
break;
case "notes":
pre_title = "<h4>Notes for <span style='color:yellow'>&quot;"+keyword+"&quot;</span></h4>";
post_text = jQuery(html).find(".notes").html();
content = pre_title + (post_text!=null ? post_text : action_non1 + "notes" + action_non2);
break;
case "comments":
pre_title = "<h4>User Contibuted Notes for <span style='color:yellow'>&quot;"+keyword+"&quot;</span></h4>";
post_text = jQuery(html).find("#usernotes").html();
content = pre_title + (post_text!=null ? post_text : action_non1 + "User Contibuted Notes" + action_non2);
break;
case "all":
mid_text = "<hr><h3><span style='color:yellow'>User Contributed Notes</span></h3><br>";
content1 = jQuery(html).find(".refentry").html();
content2 = jQuery(html).find("#usernotes").html();
content = content1 + mid_text + content2;
break;
default:
content = jQuery(html).find(".refentry").html();
}
pblock.innerHTML = content;
},
error:function(html){
var remove_chrome = "chrome:\/\/ubiquity\/manual\/en\/";
var url_search_other = "http://us2.php.net/manual-lookup.php?lang=en&pattern="+search_keyword;
// If the function URL did not exist (404) then lets get list of similar functions ...
jQuery.ajax({url:url_search_other,
success:function(html){
// Fix a[href] and img[src] links ...
html = jQuery(html).find("#content").find("a,img").each(function(e){
if(this.src) {
$(this).attr('border',0);
this.src = this.src.replace(/^\//,domain);
}
else {
link = this.href.replace(/^\//,default_url).replace(remove_chrome,default_url);
this.href = (link.match(/^http:\/\//gi) ? link : "javascript:void(0)");
}
}).parents("#content").html();
// Lets organize our data for display ...
not_found = "Doh! Could not find function <b><span style='color:yellow'>&quot;"+keyword+"&quot;</span></b> ...<br><br><img src='"+failed_image+"' /><br><br>";
var previewTemplate = ("<div id='full_view_php'>"+ not_found + html +_("<b>PHP Search Tips:</b><br><br>"+tips)+"</div>");
var previewData = { FUNC: keyword };
content = CmdUtils.renderTemplate(CmdUtils.renderTemplate(previewTemplate, previewData));
pblock.innerHTML = jQuery(content).find("b.tip").css('color','yellow').parents('#full_view_php').html();
},
error:function(html){
pblock.innerHTML = "PHP Ajax Error: Illegal Malformed URL<br><br><a href='"+url_search_other+"'>"+url_search_other+"</a>";
}
});
}
});
}
else {
// Render a preview of 'PHP Search Tips' ...
var previewTemplate = ("<div id='full_view_php'>"+_("<b>PHP Search Tips:</b><br><br>"+tips)+"</div>");
var previewData = { FUNC: "[func]"};
content = CmdUtils.renderTemplate(CmdUtils.renderTemplate(previewTemplate, previewData));
pblock.innerHTML = jQuery(content).find("b.tip").css('color','yellow').parents('#full_view_php').html();
}
// end PHP lookup.
},
execute: function execute(args) {
displayMessage("You selected: " + args.object.text, this);
}
});
@rubenvandeven
Copy link

It's a great script, but wouldn't it be more usefull to replace displayMessage(..) [line 166], with an action that would load the PHP website, and search for the selected function?
I don't really know the Ubiquity API, but I think something like this should work:
execute: function execute(args) {
Utils.openUrlInBrowser(
"http://nl.php.net/search.php?show=quickref&lang=en&pattern=" +
encodeURIComponent(args.object.text) );
}
Thanks!

@josue
Copy link
Author

josue commented Mar 30, 2011

Glad you liked the script. If i remember correctly, that line served to get highlighted word and look up the php function right away and add the php content into the Ubiquity content pane, hence letting the user to see function params and continue development without having to go to the php site. (i maybe wrong but its been more than a year). I hope to update this script as soon as Ubiquity is available for FF 4.0.

@rubenvandeven
Copy link

rubenvandeven commented Mar 31, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment