i18n_yaml_sorter_2 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a8a9ffaecafe15a0d6075bb75ea6ae92002b82b
4
+ data.tar.gz: 6ebfdd81a26de951627cfa6a1210a710913c01ef
5
+ SHA512:
6
+ metadata.gz: f48dde0425533f617476cd7596514b426a60a4b7435836c5dd3f845655bf7ee16292fa81147fabe83ed3eb8a72597713a4a4cb8d3c9e7f71d0fe8a5a9e94678e
7
+ data.tar.gz: de4569f0da2460388fe5ac1c6074bba54181eef4f7f9d339a6d0d4b5724347a751b77bca53cb7e64e378698b5e201c1da0ef4318ee2103e6b5b135ccde2b6b72
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i18n_yaml_sorter.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n_yaml_sorter_2 (0.3.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.1)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler
30
+ i18n_yaml_sorter_2!
31
+ rake
32
+ rspec
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Bernardo de Pádua
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,236 @@
1
+ = i18n_yaml_sorter
2
+
3
+
4
+ A simple YAML Hash deep sorter that will not mess with the way your strings and text values were written. Made exclusively to sort the YAML Hash keys commonly used in the i18n gem and Rails' apps. It will not sort arrays, YAML objects, etc.
5
+
6
+ It doesn't parse the YAML and dumps it again, it just sorts the lines, it's pure and simple magic! That way, it guarantees you can keep using your nice YAML constructs to create large text values, as we usually do in i18n files.
7
+
8
+ It includes a simple TextMate bundle and a rake task for Rails, so you can keep all those .yml files sorted in no time.
9
+
10
+ == Motivation
11
+
12
+ As your rails i18n app grows, it becomes very boring to keep all locale files in sync. You have to add all keys in the same order, in different languages, or you will get yourself into a mess and will lose precious time comparing the files in different locales and looking for the appropriate translation keys whenever you want to change something or see if a key is missing.
13
+
14
+ If you try deserializing/serializing the yml files using the YAML parser, to get them in the same order, you will figure that your strings in the YAMLs will be "standarized" to whatever the YAML generator prefers (eg.: strings in double quotes, character escapes, etc). It will also mess up your indentation (and use its defaults) and remove your comments.
15
+
16
+ i18n_yaml_sorter to the rescue! Add it to TextMate and you will be able to sort your yaml file in nanoseconds. Your file will look pristine, like it was human edited.
17
+
18
+ Tested in ruby-1.8.7-p334 and ruby-1.9.2-p290, should work everywhere since it is so simple. Rake task works in Rails 3.1, not sure about older versions though (patch updates are welcome, if you find it doesn't).
19
+
20
+ == Simple Example
21
+ This:
22
+
23
+ pt-BR:
24
+ # Note how this is a nice way of inputing
25
+ # paragraphs of text in YAML.
26
+ apples: >
27
+ Maçãs são boas,
28
+ só não coma
29
+ seus iPods!
30
+ grapes: Não comemos elas.
31
+ bananas: |
32
+ Bananas são "legais":
33
+ - Elas são <b> doces </b>.
34
+ isto: não é chave
35
+
36
+ Por isto todos gostam de bananas!
37
+ en-US:
38
+ # Note that our comments are important:
39
+ # Don't let your yaml sorter delete them!
40
+ grapes: We dont' eat them.
41
+ bananas: |
42
+ Bananas are "nice":
43
+ - They are <b> sweet </b>.
44
+ this: not a key
45
+
46
+ That is why everyone like bananas!
47
+ apples: >
48
+ Apples are fine,
49
+ just don't eat your
50
+ iPods!
51
+
52
+
53
+ Becomes:
54
+
55
+ en-US:
56
+ # Note that our comments are important:
57
+ # Don't let your yaml sorter delete them!
58
+ apples: >
59
+ Apples are fine,
60
+ just don't eat your
61
+ iPods!
62
+ bananas: |
63
+ Bananas are "nice":
64
+ - They are <b> sweet </b>.
65
+ this: not a key
66
+
67
+ That is why everyone like bananas!
68
+ grapes: We dont' eat them.
69
+ pt-BR:
70
+ # Note how this is a nice way of inputing
71
+ # paragraphs of text in YAML.
72
+ apples: >
73
+ Maçãs são boas,
74
+ só não coma
75
+ seus iPods!
76
+ bananas: |
77
+ Bananas são "legais":
78
+ - Elas são <b> doces </b>.
79
+ isto: não é chave
80
+
81
+ Por isto todos gostam de bananas!
82
+ grapes: Não comemos elas.
83
+
84
+
85
+ What if you use some method that employ's Ruby's YAML library to help
86
+ do the task for you? You would get an output like that (note it removes your comments):
87
+
88
+ en-US:
89
+ apples: Apples are fine, just don't eat your iPods!
90
+ bananas: |
91
+ Bananas are "nice":
92
+ - They are <b> sweet </b>.
93
+ this: not a key
94
+
95
+ That is why everyone like bananas!
96
+
97
+ grapes: We dont' eat them.
98
+ pt-BR:
99
+ apples: "Ma\xC3\xA7\xC3\xA3s s\xC3\xA3o boas, s\xC3\xB3 n\xC3\xA3o coma seus iPods!\n"
100
+ bananas: "Bananas s\xC3\xA3o \"legais\":\n - Elas s\xC3\xA3o <b> doces </b>.\n isto: n\xC3\xA3o \xC3\xA9 chave\n\n\
101
+ Por isto todos gostam de bananas!\n"
102
+ grapes: "N\xC3\xA3o comemos elas."
103
+
104
+
105
+ == More complex example
106
+
107
+ This:
108
+
109
+ b_two:
110
+ a_1: Simple most common text
111
+ b_two: |
112
+ This is the best way of
113
+ inputing large chunks of text
114
+ in the YAML files.
115
+
116
+ Note that this format keeps blank
117
+ lines in the same indentation.
118
+ d_four: "We can also
119
+ use the boring \"
120
+ across multiple lines
121
+ but have to escape then."
122
+ e_five: Or you can do it
123
+ like that as well, it also works.
124
+ c_three:
125
+ a: "Marcelo"
126
+ d_4: Rafael
127
+ # Your comments will be untouched
128
+ # but will be bound to the element
129
+ # on top of them (d_4 here).
130
+ "b": Bernardo
131
+ c_3:
132
+ unify: Luiz
133
+ klass: Lucas
134
+ a_one: >
135
+ This is another way
136
+ of inputing text. It
137
+ will squish whitespace
138
+ when deserialized
139
+ (like HTML does).
140
+
141
+
142
+ Becomes:
143
+
144
+ a_one: >
145
+ This is another way
146
+ of inputing text. It
147
+ will squish whitespace
148
+ when deserialized
149
+ (like HTML does).
150
+ b_two:
151
+ a_1: Simple most common text
152
+ b_two: |
153
+ This is the best way of
154
+ inputing large chunks of text
155
+ in the YAML files.
156
+
157
+ Note that this format keeps blank
158
+ lines in the same indentation.
159
+ d_four: "We can also
160
+ use the boring \"
161
+ across multiple lines
162
+ but have to escape then."
163
+ c_three:
164
+ a: "Marcelo"
165
+ "b": Bernardo
166
+ c_3:
167
+ klass: Lucas
168
+ unify: Luiz
169
+ d_4: Rafael
170
+ # Your comments will be untouched
171
+ # but will be bound to the element
172
+ # on top of them (d_4 here).
173
+ e_five: Or you can do it
174
+ like that as well, it also works.
175
+
176
+ == Installing
177
+
178
+ Easy, just install the gem:
179
+
180
+ $ gem install i18n_yaml_sorter
181
+
182
+ Then the +sort_yaml+ command line tool will be available. If you prefer using Ruby (in your rakes, etc), use the simple +I18nYamlSorter::Sort+ class.
183
+
184
+ == Textmate Bundle
185
+
186
+ Run this command in the Terminal to install it:
187
+
188
+ $ sort_yaml -i
189
+
190
+ A TextMate bundle, named "Yaml Sort" will be installed in your user home path. Press "Shift+Command+S" or use the Bundles menu to invoke it. The opened yaml file (or just the part of it that is selected) will be sorted. To edit the selected part of the file, make sure it is valid YAML by itself, or your yaml file might be corrupted (you can always Undo if you mess up).
191
+
192
+ == Rails Rake Taks
193
+
194
+ Declare it as a dependency in your app Gemfile, under the development group:
195
+
196
+ gem 'i18n_yaml_sorter', :group => :development
197
+
198
+ Run bundle install under your Rails' app:
199
+
200
+ $ bundle install
201
+
202
+ Now run the rake task under your Rails' app to sort all the i18n files in your +config/locales+ dir:
203
+
204
+ $rake i18n:sort
205
+
206
+ == Command line Input / Output
207
+
208
+ +sort_yaml+ will operate on STDIN and STDOUT, so sorting an existing yaml file should be as easy as:
209
+
210
+ $ sort_yaml < in.yml > out.yml
211
+
212
+ TODO: Add command line arguments parsing and options, so you can, for instance, sort a whole dir of yaml files.
213
+
214
+ == Changelog
215
+
216
+ * 0.2 - Improves gem, tests, adds TextMate Bundle, adds Rails' Rake task.
217
+ * 0.1 - Initial release
218
+
219
+ == Future improvements (Forks Welcome!)
220
+
221
+ * Make +sort_yaml+ smart, take directories, etc
222
+ * Refactoring: internal code is still a bit ugly, but works
223
+
224
+ == Note on Patches/Pull Requests
225
+
226
+ * Fork the project.
227
+ * Make your feature addition or bug fix.
228
+ * Add tests for it. This is important so I don't break it in a
229
+ future version unintentionally.
230
+ * Commit, do not mess with rakefile, version, or history.
231
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
232
+ * Send me a pull request. Bonus points for topic branches.
233
+
234
+ == Copyright
235
+
236
+ Copyright (c) 2010-2011 Bernardo de Pádua. MIT License (See LICENCE).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'i18n_yaml_sorter'
6
+
7
+ options = {}
8
+ optparse = OptionParser.new do|opts|
9
+ opts.banner = "Usage: sort_yaml < INPUT > OUTPUT | sort_yaml --install_bundle"
10
+
11
+ # Define the options, and what they do
12
+ options[:install_bundle] = false
13
+ opts.on( '-i', '--install_bundle', 'Installs TextMate for MAC OS X bundle (named "YAML Sort") in the current user home dir.' ) do
14
+ options[:install_bundle] = true
15
+ end
16
+
17
+ # This displays the help screen, all programs are
18
+ # assumed to have this option.
19
+ opts.on( '-h', '--help', 'Display this screen' ) do
20
+ puts opts
21
+ exit
22
+ end
23
+ end
24
+
25
+ optparse.parse!
26
+
27
+ if options[:install_bundle]
28
+ puts "Installing TextMate Bundle in your home dir (~/Library/Application\\ Support/TextMate/Bundles/)..."
29
+ puts
30
+ system "mkdir -p ~/Library/Application\ Support/TextMate/Bundles"
31
+ system "cp -Rfv #{File.join(File.dirname(__FILE__), '..', 'textmate', 'YAML\ Sort.tmbundle')} ~/Library/Application\\ Support/TextMate/Bundles/ "
32
+ puts
33
+ puts "Reloading TextMate bundles..."
34
+ system "osascript -e 'tell app \"TextMate\" to reload bundles'"
35
+ else
36
+ sorter = I18nYamlSorter::Sorter.new(STDIN)
37
+ puts sorter.sort
38
+ end
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "i18n_yaml_sorter_2"
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bernardo de P\u{e1}dua", 'compiledwrong']
12
+ s.date = "2081-04-06"
13
+ s.description = " Allows you to deep sort YAML files that are mainly composed of \n nested hashes and string values. Great to sort your rails I18n YAML files. You can easily\n add it to a textmate bundle, rake task, or just use the included regular comand line tool. \n "
14
+ s.email = ["berpasan@gmail.com", 'compiledwrong+i18n_yaml_sorter@gmail.com']
15
+ s.executables = ["sort_yaml"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/sort_yaml",
29
+ "i18n_yaml_sorter.gemspec",
30
+ "lib/i18n_yaml_sorter.rb",
31
+ "lib/i18n_yaml_sorter/railtie.rb",
32
+ "lib/i18n_yaml_sorter/sorter.rb",
33
+ "lib/tasks/i18n_yaml_sorter.rake",
34
+ "test/helper.rb",
35
+ "test/in.yml",
36
+ "test/in_rails.yml",
37
+ "test/in_simple.yml",
38
+ "test/out.yml",
39
+ "test/out_rails.yml",
40
+ "test/out_simple.yml",
41
+ "test/test_i18n_yaml_sorter.rb",
42
+ "textmate/YAML Sort.tmbundle/Commands/Sort YAML.tmCommand",
43
+ "textmate/YAML Sort.tmbundle/info.plist"
44
+ ]
45
+ s.homepage = "http://github.com/redealumni/i18n_yaml_sorter"
46
+ s.licenses = ["MIT"]
47
+ s.require_paths = ["lib"]
48
+ s.summary = "A I18n YAML deep sorter that will keep your locales organized and not screw up your text formating"
49
+
50
+ s.add_development_dependency 'bundler'
51
+ s.add_development_dependency 'rake'
52
+ s.add_development_dependency 'rspec'
53
+ end
54
+
@@ -0,0 +1,2 @@
1
+ require 'i18n_yaml_sorter/sorter'
2
+ require 'i18n_yaml_sorter/railtie'
@@ -0,0 +1,10 @@
1
+ module I18nYamlSorter
2
+ if defined? Rails::Railtie
3
+ require 'rails'
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/i18n_yaml_sorter.rake"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,128 @@
1
+ module I18nYamlSorter
2
+ class Sorter
3
+ def initialize(io_input)
4
+ @io_input = io_input
5
+ end
6
+
7
+ def sort
8
+ @array = break_blocks_into_array
9
+ @current_array_index = 0
10
+ sorted_yaml_from_blocks_array
11
+ end
12
+
13
+ private
14
+
15
+ def break_blocks_into_array
16
+ array = []
17
+
18
+ loop do
19
+
20
+ maybe_next_line = @read_line_again || @io_input.gets || break
21
+ @read_line_again = nil
22
+ maybe_next_line.chomp!
23
+
24
+ #Is it blank? Discard!
25
+ next if maybe_next_line.match(/^\s*$/)
26
+
27
+ #Does it look like a key: value line?
28
+ key_value_parse = maybe_next_line.match(/^(\s*)(["']?[\w\-]+["']?)(: )(\s*)(\S.*\S)(\s*)$/)
29
+ if key_value_parse
30
+ array << maybe_next_line.concat("\n") #yes, it is the beginning of a key:value block
31
+
32
+ #Special cases when it should add extra lines to the array element (multi line quoted strings)
33
+
34
+ #Is the value surrounded by quotes?
35
+ starts_with_quote = key_value_parse[5].match(/^["']/)[0] rescue nil
36
+ ends_with_quote = key_value_parse[5].match(/[^\\](["'])$/)[1] rescue nil
37
+ if starts_with_quote and !(starts_with_quote == ends_with_quote)
38
+
39
+ loop do #Append next lines until we find the closing quote
40
+ content_line = @io_input.gets || break
41
+ content_line.chomp!
42
+ array.last << content_line.concat("\n")
43
+ break if content_line.match(/[^\\][#{starts_with_quote}]\s*$/)
44
+ end
45
+
46
+ end # if starts_with_quote
47
+
48
+ next
49
+ end # if key_value_parse
50
+
51
+ # Is it a | or > string alue?
52
+ is_special_string = maybe_next_line.match(/^(\s*)(["']?[\w\-]+["']?)(: )(\s*)([|>])(\s*)$/)
53
+ if is_special_string
54
+ array << maybe_next_line.concat("\n") #yes, it is the beginning of a key block
55
+ indentation = is_special_string[1]
56
+ #Append the next lines until we find one that is not indented
57
+ loop do
58
+ content_line = @io_input.gets || break
59
+ processed_line = content_line.chomp
60
+ this_indentation = processed_line.match(/^\s*/)[0] rescue ""
61
+ if indentation.size < this_indentation.size
62
+ array.last << processed_line.concat("\n")
63
+ else
64
+ @read_line_again = content_line
65
+ break
66
+ end
67
+ end
68
+
69
+ next
70
+ end #if is_special_string
71
+
72
+ # Is it the begining of a multi level hash?
73
+ is_start_of_hash = maybe_next_line.match(/^(\s*)(["']?[\w\-]+["']?)(:)(\s*)$/)
74
+ if is_start_of_hash
75
+ array << maybe_next_line.concat("\n")
76
+ next
77
+ end
78
+
79
+ #If we got here and nothing was done, this line
80
+ # should probably be merged with the previous one.
81
+ if array.last
82
+ array.last << maybe_next_line.concat("\n")
83
+ else
84
+ array << maybe_next_line.concat("\n")
85
+ end
86
+ end #loop
87
+
88
+ #debug:
89
+ #puts array.join("$$$$$$$$$$$$$$$$$$$$$$\n")
90
+
91
+ array
92
+ end
93
+
94
+ def sorted_yaml_from_blocks_array(current_block = nil)
95
+
96
+ unless current_block
97
+ current_block = @array[@current_array_index]
98
+ @current_array_index += 1
99
+ end
100
+
101
+ out_array = []
102
+ current_match = current_block.match(/^(\s*)(["']?[\w\-]+["']?)(:)/)
103
+ current_level = current_match[1] rescue ''
104
+ current_key = current_match[2].downcase.tr(%q{"'}, "") rescue ''
105
+ out_array << [current_key, current_block]
106
+
107
+ loop do
108
+ next_block = @array[@current_array_index] || break
109
+ @current_array_index += 1
110
+
111
+ current_match = next_block.match(/^(\s*)(["']?[\w\-]+["']?)(:)/) || next
112
+ current_key = current_match[2].downcase.tr(%q{"'}, "")
113
+ next_level = current_match[1]
114
+
115
+ if current_level.size < next_level.size
116
+ out_array.last.last << sorted_yaml_from_blocks_array(next_block)
117
+ elsif current_level.size == next_level.size
118
+ out_array << [current_key, next_block]
119
+ elsif current_level.size > next_level.size
120
+ @current_array_index -= 1
121
+ break
122
+ end
123
+ end
124
+
125
+ return out_array.sort.map(&:last).join
126
+ end
127
+ end
128
+ end