diff --git a/README.markdown b/README.markdown index c478e22..7f3e785 100644 --- a/README.markdown +++ b/README.markdown @@ -85,4 +85,4 @@ whichever is your favorite. ## Other Browers - [Firefox Add-on](https://github.com/rlr/dotjs-addon) -- [Safari Extensions](https://github.com/wfarr/dotjs.safariextension) +- [Safari Extension](https://github.com/wfarr/dotjs.safariextension) diff --git a/bin/djsd b/bin/djsd index fe22a52..71cb89c 100755 --- a/bin/djsd +++ b/bin/djsd @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# -*- coding: utf-8 -*- if (%w( -h --help -help help ) & ARGV).length > 0 puts "usage: djsd [-hv]" @@ -15,18 +16,39 @@ require 'webrick' dotjs = Class.new(WEBrick::HTTPServlet::AbstractServlet) do def do_GET(request, response) - file = File.expand_path("~/.js/#{request.path.gsub('/','')}") - default = File.expand_path("~/.js/default.js") - body = '' - body << File.read(default) + "\n" if File.exists?(default) - body << File.read(file) if File.exists?(file) + body << js('default.js') + body << js(request.path.to_s.gsub('/','')) response.status = body.empty? ? 204 : 200 response['Access-Control-Allow-Origin'] = '*' response['Content-Type'] = 'text/javascript' response.body = body end + + def js(path) + if File.exists? path = File.expand_path("~/.js/#{path}") + File.read(path) + ";\n" + elsif File.exists? path = path.sub(/js$/, 'coffee') + coffee(path) + else + '' + end + end + + def coffee(path) + js = `coffee -bp #{path} 2>&1` + + case $?.exitstatus + when 0 + js + when 127 + "console.error('dotjs — `coffee\\' executable not found');" + else + error = js.split("\n").first + "console.error('dotjs — %s', #{error.inspect});" + end + end end server = WEBrick::HTTPServer.new(:Port => 3131)