Skip to content

Loading…

Support for ERB #10

Open
wants to merge 4 commits into from

1 participant

Commits on Nov 15, 2012
  1. @airblade

    Add lib/ to load path.

    airblade committed
    Allows you to run script directly in shell instead of via gem.
  2. @airblade

    Add support for ERB.

    airblade committed
  3. @airblade

    Name variable for what it is.

    airblade committed
  4. @airblade
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 5 deletions.
  1. +2 −0 bin/magic_encoding
  2. +4 −5 lib/magic_encoding.rb
View
2 bin/magic_encoding 100644 → 100755
@@ -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)
View
9 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
Something went wrong with that request. Please try again.