burek 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/config.rb +1 -0
- data/lib/core/burek_finder.rb +56 -0
- data/lib/core/burek_replacer.rb +32 -0
- data/lib/core/core.rb +43 -0
- data/lib/core/locales_creator.rb +114 -0
- data/lib/file_helpers.rb +28 -0
- data/lib/{view_helpers.rb → rails/view_helpers.rb} +0 -0
- metadata +9 -5
- data/lib/core.rb +0 -122
- data/lib/parser.rb +0 -33
data/README.md
ADDED
data/lib/config.rb
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'file_helpers'
|
2
|
+
|
3
|
+
module Burek
|
4
|
+
module Finder
|
5
|
+
|
6
|
+
# Searches all files within search folders for burek calls.
|
7
|
+
#
|
8
|
+
# ==== Returns
|
9
|
+
# Hash in whichs values represent burek call captions and keys represent locales file paths in which
|
10
|
+
# those captions were found
|
11
|
+
#
|
12
|
+
def self.find_burek_calls_in_files
|
13
|
+
new_translations = {}
|
14
|
+
Burek::FileHelpers.open_each_file do |contents, file_name|
|
15
|
+
filtered_path = filter_path(file_name)
|
16
|
+
matches = find_burek_calls(contents)
|
17
|
+
matches.each do |caption|
|
18
|
+
key = filtered_path + "/" + caption.downcase.gsub(' ','_')
|
19
|
+
new_translations[key] = caption
|
20
|
+
end
|
21
|
+
end
|
22
|
+
return new_translations
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# Finds burek calls in a string
|
27
|
+
#
|
28
|
+
# ==== Attributes
|
29
|
+
# * +string+ - A string that (possibly) contains burek calls
|
30
|
+
#
|
31
|
+
# ==== Returns
|
32
|
+
# Array of burek call captions
|
33
|
+
#
|
34
|
+
def self.find_burek_calls(string)
|
35
|
+
string.scan(Burek::Core.burek_call_regex).flatten
|
36
|
+
end
|
37
|
+
|
38
|
+
# Removes file extension from a path as well as any folder that is specified as ignored folder in config
|
39
|
+
#
|
40
|
+
# ==== Attributes
|
41
|
+
# * +file_name+ - path in string format that needs to be filtered
|
42
|
+
#
|
43
|
+
# ==== Returns
|
44
|
+
# Filtered path as a string
|
45
|
+
#
|
46
|
+
def self.filter_path(file_name)
|
47
|
+
path = file_name.split('/')
|
48
|
+
path.delete_if do |item|
|
49
|
+
Burek.config(:ignore_folders_for_key).include? item
|
50
|
+
end
|
51
|
+
path.last.gsub!(/\.(.*?)$/,'').gsub!(/^_/,'') #strip extenison from file name
|
52
|
+
return path.join('/')
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'file_helpers'
|
2
|
+
require 'core/burek_finder'
|
3
|
+
|
4
|
+
module Burek
|
5
|
+
module Replacer
|
6
|
+
|
7
|
+
def self.replace_burek_calls_in_files(to_replace)
|
8
|
+
Burek::FileHelpers.open_each_file do |content,file_name|
|
9
|
+
processed_content = replace_burek_calls(content, to_replace)
|
10
|
+
unless processed_content.nil?
|
11
|
+
File.open(file_name, 'w') do |output_file|
|
12
|
+
output_file.print processed_content
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.replace_burek_calls(contents, to_replace)
|
19
|
+
matches = Burek::Finder.find_burek_calls(contents)
|
20
|
+
changes_made = false
|
21
|
+
matches.each do |value|
|
22
|
+
if to_replace.has_key?(value)
|
23
|
+
contents.gsub!(Burek::Core.burek_call_regex(value)," t('#{to_replace[value]}')")
|
24
|
+
changes_made = true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
return contents if changes_made
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/core/core.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'config'
|
2
|
+
require 'yaml'
|
3
|
+
require 'core/burek_replacer'
|
4
|
+
require 'core/burek_finder'
|
5
|
+
require 'core/locales_creator'
|
6
|
+
require 'file_helpers'
|
7
|
+
|
8
|
+
module Burek
|
9
|
+
|
10
|
+
module Core
|
11
|
+
|
12
|
+
# Main method in burek. It works in three steps:
|
13
|
+
#
|
14
|
+
# 1. find all burek calls within folders specified in config
|
15
|
+
# 2. add new translations to locales files
|
16
|
+
# 3. replace all burek calls with regular translation calls
|
17
|
+
#
|
18
|
+
def self.run_burek
|
19
|
+
Burek::FileHelpers.create_folder_if_missing Burek.config(:translations_path)
|
20
|
+
|
21
|
+
new_translations = Burek::Finder.find_burek_calls_in_files
|
22
|
+
to_replace = Burek::LocalesCreator.create_locales(new_translations)
|
23
|
+
Burek::Replacer.replace_burek_calls_in_files(to_replace)
|
24
|
+
end
|
25
|
+
|
26
|
+
# A regex for finiding burek calls
|
27
|
+
#
|
28
|
+
# It is used by Burek::Finder and Burek::Replacer.
|
29
|
+
# Example calls it can find
|
30
|
+
# * burek('Hello world')
|
31
|
+
# * burek("Hello world")
|
32
|
+
# * burek ( "Hello world" )
|
33
|
+
#
|
34
|
+
# ==== Attributes
|
35
|
+
# * +caption+ - Use it to find burek call with specific caption.
|
36
|
+
# By default it will accept any string and capture it in group named 'key'.
|
37
|
+
#
|
38
|
+
def self.burek_call_regex(caption="(?<key>[^\\)]*)")
|
39
|
+
Regexp.new("[^a-zA-Z0-9_]burek[ \\t]*\\([ \\t]*('|\")#{caption}\('|\")[^\\)]*\\)")
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module Burek
|
2
|
+
module LocalesCreator
|
3
|
+
|
4
|
+
# Creates new or updates exisitng locales files. Called by Burek::Finder
|
5
|
+
#
|
6
|
+
# ==== Attributes
|
7
|
+
# * +new_translations+ - A hash of new translations that were found in search folders.
|
8
|
+
# Values represent burek call captions and keys represent locales file paths in which those captions were found.
|
9
|
+
#
|
10
|
+
# ==== Returns
|
11
|
+
# A hash of burek calls that need to be replaced by translation calls.
|
12
|
+
# Keys contain captions of burek calls and values contain key which translation keys should contain.
|
13
|
+
#
|
14
|
+
def self.create_locales(new_translations)
|
15
|
+
to_replace = {}
|
16
|
+
# Create files for each locale
|
17
|
+
Burek.config(:locales).each do |locale|
|
18
|
+
new_translations.each do |key,value|
|
19
|
+
path_parts = key.split("/")
|
20
|
+
item_name = path_parts.pop
|
21
|
+
path_parts_no_filename = path_parts[0..-2]
|
22
|
+
|
23
|
+
translation_file = key_to_file_path(key, locale)
|
24
|
+
translations_hash = initialize_translations_hash(translation_file, locale)
|
25
|
+
|
26
|
+
# Save info for replacing burek calls with translation calls.
|
27
|
+
# Keys are burek call captions and values are keys to be used in translation calls.
|
28
|
+
to_replace[value] = path_parts_to_key(path_parts_no_filename, item_name)
|
29
|
+
|
30
|
+
# Nest in hashes
|
31
|
+
cur_hash = translations_hash[locale.dup.force_encoding("UTF-8")]
|
32
|
+
path_parts_no_filename.each do |item|
|
33
|
+
cur_hash[item] = {} unless cur_hash.has_key?(item)
|
34
|
+
cur_hash = cur_hash[item]
|
35
|
+
end
|
36
|
+
cur_hash[item_name] = ( locale == Burek.config(:locales).first ? value.dup.force_encoding("UTF-8") : Burek.config(:translation_placeholder) )
|
37
|
+
|
38
|
+
# Save to file
|
39
|
+
Burek::LocalesCreator.translations_hash_to_file(translations_hash, translation_file)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
return to_replace
|
44
|
+
end
|
45
|
+
|
46
|
+
# Stores a translation hash in to a file
|
47
|
+
#
|
48
|
+
def self.translations_hash_to_file(translations_hash, translation_file)
|
49
|
+
clean_yaml = yaml_to_i18n_file(translations_hash.to_yaml)
|
50
|
+
translation_file.gsub!("//","/")
|
51
|
+
File.write(translation_file, clean_yaml) #Store
|
52
|
+
end
|
53
|
+
|
54
|
+
# Removes a first line from a yaml file which is added to standard YAML files
|
55
|
+
# but rails does not like it. It continas ---- or something like that
|
56
|
+
#
|
57
|
+
def self.yaml_to_i18n_file(yaml)
|
58
|
+
yaml.lines.to_a[1..-1].join
|
59
|
+
end
|
60
|
+
|
61
|
+
# Intializes a translation hash by either loading existing translation file
|
62
|
+
# or creating a hash that contains an empty hash under key which is name of current locale.
|
63
|
+
def self.initialize_translations_hash(translation_file, locale)
|
64
|
+
if File.exists?(translation_file)
|
65
|
+
translations_hash = YAML::load_file(translation_file) #Load
|
66
|
+
else
|
67
|
+
translations_hash = {}
|
68
|
+
translations_hash[locale.dup.force_encoding("UTF-8")] = {}
|
69
|
+
end
|
70
|
+
return translations_hash
|
71
|
+
end
|
72
|
+
|
73
|
+
# Creates a translation call key from path where burek call was found and burek call caption
|
74
|
+
#
|
75
|
+
def self.path_parts_to_key(path_parts, item_name)
|
76
|
+
regular_translation_key = path_parts.join('.')
|
77
|
+
regular_translation_key += "." unless regular_translation_key.nil? || regular_translation_key.empty?
|
78
|
+
regular_translation_key += "#{item_name}"
|
79
|
+
end
|
80
|
+
|
81
|
+
# Generates a file path to locales file from a file path where burek call was found.
|
82
|
+
# It will nest locales file under same subfolders in which burek call was found.
|
83
|
+
# If burek call was nested deeper then defined as subfolder depth limit in config file
|
84
|
+
# it will nest it under first X folders (where x is subfolder depth limit)
|
85
|
+
#
|
86
|
+
# ==== Attributes
|
87
|
+
# * +key+ - file path where burek call was found
|
88
|
+
# * +locale+ - locale for which this file is created
|
89
|
+
#
|
90
|
+
# ==== Returns
|
91
|
+
# A string that contains a path where locale file should be created.
|
92
|
+
# NOTE: Even though subfolders are created, the actuall locale file is NOT created.
|
93
|
+
#
|
94
|
+
def self.key_to_file_path(key, locale)
|
95
|
+
path_parts = key.split("/")
|
96
|
+
path_parts.pop
|
97
|
+
|
98
|
+
translation_file = Burek.config(:translations_path)
|
99
|
+
path_parts.each_with_index do |item, index|
|
100
|
+
if index == Burek.config(:subfolder_depth) || item == path_parts.last
|
101
|
+
translation_file += "#{item}.#{locale}.yml"
|
102
|
+
break
|
103
|
+
else
|
104
|
+
translation_file += "/#{item}/"
|
105
|
+
unless File.directory?(translation_file)
|
106
|
+
Dir.mkdir(translation_file)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
return translation_file
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
data/lib/file_helpers.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Burek
|
2
|
+
module FileHelpers
|
3
|
+
|
4
|
+
def self.open_each_file
|
5
|
+
for_each_file do |file_name|
|
6
|
+
File.open(file_name, "rb") do |file|
|
7
|
+
contents = file.read
|
8
|
+
yield contents, file_name
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.for_each_file
|
14
|
+
Burek.config(:search_folders).each do |folder|
|
15
|
+
Dir.glob(folder) do |file_name|
|
16
|
+
unless File.directory?(file_name)
|
17
|
+
yield file_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.create_folder_if_missing(path)
|
24
|
+
Dir.mkdir(path) unless File.directory?(path)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: burek
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -17,12 +17,16 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- tasks/tasks.rb
|
20
21
|
- lib/burek.rb
|
21
22
|
- lib/config.rb
|
22
|
-
- lib/core.rb
|
23
|
-
- lib/
|
24
|
-
- lib/
|
25
|
-
-
|
23
|
+
- lib/core/burek_finder.rb
|
24
|
+
- lib/core/burek_replacer.rb
|
25
|
+
- lib/core/core.rb
|
26
|
+
- lib/core/locales_creator.rb
|
27
|
+
- lib/file_helpers.rb
|
28
|
+
- lib/rails/view_helpers.rb
|
29
|
+
- README.md
|
26
30
|
homepage: https://github.com/dabrorius/burek
|
27
31
|
licenses:
|
28
32
|
- MIT
|
data/lib/core.rb
DELETED
@@ -1,122 +0,0 @@
|
|
1
|
-
require 'config'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
module Burek
|
5
|
-
|
6
|
-
module Core
|
7
|
-
|
8
|
-
def self.key_to_file_path(key, locale)
|
9
|
-
path_parts = key.split("/")
|
10
|
-
path_parts.pop
|
11
|
-
|
12
|
-
translation_file = Burek.config(:translations_path)
|
13
|
-
path_parts.each_with_index do |item, index|
|
14
|
-
if index == Burek.config(:subfolder_depth) || item == path_parts.last
|
15
|
-
translation_file += "#{item}.#{locale}.yml"
|
16
|
-
break
|
17
|
-
else
|
18
|
-
translation_file += "/#{item}/"
|
19
|
-
unless File.directory?(translation_file)
|
20
|
-
Dir.mkdir(translation_file)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
return translation_file
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.filter_path(file_name)
|
28
|
-
path = file_name.split('/')
|
29
|
-
path.delete_if do |item|
|
30
|
-
Burek.config(:ignore_folders_for_key).include? item
|
31
|
-
end
|
32
|
-
path.last.gsub!(/\.(.*?)$/,'').gsub!(/^_/,'') #strip extenison from file name
|
33
|
-
return path.join('/')
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.run_burek
|
37
|
-
# Create translations folder if missing
|
38
|
-
unless File.directory?(Burek.config(:translations_path))
|
39
|
-
Dir.mkdir(Burek.config(:translations_path))
|
40
|
-
end
|
41
|
-
|
42
|
-
new_translations = {}
|
43
|
-
# Iterate all defined subfolders subfolders
|
44
|
-
for_each_file do |file_name|
|
45
|
-
File.open(file_name, "rb") do |file|
|
46
|
-
contents = file.read
|
47
|
-
|
48
|
-
filtered_path = Burek::Core.filter_path(file_name)
|
49
|
-
matches = Burek::Parser.find_burek_calls(contents)
|
50
|
-
matches.each do |value|
|
51
|
-
key = filtered_path + "/" + value.downcase.gsub(' ','_')
|
52
|
-
new_translations[key] = value
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
to_replace = {}
|
58
|
-
# Create files for each locale
|
59
|
-
Burek.config(:locales).each do |locale|
|
60
|
-
new_translations.each do |key,value|
|
61
|
-
path_parts = key.split("/")
|
62
|
-
item_name = path_parts.pop
|
63
|
-
path_parts_no_filename = path_parts[0..-2]
|
64
|
-
|
65
|
-
# Figure out file path
|
66
|
-
translation_file = Burek::Core.key_to_file_path(key, locale)
|
67
|
-
|
68
|
-
# Initialize translations hash
|
69
|
-
if File.exists?(translation_file)
|
70
|
-
translations_hash = YAML::load_file(translation_file) #Load
|
71
|
-
else
|
72
|
-
translations_hash = {}
|
73
|
-
translations_hash[locale.dup.force_encoding("UTF-8")] = {}
|
74
|
-
end
|
75
|
-
|
76
|
-
# Save info for replacing burek calls with translation calls
|
77
|
-
regular_translation_key = path_parts_no_filename.join('.') + ".#{item_name}"
|
78
|
-
to_replace[value] = regular_translation_key
|
79
|
-
|
80
|
-
# Nest in hashes
|
81
|
-
cur_hash = translations_hash[locale.dup.force_encoding("UTF-8")]
|
82
|
-
path_parts_no_filename.each do |item|
|
83
|
-
cur_hash[item] = {} unless cur_hash.has_key?(item)
|
84
|
-
cur_hash = cur_hash[item]
|
85
|
-
end
|
86
|
-
cur_hash[item_name] = ( locale == Burek.config(:locales).first ? value.dup.force_encoding("UTF-8") : Burek.config(:translation_placeholder) )
|
87
|
-
|
88
|
-
# Save to file
|
89
|
-
clean_yaml = Burek::Parser.yaml_to_i18n_file(translations_hash.to_yaml)
|
90
|
-
translation_file.gsub!("//","/")
|
91
|
-
File.write(translation_file, clean_yaml) #Store
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
# Replace all burek calls with regular translation calls
|
98
|
-
for_each_file do |file_name|
|
99
|
-
File.open(file_name, 'r') do |file|
|
100
|
-
content = file.read
|
101
|
-
processed_content = Burek::Parser.replace_burek_calls(content, to_replace)
|
102
|
-
unless processed_content.nil?
|
103
|
-
File.open(file_name, 'w') do |output_file|
|
104
|
-
output_file.print processed_content
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def self.for_each_file
|
112
|
-
Burek.config(:search_folders).each do |folder|
|
113
|
-
Dir.glob(folder) do |file_name|
|
114
|
-
unless File.directory?(file_name)
|
115
|
-
yield file_name
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
end
|
data/lib/parser.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module Burek
|
2
|
-
|
3
|
-
module Parser
|
4
|
-
|
5
|
-
def self.find_burek_calls(string)
|
6
|
-
string.scan(burek_call_regex).flatten
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.replace_burek_calls(contents, to_replace)
|
10
|
-
matches = find_burek_calls(contents)
|
11
|
-
changes_made = false
|
12
|
-
matches.each do |value|
|
13
|
-
if to_replace.has_key?(value)
|
14
|
-
contents.gsub!(burek_call_regex(value)," t('#{to_replace[value]}')")
|
15
|
-
changes_made = true
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
return contents if changes_made
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.yaml_to_i18n_file(yaml)
|
23
|
-
yaml.lines.to_a[1..-1].join
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def self.burek_call_regex(caption="(?<key>[^\\)]*)")
|
29
|
-
Regexp.new("[^a-zA-Z0-9_]burek[ \\t]*\\([ \\t]*('|\")#{caption}\('|\")[^\\)]*\\)")
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|