Permalink
Browse files

tests for launcher; clean up dist task; website

  • Loading branch information...
1 parent 75f5493 commit 2215ef73349ff182c4edd0b82a99bff295720de5 @drnic committed Apr 26, 2008
View
@@ -13,9 +13,11 @@ require 'rake/packagetask'
$:.unshift File.dirname(__FILE__) + "/lib"
APP_VERSION = '0.1.0'
-APP_NAME = 'github_badge'
+APP_NAME = 'github-badge'
RUBYFORGE_PROJECT = 'drnicjavascript'
APP_FILE_NAME= "#{APP_NAME}.js"
+APP_BADGE_NAME= "#{APP_NAME}-launcher"
+APP_BADGE_FILE_NAME= "#{APP_NAME}-launcher.js.erb"
APP_ROOT = File.expand_path(File.dirname(__FILE__))
APP_SRC_DIR = File.join(APP_ROOT, 'src')
@@ -38,14 +40,20 @@ task :dist do
File.open(File.join(APP_DIST_DIR, APP_FILE_NAME), 'w+') do |dist|
dist << Protodoc::Preprocessor.new(APP_FILE_NAME)
end
+ File.open(File.join(APP_DIST_DIR, "#{APP_BADGE_NAME}.js"), 'w+') do |dist|
+ dist << Protodoc::Preprocessor.new(APP_BADGE_FILE_NAME)
+ end
end
Dir.chdir(APP_DIST_DIR) do
FileUtils.copy_file APP_FILE_NAME, "#{APP_NAME}-#{APP_VERSION}.js"
+ FileUtils.copy_file "#{APP_BADGE_NAME}.js", "#{APP_BADGE_NAME}-#{APP_VERSION}.js"
end
if File.directory?("website")
FileUtils.mkdir_p "website/dist"
FileUtils.copy_file "dist/#{APP_FILE_NAME}", "website/dist/#{APP_FILE_NAME}"
FileUtils.copy_file "dist/#{APP_FILE_NAME}", "website/dist/#{APP_NAME}-#{APP_VERSION}.js"
+ FileUtils.copy_file "dist/#{APP_BADGE_NAME}.js", "website/dist/#{APP_BADGE_FILE_NAME}"
+ FileUtils.copy_file "dist/#{APP_BADGE_NAME}.js", "website/dist/#{APP_BADGE_NAME}-#{APP_VERSION}.js"
end
end
@@ -0,0 +1,2 @@
+host: your_username@rubyforge.org
+remote_dir: /var/www/gforge-projects/github_badges
View
@@ -0,0 +1,76 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+begin
+ require 'newgem'
+rescue LoadError
+ puts "\n\nGenerating the website requires the newgem RubyGem"
+ puts "Install: gem install newgem\n\n"
+ exit(1)
+end
+require 'redcloth'
+require 'syntax/convertors/html'
+require 'erb'
+ENV['rakefile_just_config'] = "true"
+load File.dirname(__FILE__) + '/../Rakefile'
+
+version = APP_VERSION
+download = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}/"
+
+class Fixnum
+ def ordinal
+ # teens
+ return 'th' if (10..19).include?(self % 100)
+ # others
+ case self % 10
+ when 1: return 'st'
+ when 2: return 'nd'
+ when 3: return 'rd'
+ else return 'th'
+ end
+ end
+end
+
+class Time
+ def pretty
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
+ end
+end
+
+def convert_syntax(syntax, source)
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
+end
+
+if ARGV.length >= 1
+ src, template = ARGV
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
+
+else
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
+ exit!
+end
+
+template = ERB.new(File.open(template).read)
+
+title = nil
+body = nil
+File.open(src) do |fsrc|
+ title_text = fsrc.readline
+ body_text_template = fsrc.read
+ body_text = ERB.new(body_text_template).result(binding)
+ syntax_items = []
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
+ ident = syntax_items.length
+ element, syntax, source = $1, $2, $3
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
+ "syntax-temp-#{ident}"
+ }
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
+ body = RedCloth.new(body_text).to_html
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
+end
+stat = File.stat(src)
+created = stat.ctime
+modified = stat.mtime
+
+$stdout << template.result(binding)
@@ -1,7 +1,7 @@
var GitHubBadge = GitHubBadge || {};
GitHubBadge.Launcher = new function() {
- var filename = "github_badge"; // without .js
+ var filename = "github-badge-launcher"; // without .js
function requestContent( url, callback ) {
// inserting via DOM fails in Safari 2.0, so brute force approach
@@ -12,26 +12,27 @@ GitHubBadge.Launcher = new function() {
function basePath() {
var scripts = document.getElementsByTagName("script");
for (var i=0; i < scripts.length; i++) {
- if (scripts[i].src && scripts[i].src.match(/filename\.js(\?.*)?/)) {
- return scripts[i].src.replace(/filename\.js(\?.*)?/, '');
+ if (scripts[i].src && scripts[i].src.match(/github-badge-launcher\.js(\?.*)?/)) {
+ return scripts[i].src.replace(/github-badge-launcher\.js(\?.*)?/, '');
}
}
}
this.init = function() {
var libraries = [
[typeof jQuery, "ext/jquery"],
- [typeof jQuery.template, "ext/jquery.template"]
+ // ["undefined", "ext/jquery"],
+ [typeof jQuery != "undefined" && typeof jQuery.template, "ext/jquery.template"]
];
var scripts = document.getElementsByTagName("script");
for (var i=0; i < scripts.length; i++) {
- if (scripts[i].src && scripts[i].src.match(/filename\.js(\?.*)?/)) {
- var path = scripts[i].src.replace(/filename\.js(\?.*)?/, '');
+ if (scripts[i].src && scripts[i].src.match(/github-badge-launcher\.js(\?.*)?/)) {
+ var path = scripts[i].src.replace(/github-badge-launcher\.js(\?.*)?/, '');
for (var i=0; i < libraries.length; i++) {
if (libraries[i][0] == "undefined" || !libraries[i][0]) {
var url = path + libraries[i][1] + ".js";
if (i == libraries.length - 1) {
- requestContent(url, "GitHubBadge.ClassName.loadedLibraries");
+ requestContent(url, "GitHubBadge.Launcher.loadedLibraries");
} else {
requestContent(url);
}
@@ -0,0 +1,7 @@
+<%= include 'HEADER' %>
+
+var GithubBadge = {
+ Version: '<%= APP_VERSION %>',
+};
+
+<%= include 'github-badge-launcher.js' %>
File renamed without changes.
View
@@ -0,0 +1,35 @@
+require 'yaml'
+
+def website_config
+ unless @website_config
+ begin
+ @website_config = YAML.load(File.read(File.expand_path(File.dirname(__FILE__) + "/../config/website.yml")))
+ rescue
+ puts <<-EOS
+To upload your website to a host, you need to configure
+config/website.yml. See config/website.yml.sample for
+an example.
+EOS
+ exit
+ end
+ end
+ @website_config
+end
+
+desc 'Generate website files'
+task :website_generate => [:ruby_env, :dist] do
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
+ end
+end
+
+desc 'Upload website files to rubyforge'
+task :website_upload do
+ host = website_config["host"] # "#{rubyforge_username}@rubyforge.org"
+ remote_dir = website_config["remote_dir"] # "/var/www/gforge-projects/#{PATH}/"
+ local_dir = 'website'
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
+end
+
+desc 'Generate and upload website files'
+task :website => [:website_generate, :website_upload]
@@ -0,0 +1,60 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <title>JavaScript unit test file</title>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <script src="../assets/jsunittest.js" type="text/javascript"></script>
+
+ <link rel="stylesheet" href="../assets/unittest.css" type="text/css" />
+</head>
+<body>
+
+<div id="content">
+
+ <div id="header">
+ <h1>JavaScript unit test file</h1>
+ <p>
+ This file tests <strong>launcher.js</strong>.
+ </p>
+ </div>
+
+ <!-- Log output (one per Runner, via {testLog: "testlog"} option)-->
+ <div id="testlog"></div>
+
+ <script type="text/javascript" charset="utf-8">
+ var GitHubBadge = GitHubBadge || {};
+ GitHubBadge.buildUserBadgeExecuted = false;
+ GitHubBadge.buildUserBadge = function() {
+ GitHubBadge.buildUserBadgeExecuted = true;
+ };
+ </script>
+ <!-- Put sample/test html here -->
+ <div id="sample">
+ <div id="github-badge" width="150px"></div>
+ <script src="../../src/github-badge-launcher.js" type="text/javascript"></script>
+ </div>
+</div>
+
+<script type="text/javascript">
+// <![CDATA[
+
+ new Test.Unit.Runner({
+ "test that jQuery + jQuery.template loaded": function() { with(this) {
+ assert(jQuery, 'jQuery not loaded');
+ assert(jQuery.template, 'jQuery.template not loaded');
+ }},
+
+ "test that badge built": function() { with(this) {
+ assert(GitHubBadge.buildUserBadgeExecuted, 'Badge code was not executed');
+ }}
+ }, {testLog: "testlog"});
+ // For each Test.UnitRunner instance, specify the element id where results will be
+ // published; e.g. <div id="testlog"/> above.
+ // That is, you can have multiple "new Test.Unit.Runner() { ... }" on this page, just
+ // create more <div id="testlog2"></div> etc, and pass the element id to the hash above:
+ // e.g. {testLog: "testlog2"}
+// ]]>
+</script>
+</body>
+</html>
@@ -0,0 +1,92 @@
+/* GitHub Badge, version 0.1.0
+ * (c) 2008 Dr Nic Williams
+ *
+ * GitHub Badge is freely distributable under
+ * the terms of an MIT-style license.
+ * For details, see the web site: http://github.com/drnic/github-badges/tree/master
+ *
+ *--------------------------------------------------------------------------*/
+
+var GithubBadge = {
+ Version: '0.1.0',
+};
+
+var GitHubBadge = GitHubBadge || {};
+GitHubBadge.buildUserBadge = function(username) {
+ (function($){
+ $('#github-badge')
+ .empty()
+ .buildHeader("My projects", username)
+ .buildBody(username)
+ .buildFooter();
+ })(jQuery);
+};
+
+GitHubBadge.loadUserInfo = function(data) {
+ var template = $.template(
+ "<li class='public'>"
+ + "<img src='http://github.com/images/icons/public.png' alt='public'>"
+ + "<strong><a href='${url}'>${name}</a></strong>"
+ +"</li>"
+ );
+ var showMore = $("<div><a href='#' class='more'>Show more</a></div>")
+ .find('a')
+ .click(function(event) {
+ $('#github-badge .body li').show();
+ $('#github-badge .more').hide();
+ return false;
+ });
+ var list = $("<div class='repos'><ul id='repo_listing'></ul></div>");
+ $('#github-badge .body')
+ .empty()
+ .append(list)
+ .append(showMore);
+ list = list.find('ul');
+ orderedRepos = data.user.repositories.sort(GitHubBadge.compareRepos)
+ $.each(orderedRepos, function(index) {
+ list.append(template, this);
+ });
+ $('#github-badge .body li:gt(9)').hide(); // hide extras
+};
+
+GitHubBadge.compareRepos = function(repo1, repo2) {
+ var properties = ['network', 'watched'];
+ for (var i=0; i < properties.length; i++) {
+ var comparison = GitHubBadge.compareReposProperty(repo1, repo2, properties[i]);
+ if (comparison != 0) return comparison;
+ };
+ return 0;
+};
+
+GitHubBadge.compareReposProperty = function(repo1, repo2, property) {
+ if ((property in repo1) && !(property in repo2)) return -1;
+ if (!(property in repo1) && (property in repo2)) return 1;
+ if ((property in repo1) && (property in repo2)) {
+ return repo2[property] - repo1[property];
+ }
+ return 0;
+};
+
+(function($){
+ $.fn.buildBody = function() {
+ return this.append($("<div class='body'>loading...</div>"));
+ };
+
+ $.fn.buildHeader = function(title, username) {
+ var template = $.template(
+ "<legend class='header'>${title} <span>("
+ + "<a href='http://github.com/${username}'>${username}</a>)"
+ + "</span></legend>")
+ return this.append(template, { title: title, username: username });
+ };
+
+ $.fn.buildFooter = function() {
+ return this.append($(
+ "<div class='footer'>"
+ + "Powered by <a href='http://github.com'>GitHub</a> | "
+ + "Written by <a href='http://drnicwilliams.com'>Dr Nic</a>"
+ + "</div>"
+ + "</fieldset>"
+ ));
+ };
+})(jQuery);
Oops, something went wrong.

0 comments on commit 2215ef7

Please sign in to comment.