diff --git a/rubberin b/rubberin index 6f7a64f..03e6365 100755 --- a/rubberin +++ b/rubberin @@ -25,17 +25,18 @@ # Public License for more details. # # You should have received a copy of the GNU General Public License along -# with this p rogram; if not, write to the Free Software Foundation, Inc., +# with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -require 'getoptlong' -require 'rb-inotify' -require 'pathname' +require "getoptlong" +require "rb-inotify" +require "pathname" # Small necessary Pathname class extension. class Pathname def extname=(ext) - raise "Invalid extension" unless ext.is_a? String and ext.match(/^\./) + raise "Invalid extension" unless ext.is_a?(String) && ext.match(/^\./) + @path.sub!(/#{extname}$/, ext) end end @@ -44,7 +45,7 @@ end # Print the usage. def usage - puts "Usage: #{Program} [options]... [tex-file] [rubber-options]..." + puts "Usage: #{PROGRAM} [options]... [tex-file] [rubber-options]..." end # Print the command line help. @@ -62,34 +63,35 @@ end # Compile the input file using the right options based on the mode. def compile(infile, mode) - mode_opt = {:pdf => "--pdf", - :ps => "--ps", - :pspdf => "--ps --pdf"}[mode] - params = ARGV.join(' ') - err_file = infile.with_extname('err') + mode_opt = { pdf: "--pdf", + ps: "--ps", + pspdf: "--ps --pdf" }[mode] + params = ARGV.join(" ") + err_file = infile.with_extname("err") _ret = system "rubber --inplace #{mode_opt} #{params} #{infile} 2> #{err_file}" File.open(err_file) { |file| puts file.read } # Remove the output save file if compile was succesful. - #clean(infile) if ret + # clean(infile) if ret end # Start the right viewer based on the mode. def view(infile, mode) case mode - when :dvi - exec "xdvi.bin", "-watchfile", "1", - "-name", "xdvi", infile.with_extname('dvi').to_s - when :ps - exec "evince", infile.with_extname('ps').to_s - when :pdf, :pspdf - exec "evince", infile.with_extname('pdf').to_s + when :dvi + exec "xdvi.bin", + "-watchfile", "1", + "-name", "xdvi", infile.with_extname("dvi").to_s + when :ps + exec "evince", infile.with_extname("ps").to_s + when :pdf, :pspdf + exec "evince", infile.with_extname("pdf").to_s end end # Clean up cruft related to compilation of the input file. def clean(infile) - err_file = infile.with_extname('err') + err_file = infile.with_extname("err") File.unlink(err_file) if err_file.exist? system "rubber --clean --inplace #{infile}" end @@ -97,55 +99,55 @@ end # Reload the right viewer based on the mode. def reload(infile, mode) case mode - when :ps - system "evince", infile.with_extname('ps').to_s - when :pdf, :pspdf - system "evince", infile.with_extname('pdf').to_s + when :ps + system "evince", infile.with_extname("ps").to_s + when :pdf, :pspdf + system "evince", infile.with_extname("pdf").to_s end end ## Initialisation -Program = File.basename $0 -Version = '0.6' +PROGRAM = File.basename($PROGRAM_NAME).freeze +VERSION = "0.6".freeze # Parse the command line options. # Determine the compile mode from the options. mode = :dvi -opts = GetoptLong.new( - [ "--help", "-h", GetoptLong::NO_ARGUMENT ], - [ "--ps", "-p", GetoptLong::NO_ARGUMENT ], - [ "--pdf", "-d", GetoptLong::NO_ARGUMENT ], - [ "--version", "-v", GetoptLong::NO_ARGUMENT ]) +opts = GetoptLong.new(["--help", "-h", GetoptLong::NO_ARGUMENT], + ["--ps", "-p", GetoptLong::NO_ARGUMENT], + ["--pdf", "-d", GetoptLong::NO_ARGUMENT], + ["--version", "-v", GetoptLong::NO_ARGUMENT]) opts.ordering = GetoptLong::REQUIRE_ORDER opts.quiet = true -opts.each do |opt, arg| +opts.each do |opt, _arg| case opt - when "--help" - help - exit 0 - when "--ps" - mode = :ps - when "--pdf" - mode = if mode == :ps then :pspdf - else :pdf - end - when "--version" - puts "#{Program} #{Version}" - exit 0 + when "--help" + help + exit 0 + when "--ps" + mode = :ps + when "--pdf" + mode = if mode == :ps then :pspdf + else :pdf + end + when "--version" + puts "#{PROGRAM} #{VERSION}" + exit 0 end end # Get the input file from the arguments. -if ARGV.length < 1 +if ARGV.empty? usage exit 1 else infile = Pathname.new(ARGV.shift) def infile.base - self.basename(self.extname) + basename(extname) end + def infile.with_extname(ext) - file = self.base + file = base file.extname = ".#{ext}" file end @@ -154,8 +156,8 @@ end # Check the input file. begin File.open(infile) {} -rescue => err - puts "#{Program}: #{err}" +rescue SystemCallError => e + puts "#{PROGRAM}: #{e}" exit 2 end @@ -177,9 +179,10 @@ view(infile, mode) if not viewer_pid exit end end -# If xdvi exits, this program should exit too. + +# If xdvi/evince exits, this program should exit too. Signal.trap("CLD") do - puts "#{Program}: viewer exited, so will I!" + puts "#{PROGRAM}: viewer exited, so will I!" clean(infile) exit end @@ -195,6 +198,7 @@ dirs.each do |dir| # Only compile if a dependency of the input file has been modified file_path = (dir + ev.name).to_s next unless files.include? file_path + puts "I: file #{ev.name} modified, compiling #{infile}..." compile(infile, mode) reload(infile, mode)