diff --git a/bin/magic_encoding b/bin/magic_encoding old mode 100644 new mode 100755 index d7aee18..27573cf --- a/bin/magic_encoding +++ b/bin/magic_encoding @@ -2,6 +2,8 @@ # A simple tool to prepend magic comments for encoding to multiple ".rb" files +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') + require 'magic_encoding' AddMagicComment.process(ARGV) diff --git a/lib/magic_encoding.rb b/lib/magic_encoding.rb index 80ab9a2..969426c 100644 --- a/lib/magic_encoding.rb +++ b/lib/magic_encoding.rb @@ -1,5 +1,3 @@ -# -*- encoding : utf-8 -*- - # A simple library to prepend magic comments for encoding to multiple ".rb" files module AddMagicComment @@ -15,7 +13,7 @@ def self.process(options) encoding = options[0] || "utf-8" directory = options[1] || Dir.pwd - prefix = "-*- encoding : #{encoding} -*-\n" + magic_comment = "encoding: #{encoding}" # TODO : add options for recursivity (and application of the script to a single file) @@ -23,6 +21,7 @@ def self.process(options) 'rb' => '# {text}', 'rake' => '# {text}', 'haml' => '-# {text}', + 'erb' => '<%# {text} -%>', } count = 0 @@ -34,12 +33,12 @@ def self.process(options) lines = file.readlines # remove current encoding comment(s) - while lines[0].match(/^-?# ?(-\*-)? ?(en)?coding/) + while lines[0].match(/^(<%|-)?# ?(-\*-)? ?(en)?coding/) lines.shift end # set current encoding - lines.insert(0,comment_style.sub('{text}', prefix)) + lines.insert(0, comment_style.sub('{text}', magic_comment) + "\n") count += 1 file.pos = 0