bin/typogruby in typogruby-1.0.7 vs bin/typogruby in typogruby-1.0.8

- old
+ new

@@ -1,12 +1,14 @@ #!/usr/bin/env ruby -rubygems +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'typogruby' require 'optparse' operations = [:smartypants, :initial_quotes, :amp, :widont, :caps] explicit_on = [] explicit_off = [] +output_filename = nil OptionParser.new do |options| options.banner = <<-EOS Improves web typography with text filters. @@ -17,10 +19,12 @@ typogruby FILE Runs all filters typogruby --no-widows FILE Runs all filters except for widows typogruby -w FILE Runs only the filter for widows typogruby --widows FILE Runs only the filter for widows + typogruby -o FILE2 FILE1 Filters FILE1 and saves to FILE2 + cat FILE | typogruby Filter input from STDIN Usage: typogruby [options] filename [filename, ...] EOS options.separator "" @@ -36,25 +40,33 @@ options.on '-a', '--[no-]amps', 'Wrap ampersands' do |v| (v ? explicit_on : explicit_off) << :amp end + options.on '-n', '--[no-]entities', 'Convert special characters to HTML entities' do |v| + (v ? explicit_on : explicit_off) << :entities + end + options.on '-w', '--[no-]widows', 'Prevent widows' do |v| (v ? explicit_on : explicit_off) << :widont end options.on '-c', '--[no-]caps', 'Wrap consecutive capitals' do |v| (v ? explicit_on : explicit_off) << :caps end + options.on '-o', '--output FILENAME', 'Save output to file' do |v| + output_filename = v + end + options.on_tail '-h', '--help', 'Show this message' do $stderr.print options exit end options.on_tail '-v', '--version', 'Display version information' do - $stderr.print Typogruby.version + $stderr.print "Typogruby #{Typogruby.version}" exit end options.parse! end @@ -64,20 +76,36 @@ explicit_on else operations end -unless ARGV.any? - $stderr.puts 'No file specified' +begin + # Apply every filter to our input text + output = operations_todo.inject(ARGF.read) { |t, o| Typogruby.send(o, t) } + +# End the program nicely when the user interrupt with ctrl-c +rescue Interrupt + $stderr.puts "Interrupted by user." exit 1 + +# Inform the user of any errors that have occured +rescue Exception => e + $stderr.puts "Error processing input:" + $stderr.puts e.message + exit 1 end -output = ARGV.inject('') do |output, filename| +# Either use the output_filename to save the output to a file on disk, +# or print it to stdout +if output_filename begin - output + operations_todo.inject(File.read(filename)) { |t, o| Typogruby.send(o, t) } - rescue - $stderr.print "Error processing '#{filename}'" - output + File.open(output_filename, 'w') do |f| + f.print output + end + rescue Exception => e + $stdout.print "A problem occured when trying to write to file #{output_filename}:" + $stdout.print e.message + exit 1 end +else + $stdout.print output end - -$stdout.print output \ No newline at end of file