Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit f611853561bb0b805a0375bf10bf62b8a9b82b88 Manuel Ryan committed Aug 3, 2010
Showing with 123 additions and 0 deletions.
  1. +3 −0 Changelog
  2. +20 −0 LICENCE
  3. +36 −0 README.rdoc
  4. +12 −0 bin/magic_encoding
  5. +33 −0 lib/magic_encoding.rb
  6. +19 −0 magic_encoding.gemspec
@@ -0,0 +1,3 @@
+## 0.0.1 (August 3, 2010)
+
+Initial Version
20 LICENCE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Manuel Ryan
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
+= Magic encoding
+
+Magic Encoding is a little tool that allows you to quickly
+add or modify the magic comments that indicate source file
+encoding for an entire directory structure, please note that
+this tool only sets the comments, and does not perform any
+file encoding conversion whatsoever.
+
+If you are looking for a way to convert files to a specific encoding, check out iconv (GNU)
+
+I originally wrote this to get rid of "invalid multibyte char (US-ASCII)"
+errors when writing strings containing non ascii characters like éöàüèä
+in ruby on rails controllers
+
+== Usage
+
+you can call the tool with default parameters like so
+
+ magic_encoding
+
+this will prepend every ".rb" file in the working directory (recursively) with the following line :
+
+ # -*- encoding : utf-8 -*-
+
+Notes :
+- if the encoding is already set, the comment remains unchanged
+- the rest of the file remains unchanged
+
+you can pass options to the tool to specify the desired encoding and the path where you want the tool to run, for example :
+
+ magic_encoding Shift-JIS /path/to/ruby/project
+
+For more information on ruby >= 1.9 encoding features, check out http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings
+
+
+
@@ -0,0 +1,12 @@
+#!/usr/bin/env ruby
+
+require 'magic_encoding'
+
+case ARGV.count
+when 0
+ AddMagicComment.process
+when 1
+ AddMagicComment.process(ARGV[0])
+when 2
+ AddMagicComment.process(ARGV[0], ARGV[1])
+end
@@ -0,0 +1,33 @@
+# -*- encoding : utf-8 -*-~
+
+# A simple library to prepend the encoding magic comment introduced with ruby 1.9 to
+# multiple ".rb" files
+
+class AddMagicComment
+
+ # TODO : check that the encoding specified is a valid encoding
+ def self.process(encoding = "utf-8", directory = Dir.pwd)
+
+ # TODO : add support for other formats of magic comment (coding instead of encoding, emacs style magic comment, etc..)
+ prefix = "# -*- encoding : #{encoding} -*-~\n"
+
+ # TODO : add options for recursivity (and application of the script to a single file)
+ rbfiles = File.join(directory ,"**", "*.rb")
+ Dir.glob(rbfiles).each do |filename|
+ file = File.new(filename, "r+")
+
+ file_data = file.read
+ prefix_match = file_data[0..prefix.length-1]
+
+ # go back to top
+ file.pos = 0
+
+ # TODO : detect when to replace current encoding
+ if prefix_match and prefix_match != prefix
+ file.puts(prefix+file_data)
+ end
+ file.close
+ end
+ p "Encoding magic comment set for #{Dir.glob(rbfiles).count} source files"
+ end
+end
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{magic_encoding}
+ s.version = "0.0.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Manuel Ryan"]
+ s.date = %q{2010-08-03}
+ s.default_executable = %q{magic_encoding}
+ s.email = ["ryan@shamu.ch"]
+ s.executables = ["magic_encoding"]
+ s.files = Dir.glob("{bin,lib}/**/*") + %w(README.rdoc CHANGELOG)
+ s.homepage = %q{http://github.com/m-ryan/magic_encoding}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{Easily add magic comments for encoding on multiple ruby source files}
+
+end

0 comments on commit f611853

Please sign in to comment.