Skip to content

Commit

Permalink
Added the dependants searching js
Browse files Browse the repository at this point in the history
  • Loading branch information
ioncache committed Oct 13, 2010
1 parent 30bd9d7 commit 8e7640e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 8 deletions.
53 changes: 45 additions & 8 deletions app.psgi
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use Plack::App::Proxy;
use Plack::Builder;

my $pod_highlight = q[
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shCore.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushPerl.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushJScript.js"></script>
<link href="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shCore.css" rel="stylesheet" type="text/css" />
<link href="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://github.com/oalders/cpan-mangler/raw/master/js/deps.js"></script>
<link type="text/css" href="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shCore.css" rel="stylesheet" />
<link type="text/css" href="http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shThemeDefault.css" rel="stylesheet" />
<style type="text/css">.highlight {background:yellow}</style>
<script type="text/javascript">
Expand All @@ -28,6 +29,42 @@ $(document).ready(function() {
SyntaxHighlighter.defaults['gutter'] = false;
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all();
for each ( sr in document.getElementsByTagName( 'h2' ) ) {
// parse the page to get the list of modules
module = sr.childNodes[0].childNodes[0].innerHTML;
// keep a reference to the line that shows information about the module
infoblock = find_sibling_by_tagname(sr, "SMALL", "P", 1);
if ( ! infoblock ) {
// edge case - there is no description for this module
infoblock = find_sibling_by_tagname(sr, "SMALL", "P", 0);
}
// keep track of the module distribution
if ( infoblock && infoblock.childNodes[1] ) {
dist = infoblock.childNodes[1].href;
}
if ( dist ) {
dist = dist.replace(/\/$/, '');
dist = dist.replace(/^.*\//, '');
dists_by_module[module] = dist;
infoblocks_by_module[module] = infoblock;
dists[dist] = 1;
}
}
// loop through the dists to get the number cpan dependents
// a callback will update the page with the number of dependents for each module
for ( var dist in dists ) {
num_dists++;
}
for ( var dist in dists ) {
gather_cpan_dependents(dist);
}
});
</script>
Expand All @@ -51,23 +88,23 @@ $(document).ready(function() {

my $app = builder {
#enable "Debug", panels => [qw(Environment Memory Timer Response)];
mount "/source" => builder {
mount '/source' => builder {
enable 'HTMLify',
set_head => $source_highlight,
set_body_start => qq[<pre class="brush: pl">],
set_body_end => qq[</pre>];
set_body_start => '<pre class="brush: pl">',
set_body_end => '</pre>';
;
enable 'SimpleContentFilter', filter => sub {
encode_entities($_);
};
Plack::App::Proxy->new( remote => 'http://cpansearch.perl.org/src/' )->to_app;
};
mount "/" => builder {
mount '/' => builder {
enable 'SimpleContentFilter', filter => sub {
s{</head>}{$pod_highlight</head>}i;
s{/src/}{/source/}gi;
};
enable "+HTML::Highlighter", param => "query";
enable '+HTML::Highlighter', param => 'query';
Plack::App::Proxy->new( remote => 'http://search.cpan.org/' )->to_app;
};
};
Expand Down
93 changes: 93 additions & 0 deletions js/deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var infoblocks_by_module = [];
var dists_by_module = [];
var dists = [];
var dependent_counts = [];
var num_dists_fetched = 0;
var num_dists = 0;

// the DOM for search.cpan.org isn't easy to parse
// this function recursively finds DOM siblings by tagname
function find_sibling_by_tagname(start, tagname, failsafe, iteration) {
next = start.nextSibling;
if ( ! next ) {
return;
}
if ( next.tagName ) {
if ( next.tagName == failsafe ) {
return;
}
if ( next.tagName == tagname ) {
if ( ! iteration || iteration == 0 ) {
return next;
}
else {
iteration = iteration - 1;
}
}
}
return find_sibling_by_tagname(next, tagname, failsafe, iteration);
}

// this queries the external deps.cpantesters.org for the distribution
// and counts the number of dependents
// and calls the callback to have the page updated for all modules that are part of the distribution
function gather_cpan_dependents(dist) {
GM_xmlhttpRequest({
method: "GET",
url: 'http://deps.cpantesters.org/depended-on-by.pl?dist=' + escape(dist),
onload: function( resp ) {
num = '0';
if ( resp.responseText ) {
num = resp.responseText.split('<li>').length-1;
}
dependent_counts[dist] = num;
add_dependents_to_page(dist);
num_dists_fetched += 1;
if ( num_dists_fetched == num_dists ) {
show_top_dists();
}
}
});
}

// this is the callback to update the page
// for the distribution, it updates the page for each module that is part of the distribution
function add_dependents_to_page(dist) {
for ( var module in infoblocks_by_module ) {
if ( infoblocks_by_module[module] && dists_by_module[module] == dist ) {
infoblocks_by_module[module].innerHTML += "(<a href=\"http://deps.cpantesters.org/depended-on-by.pl?dist=" + dists_by_module[module] + "\">CPAN dependents</a>: " + dependent_counts[dist] + ")";
}
}
}

function show_top_dists() {
var newP = document.createElement("p");
var txt = 'Distributions in order of number of CPAN dependents: <pre>';
dependent_counts = assocSort(dependent_counts);
for ( var dist in dependent_counts ) {
txt += dist + " (" + dependent_counts[dist] + " dependents)";
txt += "\n";
for ( module in infoblocks_by_module ) {
if ( infoblocks_by_module[module] && dists_by_module[module] == dist ) {
txt += " " + module + "\n";
}
}
txt += "\n";
}
txt += "</pre>";
newP.innerHTML = txt;
document.getElementsByTagName('h2')[0].parentNode.insertBefore(newP,document.getElementsByTagName('h2')[0].previousSibling);
}

// found this function on the interwebs for sorting an associative array by value
function assocSort (oAssoc) {
var idx; var key; var arVal = []; var arValKey = []; var oRes = {};
for (key in oAssoc) {
arVal[arVal.length] = oAssoc[key];
arValKey[oAssoc[key]] = key;
}
arVal.sort().reverse();
for (idx in arVal)
oRes[arValKey[arVal[idx]]] = arVal[idx];
return oRes;
}

0 comments on commit 8e7640e

Please sign in to comment.