herbgobbler 0.1.2 → 0.1.3
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.
- data/bin/gobble +23 -27
- data/grammer/erb_grammer.treetop +11 -1
- data/lib/commandline/commandline.rb +3 -0
- data/lib/commandline/gobble_all.rb +40 -0
- data/lib/commandline/gobble_share.rb +9 -0
- data/lib/commandline/gobble_single_file.rb +40 -0
- data/lib/core/rails_translation_store.rb +40 -1
- data/lib/herbgobbler.rb +1 -0
- metadata +8 -4
data/bin/gobble
CHANGED
@@ -2,42 +2,38 @@
|
|
2
2
|
require 'lib/herbgobbler'
|
3
3
|
|
4
4
|
def print_usage
|
5
|
-
puts "
|
5
|
+
puts " ***** HerbGobbler Usage ***** "
|
6
|
+
puts
|
7
|
+
puts "Options "
|
8
|
+
puts "-a : Process an entire Rails Code Base"
|
9
|
+
puts "-f <full path to file> : Process a single file"
|
10
|
+
puts
|
11
|
+
puts "Usage: gobble <options> <rails root>"
|
6
12
|
end
|
7
13
|
|
8
|
-
|
9
|
-
# when the context is set that rails will be able to find
|
10
|
-
# it through the default . key syntax
|
11
|
-
def convert_path_to_key_path( path )
|
12
|
-
path.split('.').first.gsub( '/app/views/', '')
|
13
|
-
end
|
14
|
-
|
15
|
-
if( ARGV.length < 1 )
|
14
|
+
if( ARGV.length < 2 )
|
16
15
|
print_usage
|
17
16
|
else
|
17
|
+
option = ARGV.shift
|
18
|
+
rails_root = ARGV.pop
|
19
|
+
case option
|
20
|
+
when "-a"
|
21
|
+
command_line_object = GobbleAll.new( rails_root, ARGV )
|
22
|
+
when "-f"
|
23
|
+
command_line_object = GobbleSingleFile.new( rails_root, ARGV )
|
24
|
+
else
|
25
|
+
print_usage
|
26
|
+
end
|
18
27
|
|
19
|
-
rails_root = ARGV[0]
|
20
|
-
locale_file_name = "en.yml"
|
21
|
-
|
22
|
-
rails_view_directory = "#{rails_root}/app/views"
|
23
|
-
full_yml_file_path = "#{rails_root}/config/locales/#{locale_file_name}"
|
24
28
|
|
25
|
-
|
26
|
-
|
29
|
+
if( !command_line_object.nil? && command_line_object.valid? )
|
30
|
+
command_line_object.execute
|
31
|
+
else
|
32
|
+
print_usage
|
33
|
+
end
|
27
34
|
|
28
|
-
Dir["#{rails_view_directory}/**/*html.erb" ].each do |full_erb_file_path|
|
29
|
-
|
30
|
-
erb_file = full_erb_file_path.gsub( rails_root, '' )
|
31
|
-
rails_translation_store.start_new_context( convert_path_to_key_path( erb_file.to_s ) )
|
32
|
-
erb_file = ErbFile.load( full_erb_file_path )
|
33
|
-
erb_file.extract_text( text_extractor )
|
34
35
|
|
35
|
-
File.open(full_erb_file_path, 'w') {|f| f.write(erb_file.to_s) }
|
36
|
-
puts "Wrote #{full_erb_file_path}"
|
37
|
-
end
|
38
36
|
|
39
|
-
File.open(full_yml_file_path, 'w') {|f| f.write(rails_translation_store.serialize) }
|
40
|
-
puts "Wrote #{full_yml_file_path}"
|
41
37
|
|
42
38
|
end
|
43
39
|
|
data/grammer/erb_grammer.treetop
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
grammar ERBGrammer
|
2
2
|
rule erb
|
3
|
-
(
|
3
|
+
( processing_instruction /
|
4
|
+
erb_string /
|
4
5
|
erb_block /
|
5
6
|
ignorable_script_tag_content /
|
6
7
|
html_end_tag /
|
@@ -90,6 +91,15 @@ grammar ERBGrammer
|
|
90
91
|
|
91
92
|
end
|
92
93
|
|
94
|
+
rule processing_instruction
|
95
|
+
'<!' ( !'>' .)+ '>' <NonTextNode>
|
96
|
+
{
|
97
|
+
def node_name
|
98
|
+
"processing_instruction"
|
99
|
+
end
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
93
103
|
rule erb_block
|
94
104
|
start_block:'<%' block_contents:((!erb_block_end .)*) end_block:not_combindable_erb_block_end <NonTextNode>
|
95
105
|
{
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class GobbleAll
|
2
|
+
include GobbleShare
|
3
|
+
|
4
|
+
def initialize( rails_root, options )
|
5
|
+
@rails_root = rails_root
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute
|
10
|
+
puts "Processing all of: #{@rails_root}"
|
11
|
+
puts ""
|
12
|
+
|
13
|
+
locale_file_name = "en.yml"
|
14
|
+
|
15
|
+
rails_view_directory = "#{@rails_root}/app/views"
|
16
|
+
full_yml_file_path = "#{@rails_root}/config/locales/#{locale_file_name}"
|
17
|
+
|
18
|
+
rails_translation_store = RailsTranslationStore.new
|
19
|
+
text_extractor = RailsTextExtractor.new( rails_translation_store )
|
20
|
+
|
21
|
+
Dir["#{rails_view_directory}/**/*html.erb" ].each do |full_erb_file_path|
|
22
|
+
|
23
|
+
erb_file = full_erb_file_path.gsub( @rails_root, '' )
|
24
|
+
rails_translation_store.start_new_context( convert_path_to_key_path( erb_file.to_s ) )
|
25
|
+
erb_file = ErbFile.load( full_erb_file_path )
|
26
|
+
erb_file.extract_text( text_extractor )
|
27
|
+
|
28
|
+
File.open(full_erb_file_path, 'w') {|f| f.write(erb_file.to_s) }
|
29
|
+
puts "Wrote #{full_erb_file_path}"
|
30
|
+
end
|
31
|
+
|
32
|
+
File.open(full_yml_file_path, 'w') {|f| f.write(rails_translation_store.serialize) }
|
33
|
+
puts "Wrote #{full_yml_file_path}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid?
|
37
|
+
@options.empty?
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module GobbleShare
|
2
|
+
# remove the file extension and the app/views/ so that
|
3
|
+
# when the context is set that rails will be able to find
|
4
|
+
# it through the default . key syntax
|
5
|
+
def convert_path_to_key_path( path )
|
6
|
+
path.split('.').first.gsub( '/app/views/', '')
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class GobbleSingleFile
|
2
|
+
include GobbleShare
|
3
|
+
|
4
|
+
def initialize( rails_root, options )
|
5
|
+
@rails_root = rails_root
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute
|
10
|
+
file_name = @options.first
|
11
|
+
locale_file_name = "en.yml"
|
12
|
+
rails_view_directory = "#{@rails_root}/app/views"
|
13
|
+
full_yml_file_path = "#{@rails_root}/config/locales/#{locale_file_name}"
|
14
|
+
erb_file = "/app/#{file_name.gsub( /.*\/app\//, '' )}"
|
15
|
+
puts "Gobbling file: #{file_name}"
|
16
|
+
puts ""
|
17
|
+
|
18
|
+
# load erb file into rails translation store
|
19
|
+
rails_translation_store = RailsTranslationStore.load_from_file( full_yml_file_path )
|
20
|
+
text_extractor = RailsTextExtractor.new( rails_translation_store )
|
21
|
+
|
22
|
+
# open file to process
|
23
|
+
rails_translation_store.start_new_context( convert_path_to_key_path( erb_file.to_s ) )
|
24
|
+
|
25
|
+
erb_file = ErbFile.load( file_name )
|
26
|
+
erb_file.extract_text( text_extractor )
|
27
|
+
|
28
|
+
# wiret file
|
29
|
+
File.open(file_name, 'w') {|f| f.write(erb_file.to_s) }
|
30
|
+
|
31
|
+
# re-write yml file
|
32
|
+
File.open(full_yml_file_path, 'w') {|f| f.write(rails_translation_store.serialize) }
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def valid?
|
37
|
+
@options.size == 1
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -1,10 +1,49 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
class RailsTranslationStore < BaseTranslationStore
|
2
4
|
|
3
5
|
def initialize( language = "en" )
|
4
6
|
super()
|
5
|
-
@language = language
|
7
|
+
@language = language
|
6
8
|
end
|
7
9
|
|
10
|
+
|
11
|
+
def self.load_from_file( full_path_to_file, language = "en" )
|
12
|
+
if( File.exists?( full_path_to_file ) )
|
13
|
+
RailsTranslationStore.load_from_string( File.read( full_path_to_file ), language )
|
14
|
+
else
|
15
|
+
RailsTranslationStore.new( language )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# This is used to load an existing rails transaltion store (in yaml
|
20
|
+
# format) to a new rails translation store
|
21
|
+
def self.load_from_string( string, language = "en" )
|
22
|
+
to_return = RailsTranslationStore.new( language )
|
23
|
+
yaml = YAML.load( string )
|
24
|
+
yaml['en'].each_pair do |language, translations|
|
25
|
+
self.process_yaml_pair( to_return, language, translations, language )
|
26
|
+
end
|
27
|
+
to_return
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.process_yaml_pair( translation_store, key, value, directory_like_key )
|
31
|
+
if( value.is_a?( Hash ) )
|
32
|
+
value.each_pair do |key, value|
|
33
|
+
if( value.is_a?( Hash ) )
|
34
|
+
directory_like_key = "#{directory_like_key}/" unless directory_like_key.empty?
|
35
|
+
directory_like_key = "#{directory_like_key}#{key}"
|
36
|
+
end
|
37
|
+
self.process_yaml_pair( translation_store, key, value, directory_like_key )
|
38
|
+
end
|
39
|
+
translation_store
|
40
|
+
else
|
41
|
+
translation_store.start_new_context( directory_like_key )
|
42
|
+
translation_store.add_translation( key, value )
|
43
|
+
translation_store
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
8
47
|
# This goes out of it's way to keep things in exactly the correct
|
9
48
|
# order. This makes it easier for translators when dealing with
|
10
49
|
# files. This is the reason to not just throw things into a hash
|
data/lib/herbgobbler.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: herbgobbler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Douglas Sellers
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-03-06 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -76,6 +76,10 @@ extra_rdoc_files: []
|
|
76
76
|
|
77
77
|
files:
|
78
78
|
- bin/gobble
|
79
|
+
- lib/commandline/commandline.rb
|
80
|
+
- lib/commandline/gobble_all.rb
|
81
|
+
- lib/commandline/gobble_share.rb
|
82
|
+
- lib/commandline/gobble_single_file.rb
|
79
83
|
- lib/core/base_node.rb
|
80
84
|
- lib/core/base_text_extractor.rb
|
81
85
|
- lib/core/base_translation_store.rb
|