Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/magic_encoding
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 4 additions & 5 deletions lib/magic_encoding.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- encoding : utf-8 -*-

# A simple library to prepend magic comments for encoding to multiple ".rb" files

module AddMagicComment
Expand All @@ -15,14 +13,15 @@ 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)

extensions = {
'rb' => '# {text}',
'rake' => '# {text}',
'haml' => '-# {text}',
'erb' => '<%# {text} -%>',
}

count = 0
Expand All @@ -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
Expand Down