magic_encoding_just_for_ruby 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f454a337642b4044d1ed4c72f29d3c719783306
4
+ data.tar.gz: 06e2ce258618bc6776daa05c560018ac2a08a862
5
+ SHA512:
6
+ metadata.gz: 337668cf7f20ff1430f707ace408a84ff1df2204739762e86476000b2a79bc2a4461da1f5f6012152d1a98634a8acd31062aea592784f71f5a45d81c636a7203
7
+ data.tar.gz: fe66f8d2b882541fd3b778965d32bb5023f7d06cec656fcc1d475eb86b2a1e7b70b396fb31b5ea7e74ba3d86220e7bdbb50b87c5abd7c4d62d9dd402ef00fd00
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ ## 0.0.1 (August 3, 2010)
2
+
3
+ Initial Version
4
+
5
+ ## 0.0.2 (August 3, 2010)
6
+
7
+ Existing magic comments are now erased before the file is prepended with the desired magic comment
8
+ Bugfixes
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.
data/README.rdoc ADDED
@@ -0,0 +1,40 @@
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
+ == Installation
16
+
17
+ gem install magic_encoding
18
+
19
+ == Usage
20
+
21
+ you can call the tool from the console with default parameters like so
22
+
23
+ magic_encoding
24
+
25
+ this will prepend every ".rb" file in the working directory (recursively) with the following line :
26
+
27
+ # encoding : utf-8
28
+
29
+ Notes :
30
+ - existing magic comments are replaced
31
+ - the rest of the file remains unchanged
32
+
33
+ you can pass options to the tool to specify the desired encoding and the path where you want the tool to run, for example :
34
+
35
+ magic_encoding Shift-JIS /path/to/ruby/project
36
+
37
+ For more information on ruby >= 1.9 encoding features, check out http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings
38
+
39
+
40
+
@@ -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,67 @@
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
+ # TODO : allow use of only one option, so the encoding would be guessed (maybe using `file --mime`?)
12
+ def self.process(options)
13
+
14
+ # defaults
15
+ encoding = options[0] || "utf-8"
16
+ directory = options[1] || Dir.pwd
17
+
18
+ #prefix = "-*- encoding : #{encoding} -*-\n"
19
+ prefix = "encoding: utf-8\n"
20
+
21
+ # TODO : add options for recursivity (and application of the script to a single file)
22
+
23
+ extensions = {
24
+ 'rb' => '# {text}',
25
+ 'rake' => '# {text}',
26
+ 'haml' => '-# {text}',
27
+ }
28
+
29
+ count = 0
30
+ extensions.each do |ext, comment_style|
31
+ rbfiles = File.join(directory ,'**', '*.'+ext)
32
+ Dir.glob(rbfiles).each do |filename|
33
+ file = File.new(filename, "r+")
34
+
35
+ lines = file.readlines
36
+
37
+ # remove current encoding comment(s)
38
+ while lines[0].match(/^-?# ?(-\*-)? ?(en)?coding/)
39
+ lines.shift
40
+ end
41
+
42
+ # set current encoding
43
+ lines.insert(0,comment_style.sub('{text}', prefix))
44
+ count += 1
45
+
46
+ file.pos = 0
47
+ file.puts(lines.join)
48
+ file.close
49
+ end
50
+ end
51
+
52
+ puts "Magic comments set for #{count} source files"
53
+ end
54
+
55
+ end
56
+
57
+ class String
58
+
59
+ def starts_with?(s)
60
+ self[0..s.length-1] == s
61
+ end
62
+
63
+ end
64
+
65
+
66
+
67
+
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_encoding_just_for_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - messiahxu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - ryan@shamu.ch
16
+ executables:
17
+ - magic_encoding
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/magic_encoding
22
+ - lib/magic_encoding.rb
23
+ - README.rdoc
24
+ - CHANGELOG
25
+ - LICENCE
26
+ homepage: http://github.com/messiahxu/magic_encoding
27
+ licenses: []
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.6
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.1.11
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Easily add magic comments for encoding on multiple ruby source files
49
+ test_files: []