Skip to content

Commit

Permalink
Added bin/beautify_js, which is a wrapper for rhino beautify-cl.js that
Browse files Browse the repository at this point in the history
also lets you supply input via standard input rather than suppyling a
filename. This allows it to be used in vim as a filter command.
  • Loading branch information
TylerRick committed Feb 22, 2009
1 parent 0f6090c commit 25cd124
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions bin/beautify_js
@@ -0,0 +1,53 @@
#!/usr/bin/ruby

def print_usage
puts <<End
Usage: #{File.basename($0)} file
Usage: echo 'JavaScript input' | #{File.basename($0)}
Using this is a vim filter command
----------------------------------
This can also be used as a vim filter command (see help filter).
Simply select the lines to be beautified in visual mode and type .!beautify_js.
Better yet, create a vim command to execute it for you, and put that in your .vimrc:
command! -range=% -nargs=0 BeautifyJavascript <line1>,<line2>!#{$0}
Then you can simply type BeautifyJavascript to process the entire buffer or select a range of lines to only pass those lines through the filter.
End
exit
end


if STDIN.tty?
if ARGV.size >= 1
# Get the absolute path of the filename given
require 'pathname'
path = Pathname.new(ARGV[0]).realpath.to_s
else
print_usage
end
else
# Assume they are piping the input in. Save that input in a temporary file and pass that file to beautify-cl.js
require 'tempfile'
file = Tempfile.new('beautify_js')
file.puts STDIN.read
file.close
path = file.path
end
#system "cat #{path}"


# Change directory so that the load() calls in beautify-cl.js are able to find the files they need
Dir.chdir File.dirname(__FILE__)
Dir.chdir '..'
#puts Dir.getwd


command = "rhino beautify-cl.js '#{path}' 2>&1"
#puts command
#output = `#{command}`
system command

0 comments on commit 25cd124

Please sign in to comment.