Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ERB #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/magic_encoding 100644 → 100755
Expand Up @@ -2,6 +2,8 @@


# A simple tool to prepend magic comments for encoding to multiple ".rb" files # 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' require 'magic_encoding'


AddMagicComment.process(ARGV) AddMagicComment.process(ARGV)
9 changes: 4 additions & 5 deletions lib/magic_encoding.rb
@@ -1,5 +1,3 @@
# -*- encoding : utf-8 -*-

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


module AddMagicComment module AddMagicComment
Expand All @@ -15,14 +13,15 @@ def self.process(options)
encoding = options[0] || "utf-8" encoding = options[0] || "utf-8"
directory = options[1] || Dir.pwd 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) # TODO : add options for recursivity (and application of the script to a single file)


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


count = 0 count = 0
Expand All @@ -34,12 +33,12 @@ def self.process(options)
lines = file.readlines lines = file.readlines


# remove current encoding comment(s) # remove current encoding comment(s)
while lines[0].match(/^-?# ?(-\*-)? ?(en)?coding/) while lines[0].match(/^(<%|-)?# ?(-\*-)? ?(en)?coding/)
lines.shift lines.shift
end end


# set current encoding # set current encoding
lines.insert(0,comment_style.sub('{text}', prefix)) lines.insert(0, comment_style.sub('{text}', magic_comment) + "\n")
count += 1 count += 1


file.pos = 0 file.pos = 0
Expand Down