magic_encoding 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ ## 0.0.1 (August 3, 2010)
2
+
3
+ Initial Version
data/LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Manuel Ryan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ = Magic encoding
2
+
3
+ Magic Encoding is a little tool that allows you to quickly
4
+ add or modify the magic comments that indicate source file
5
+ encoding for an entire directory structure, please note that
6
+ this tool only sets the comments, and does not perform any
7
+ file encoding conversion whatsoever.
8
+
9
+ If you are looking for a way to convert files to a specific encoding, check out iconv (GNU)
10
+
11
+ I originally wrote this to get rid of "invalid multibyte char (US-ASCII)"
12
+ errors when writing strings containing non ascii characters like éöàüèä
13
+ in ruby on rails controllers
14
+
15
+ == Usage
16
+
17
+ you can call the tool with default parameters like so
18
+
19
+ magic_encoding
20
+
21
+ this will prepend every ".rb" file in the working directory (recursively) with the following line :
22
+
23
+ # -*- encoding : utf-8 -*-
24
+
25
+ Notes :
26
+ - existing magic comments are replaced
27
+ - the rest of the file remains unchanged
28
+
29
+ you can pass options to the tool to specify the desired encoding and the path where you want the tool to run, for example :
30
+
31
+ magic_encoding Shift-JIS /path/to/ruby/project
32
+
33
+ For more information on ruby >= 1.9 encoding features, check out http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings
34
+
35
+
36
+
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # A simple tool to prepend magic comments for encoding to multiple ".rb" files
4
+
5
+ require 'magic_encoding'
6
+
7
+ AddMagicComment.process(ARGV)
@@ -0,0 +1,56 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ # A simple library to prepend magic comments for encoding to multiple ".rb" files
4
+
5
+ module AddMagicComment
6
+
7
+ # Options :
8
+ # 1 : Encoding
9
+ # 2 : Path
10
+ # TODO : check that the encoding specified is a valid encoding
11
+ def self.process(options)
12
+
13
+ # defaults
14
+ encoding = options[0] || "utf-8"
15
+ directory = options[1] || Dir.pwd
16
+
17
+ prefix = "# -*- encoding : #{encoding} -*-\n"
18
+
19
+ # TODO : add options for recursivity (and application of the script to a single file)
20
+ rbfiles = File.join(directory ,"**", "*.rb")
21
+ Dir.glob(rbfiles).each do |filename|
22
+ file = File.new(filename, "r+")
23
+
24
+ lines = file.readlines
25
+
26
+ # remove current encoding comment(s)
27
+ while lines[0] && (
28
+ lines[0].starts_with?("# encoding") ||
29
+ lines[0].starts_with?("# coding") ||
30
+ lines[0].starts_with?("# -*- encoding"))
31
+ lines.shift
32
+ end
33
+
34
+ # set current encoding
35
+ lines.insert(0,prefix)
36
+
37
+ file.pos = 0
38
+ file.puts(lines.join)
39
+ file.close
40
+ end
41
+ p "Magic comments set for #{Dir.glob(rbfiles).count} source files"
42
+ end
43
+
44
+ end
45
+
46
+ class String
47
+
48
+ def starts_with?(s)
49
+ self[0..s.length-1] == s
50
+ end
51
+
52
+ end
53
+
54
+
55
+
56
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_encoding
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Manuel Ryan
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-03 00:00:00 +02:00
18
+ default_executable: magic_encoding
19
+ dependencies: []
20
+
21
+ description:
22
+ email:
23
+ - ryan@shamu.ch
24
+ executables:
25
+ - magic_encoding
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - bin/magic_encoding
32
+ - lib/magic_encoding.rb
33
+ - README.rdoc
34
+ - CHANGELOG
35
+ - LICENCE
36
+ has_rdoc: true
37
+ homepage: http://github.com/m-ryan/magic_encoding
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 3
61
+ - 6
62
+ version: 1.3.6
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Easily add magic comments for encoding on multiple ruby source files
70
+ test_files: []
71
+