Permalink
Browse files

Converted to CoffeeScript

  • Loading branch information...
1 parent 882f87c commit b5d25114b886aca2b9ed73497772a2f77906e343 @brymck committed Aug 15, 2011
View
@@ -0,0 +1,76 @@
+require 'fileutils'
+task default: :build
+
+def check_tree(*dirs)
+ tree = "."
+ dirs.each do |dir|
+ tree << "/#{dir}"
+ Dir.mkdir tree unless Dir.exists?(tree)
+ end
+end
+
+def top_level?(entry)
+ entry =~ /^\.+$/
+end
+
+task :build do
+ puts
+ Rake::Task[:manifest].invoke
+ Rake::Task[:vendor].invoke
+ Rake::Task[:lib].invoke
+ Rake::Task[:img].invoke
+ Rake::Task[:coffee].invoke
+ Rake::Task[:haml].invoke
+ puts "\nDone!"
+ puts
+end
+
+task :coffee do
+ check_tree "ext", "js"
+ puts "----> Converting CoffeeScript to JavaScript"
+ Dir.entries("./src/coffee/").each do |entry|
+ unless top_level?(entry)
+ puts " #{entry}"
+ result = %x[coffee --compile --output ext/js/ src/coffee/#{entry}]
+ puts result unless result.empty?
+ end
+ end
+end
+
+task :haml do
+ check_tree "ext"
+ puts "----> Converting HTML to HAML"
+ Dir.entries("./src/haml/").each do |entry|
+ unless top_level?(entry)
+ puts " #{entry}"
+ result = %x[haml -f html5 src/haml/#{entry} ext/#{entry.gsub /\.haml$/, ".html"}]
+ puts result unless result.empty?
+ end
+ end
+end
+
+task :img do
+ check_tree "ext", "img"
+ puts "----> Copying images"
+ Dir.entries("./src/img/").each do |entry|
+ unless top_level?(entry)
+ puts " #{entry}"
+ FileUtils.copy "src/img/#{entry}", "ext/img"
+ end
+ end
+end
+
+task :lib do
+ puts "----> Copying library files"
+ FileUtils.copy "lib/js/xlcolorscale-0.5.min.js", "ext/js"
+end
+
+task :manifest do
+ puts "----> Copying manifest"
+ FileUtils.copy "src/manifest.json", "ext"
+end
+
+task :vendor do
+ puts "----> Copying vendor files"
+ FileUtils.copy "vendor/js/jquery-1.6.1.min.js", "ext/js"
+end
View
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang='en'>
+ <head>
+ <meta charset='utf-8'>
+ <title>Vim Ratings Backgrounds</title>
+ </head>
+ <body>
+ <script src='/js/background.js' type='text/javascript'></script>
+ </body>
+</html>
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,49 @@
+(function() {
+ var Background;
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ Background = (function() {
+ var DEFAULTS;
+ DEFAULTS = {
+ rMin: 0,
+ rMax: 1000,
+ dlMin: 0,
+ dlMax: 10000,
+ rprDec: 2,
+ rprMin: -1,
+ rprMax: 4,
+ rpdDec: 3,
+ rpdMin: -0.05,
+ rpdMax: 0.25
+ };
+ Background.prototype.reset = function() {
+ var key, _results;
+ for (key in DEFAULTS) {
+ if (!(key in localStorage)) {
+ localStorage[key] = JSON.stringify(DEFAULTS[key]);
+ }
+ this.options[key] = JSON.parse(localStorage[key]);
+ }
+ _results = [];
+ for (key in localStorage) {
+ _results.push(!(key in DEFAULTS) ? delete localStorage[key] : void 0);
+ }
+ return _results;
+ };
+ function Background() {
+ this.options = {};
+ this.reset();
+ chrome.extension.onRequest.addListener(__bind(function(request, sender, sendResponse) {
+ switch (request) {
+ case "options":
+ return sendResponse({
+ options: this.options
+ });
+ default:
+ return sendResponse({});
+ }
+ }, this));
+ }
+ return Background;
+ })();
+ window.background = new Background;
+}).call(this);
View
@@ -0,0 +1,111 @@
+(function() {
+ var VimRatings, vimRatings;
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ VimRatings = (function() {
+ var RATINGS_PER_DOWNLOAD;
+ RATINGS_PER_DOWNLOAD = /<b>(-?\d+)\/(\d+)<\/b>/;
+ VimRatings.prototype.stripTags = function(str) {
+ return str.replace(/<[^>]+>/g, "");
+ };
+ VimRatings.prototype.colspan = function($node) {
+ return parseInt($node.attr("colspan") || 1);
+ };
+ VimRatings.prototype.getColor = function(value, decimals, min, max, tag) {
+ if (xlcs) {
+ return "<" + tag + " style='background-color:" + (xlcs.convert(value, min, max)) + "'>" + (value.toFixed(decimals)) + "</" + tag + ">";
+ } else {
+ return "<" + tag + ">" + (value.toFixed(decimals)) + "</" + tag + ">";
+ }
+ };
+ VimRatings.prototype.matchUrl = function(search) {
+ return window.location.href.match(search) !== null;
+ };
+ VimRatings.prototype.averageRating = function(scriptId, $node) {
+ return $.get("http://www.vim.org/scripts/script.php", {
+ script_id: scriptId
+ }, __bind(function(data) {
+ var numbers, rpr;
+ numbers = data.match(RATINGS_PER_DOWNLOAD);
+ rpr = numbers[1] === "0" ? 0 : parseInt(numbers[1], 10) / parseInt(numbers[2], 10);
+ return $node.next("td").replaceWith($(this.getColor(rpr, this.options.rprDec, this.options.rprMin, this.options.rprMax, "td")).attr("align", "right"));
+ }, this));
+ };
+ VimRatings.prototype.addToSearchResults = function() {
+ var $lastTd, $results, $td, $tds, $th, $tr, counter, downloads, downloadsCol, isOddRow, rating, ratingCol, scriptId, td, th, tr, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _results;
+ if (this.matchUrl("script_search_results.php")) {
+ $results = $("h1:contains('Search Results')").next("table");
+ counter = 0;
+ _ref = $results.find("tr.tableheader th");
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ th = _ref[_i];
+ $th = $(th);
+ counter += this.colspan($th);
+ switch (this.stripTags($th.text()).toLowerCase()) {
+ case "rating":
+ ratingCol = counter;
+ break;
+ case "downloads":
+ downloadsCol = counter;
+ }
+ }
+ isOddRow = true;
+ _ref2 = $results.find("tr");
+ _results = [];
+ for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
+ tr = _ref2[_j];
+ $tr = $(tr);
+ $tds = $tr.find("td, th");
+ counter = 0;
+ for (_k = 0, _len3 = $tds.length; _k < _len3; _k++) {
+ td = $tds[_k];
+ $td = $(td);
+ counter += this.colspan($td);
+ if (counter === ratingCol) {
+ rating = parseInt($td.text());
+ if (ratingCol > downloadsCol) {
+ $lastTd = $td;
+ break;
+ }
+ } else if (counter === downloadsCol) {
+ downloads = parseInt($td.text());
+ if (downloadsCol > ratingCol) {
+ $lastTd = $td;
+ break;
+ }
+ }
+ }
+ _results.push(typeof $tds.eq(0).attr("colspan") !== "undefined" ? $lastTd.after("<td>").after("<td>") : $tr.hasClass("tableheader") ? $lastTd.after("<th>Rating/<br>Download</th>").after("<th>Average<br>Rating</th>") : (scriptId = $("a:first", $tr).attr("href").match(/script_id=(\d+)/)[1], $tds.eq(ratingCol - 1).css("backgroundColor", xlcs.convert(rating, this.options.rMin, this.options.rMax)), $tds.eq(downloadsCol - 1).css("backgroundColor", xlcs.convert(downloads, this.options.dlMin, this.options.dlMax)), $lastTd.after($(this.getColor(rating / downloads, this.options.rpdDec, this.options.rpdMin, this.options.rpdMax, "td")).attr("align", "right")).after($("<td>").addClass(isOddRow != null ? isOddRow : {
+ "rowodd": "roweven"
+ })), this.averageRating(scriptId, $lastTd), isOddRow = !isOddRow));
+ }
+ return _results;
+ }
+ };
+ VimRatings.prototype.addToScriptKarma = function() {
+ var $karma, $rating, dl, match, numbers, re, rpd, text;
+ if (this.matchUrl("script.php")) {
+ $karma = $("b:contains('script karma')").closest("table");
+ $rating = $karma.find("td:contains('Rating')");
+ text = $rating.text();
+ numbers = [];
+ re = /-?\d+/g;
+ while (match = re.exec(text)) {
+ numbers.push(match);
+ }
+ rpd = " (" + (this.getColor(numbers[0] / numbers[1], this.options.rpdDec, this.options.rpdMin, this.options.rpdMax, "span")) + ") ";
+ dl = "(" + (this.getColor(numbers[0] / numbers[2], this.options.rprDec, this.options.rprMin, this.options.rprMax, "span")) + ") ";
+ $rating.children("b").after(rpd);
+ return $rating.contents().eq(5).after(dl);
+ }
+ };
+ function VimRatings() {
+ chrome.extension.sendRequest("options", __bind(function(response) {
+ this.options = response.options;
+ this.addToSearchResults();
+ return this.addToScriptKarma();
+ }, this));
+ }
+ return VimRatings;
+ })();
+ vimRatings = new VimRatings;
+}).call(this);
File renamed without changes.
View
@@ -0,0 +1,29 @@
+(function() {
+ var Options, options;
+ Options = (function() {
+ function Options() {
+ var $form, bg, key;
+ bg = chrome.extension.getBackgroundPage();
+ $form = $("#options_form");
+ for (key in bg.background.options()) {
+ $form.find("input[name=" + key + "]").val(key);
+ }
+ $form.submit(function() {
+ var $input, input, value, _i, _len, _ref;
+ _ref = $form.find("input[type=text]");
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ input = _ref[_i];
+ $input = $(input);
+ key = $input.attr("name");
+ value = $input.val();
+ localStorage[key] = JSON.stringify(value);
+ }
+ bg.background.reset();
+ alert("Saved!");
+ return false;
+ });
+ }
+ return Options;
+ })();
+ options = new Options;
+}).call(this);
View
@@ -0,0 +1,22 @@
+{
+ "name": "Vim Ratings",
+ "version": "1.0",
+ "description": "Adds average rating and rating per download for vim plugins at vim.org, then color-codes them.",
+ "icons": {
+ "16": "img/icon_16.png",
+ "48": "img/icon_48.png",
+ "128": "img/icon_128.png"
+ },
+ "background_page": "background.html",
+ "content_scripts": [
+ {
+ "matches": ["http://www.vim.org/*"],
+ "js": [
+ "js/jquery-1.6.1.min.js",
+ "js/xlcolorscale-0.5.min.js",
+ "js/content.js"
+ ]
+ }
+ ],
+ "options_page": "options.html"
+}
View
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<html lang='en'>
+ <head>
+ <meta charset='utf-8'>
+ <title>Vim Ratings Options</title>
+ </head>
+ <body>
+ <h1>Vim Ratings Options</h1>
+ <form id='options_form'>
+ <table>
+ <thead>
+ <tr>
+ <th>Metric</th>
+ <th>Minimum</th>
+ <th>Maximum</th>
+ <th>Decimals</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>Rating</td>
+ <td><input name='rMin' type='text'></td>
+ <td><input name='rMax' type='text'></td>
+ <td>0</td>
+ </tr>
+ <tr>
+ <td>Downloads</td>
+ <td><input name='dlMin' type='text'></td>
+ <td><input name='dlMax' type='text'></td>
+ <td>0</td>
+ </tr>
+ <tr>
+ <td>Average Rating</td>
+ <td><input name='rprMin' type='text'></td>
+ <td><input name='rprMax' type='text'></td>
+ <td><input name='rprDec' type='text'></td>
+ </tr>
+ <tr>
+ <td>Rating/Download</td>
+ <td><input name='rpdMin' type='text'></td>
+ <td><input name='rpdMax' type='text'></td>
+ <td><input name='rpdDec' type='text'></td>
+ </tr>
+ </tbody>
+ </table>
+ <input type='submit' value='Save'>
+ </form>
+ <script src='/js/jquery-1.6.1.min.js' type='text/javascript'></script>
+ <script src='/js/options.js' type='text/javascript'></script>
+ </body>
+</html>
@@ -0,0 +1,9 @@
+/*
+ xlColorScale v0.5
+ http://www.brymck.com
+
+ Copyright 2011, Bryan McKelvey
+ Licensed under the MIT License
+ http://www.brymck.com/license
+*/
+var xlColorScale=xlcs=function(){var e=[[248,105,107],[255,235,132],[99,190,123]];return{convert:function(c,b,a){var d=e.length-1;b==null&&(b=0);a==null&&(a=a||100);c<b?c=b:c>a&&(c=a);b=(c-b)/(a-b);a=parseInt(b*(e.length-1),10);a>=d&&(a=d-1);for(var c=a,d=(b-a/d)/((a+1)/d-a/d),f="#",b=0;b<3;b++)a=parseInt(e[c][b]*(1-d)+e[c+1][b]*d,10),a=a.toString(16),f+=(a.length===1?"0":"")+a;return f}}}();
View
@@ -1,10 +0,0 @@
-<!DOCTYPE HTML>
-<html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title>Vim Ratings Background</title>
- </head>
- <body>
- <script src="/js/background.js" type="text/javascript" charset="utf-8"></script>
- </body>
-</html>
Oops, something went wrong. Retry.

0 comments on commit b5d2511

Please sign in to comment.