From cb50e67f76ca1a64cc024118e62dbd2f427ba20a Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 15 Nov 2012 10:48:21 +0100 Subject: [PATCH 1/4] Add lib/ to load path. Allows you to run script directly in shell instead of via gem. --- bin/magic_encoding | 2 ++ 1 file changed, 2 insertions(+) mode change 100644 => 100755 bin/magic_encoding 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) From 44bbbf03f6009d2bc59e8f3510080beb161c35ef Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 15 Nov 2012 10:49:10 +0100 Subject: [PATCH 2/4] Add support for ERB. --- lib/magic_encoding.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/magic_encoding.rb b/lib/magic_encoding.rb index 80ab9a2..1288b23 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" + prefix = "-*- 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}', prefix) + "\n") count += 1 file.pos = 0 From e07a07e4b1e93f190a6d09a9205842d09f8e8328 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 15 Nov 2012 10:49:35 +0100 Subject: [PATCH 3/4] Name variable for what it is. --- lib/magic_encoding.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/magic_encoding.rb b/lib/magic_encoding.rb index 1288b23..1d5f006 100644 --- a/lib/magic_encoding.rb +++ b/lib/magic_encoding.rb @@ -13,7 +13,7 @@ def self.process(options) encoding = options[0] || "utf-8" directory = options[1] || Dir.pwd - prefix = "-*- encoding : #{encoding} -*-" + magic_comment = "-*- encoding : #{encoding} -*-" # TODO : add options for recursivity (and application of the script to a single file) @@ -38,7 +38,7 @@ def self.process(options) end # set current encoding - lines.insert(0, comment_style.sub('{text}', prefix) + "\n") + lines.insert(0, comment_style.sub('{text}', magic_comment) + "\n") count += 1 file.pos = 0 From 157660a749cd9c0da1dcb1fc1226eecffe9c52e7 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 15 Nov 2012 15:59:16 +0100 Subject: [PATCH 4/4] Use magic comment that works with ERB. See the regex used by Rails 2.3 here: https://github.com/rails/rails/blob/2-3-stable/actionpack/lib/action_view/template_handlers/erb.rb#L14 --- lib/magic_encoding.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/magic_encoding.rb b/lib/magic_encoding.rb index 1d5f006..969426c 100644 --- a/lib/magic_encoding.rb +++ b/lib/magic_encoding.rb @@ -13,7 +13,7 @@ def self.process(options) encoding = options[0] || "utf-8" directory = options[1] || Dir.pwd - magic_comment = "-*- encoding : #{encoding} -*-" + magic_comment = "encoding: #{encoding}" # TODO : add options for recursivity (and application of the script to a single file) @@ -21,7 +21,7 @@ def self.process(options) 'rb' => '# {text}', 'rake' => '# {text}', 'haml' => '-# {text}', - 'erb' => '<%# {text} %>', + 'erb' => '<%# {text} -%>', } count = 0