• Download Gist
Ubiquity--PHP-basic-function-reference-search.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
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);
}
});

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!

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.

Ah, then I used the script different from what it was intended for.
But you're right, we can't use the script until Ubiquity gets updated,
so working on it would be of no use. Let's hope the new Ubiquity arrives
soon!

Op 30-3-2011 22:18, josue schreef:

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.