web_optimizer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -2
- data/lib/common/google_translator.rb +32 -0
- data/lib/common/mechanize_helper.rb +48 -0
- data/lib/common/util.rb +18 -0
- data/lib/web_optimizer/version.rb +1 -1
- data/lib/web_stuff/yaml_translations.rb +52 -0
- data/lib/web_stuff.rb +7 -0
- metadata +7 -2
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Web Optimizer
|
2
2
|
|
3
3
|
Some util script to optime your assets (images, js files, css files).
|
4
4
|
Contribute more are welcome, please drop me an email at
|
@@ -19,13 +19,20 @@ vo.mita.ov at gmail.com if you need help or want to contribute
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
-
Compress
|
22
|
+
Compress Stuffs
|
23
23
|
```
|
24
24
|
WebOptimizer.compress_css(dir, ignore_paths=[])
|
25
25
|
WebOptimizer.compress_js(dir, ignore_paths=[])
|
26
26
|
WebOptimizer.compress_img(dir, ignore_paths=[])
|
27
27
|
```
|
28
28
|
|
29
|
+
Translation Yaml files
|
30
|
+
```
|
31
|
+
require "web_stuff"
|
32
|
+
dir_path = "/Users/tamvo/code/rails/minesweeper/config/locales"
|
33
|
+
WebStuff::YamlTranslations.translations_locale_dir(dir_path, "en", "it")
|
34
|
+
```
|
35
|
+
|
29
36
|
## Contributing
|
30
37
|
|
31
38
|
1. Fork it
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "mechanize"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
require "common/mechanize_helper"
|
4
|
+
# require File.expand_path(File.join(File.dirname(__FILE__), "mechanize_helper.rb"))
|
5
|
+
include Common::MechanizeHelper
|
6
|
+
|
7
|
+
module Common
|
8
|
+
module GoogleTranslator
|
9
|
+
def self.translate(str, from_locale, to_locale)
|
10
|
+
str = str.strip
|
11
|
+
return "" if str.blank?
|
12
|
+
params = {
|
13
|
+
"client" => "t",
|
14
|
+
"sl" => from_locale,
|
15
|
+
"tl" => to_locale,
|
16
|
+
"hl" => "en",
|
17
|
+
"sc" => "2",
|
18
|
+
"ie" => "UTF-8",
|
19
|
+
"oe" => "UTF-8",
|
20
|
+
"oc" => "1",
|
21
|
+
"otf" => "2",
|
22
|
+
"ssel" => "3",
|
23
|
+
"tsel" => "6",
|
24
|
+
"q" => str
|
25
|
+
}
|
26
|
+
result = agent.get("http://translate.google.com.vn/translate_a/t", params)
|
27
|
+
json = JSON(result.body.match(/^\[(\[\[.*?\]\])/)[1])
|
28
|
+
json[0][0]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Common
|
2
|
+
module MechanizeHelper
|
3
|
+
def agent(force_new=false)
|
4
|
+
if force_new
|
5
|
+
@_agent = Mechanize.new
|
6
|
+
else
|
7
|
+
@_agent ||= Mechanize.new
|
8
|
+
end
|
9
|
+
@_agent.keep_alive = false
|
10
|
+
@_agent.open_timeout = 10
|
11
|
+
@_agent.read_timeout = 120
|
12
|
+
@_agent.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) Gecko/20100101 Firefox/19.0'
|
13
|
+
@_agent.ssl_version = 'SSLv3'
|
14
|
+
# @_agent.set_proxy("localhost", 8888)
|
15
|
+
@_agent
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_timeout(seconds)
|
19
|
+
@_agent.open_timeout = seconds
|
20
|
+
end
|
21
|
+
|
22
|
+
def html_page body
|
23
|
+
uri = URI 'http://example/'
|
24
|
+
Mechanize::Page.new uri, nil, body, 200, agent
|
25
|
+
end
|
26
|
+
|
27
|
+
def post_multipart(url, post_params, headers={})
|
28
|
+
post_params = post_params.with_indifferent_access
|
29
|
+
page = self.html_page <<-BODY
|
30
|
+
<form action="#{url}" enctype="multipart/form-data" method="POST">
|
31
|
+
</form>
|
32
|
+
BODY
|
33
|
+
|
34
|
+
page.forms.first.tap do |form|
|
35
|
+
post_params.each do |key, value|
|
36
|
+
if value.is_a?(IO)
|
37
|
+
ul = Mechanize::Form::FileUpload.new({'name' => key.to_s},::File.basename(value.path))
|
38
|
+
ul.file_data = value.read
|
39
|
+
form.file_uploads << ul
|
40
|
+
else
|
41
|
+
form.add_field! key.to_s, value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end.submit nil, headers
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
data/lib/common/util.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Common
|
2
|
+
module Util
|
3
|
+
def self.write_file(file_name, content, append=false)
|
4
|
+
File.open(file_name, append ? "a" : "w") do |f|
|
5
|
+
f.write(content)
|
6
|
+
end
|
7
|
+
rescue Exception => ex
|
8
|
+
write_file(file_name, content.force_encoding('UTF-8'), append)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.yaml(hash)
|
12
|
+
method = hash.respond_to?(:ya2yaml) ? :ya2yaml : :to_yaml
|
13
|
+
string = hash.deep_stringify_keys.send(method)
|
14
|
+
string.gsub("!ruby/symbol ", ":").sub("---","").split("\n").map(&:rstrip).join("\n").strip
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "fileutils"
|
2
|
+
require "active_support"
|
3
|
+
|
4
|
+
module WebStuff
|
5
|
+
extend Common::GoogleTranslator
|
6
|
+
|
7
|
+
module YamlTranslations
|
8
|
+
def self.translations_locale_dir(dir_path, from_locale, to_locale)
|
9
|
+
raise Exception.new("Invalid dir path") unless File.directory? dir_path
|
10
|
+
puts "translations_locale_dir"
|
11
|
+
|
12
|
+
Dir[File.join(dir_path, "**/*.#{from_locale}.yml"), File.join(dir_path, "**/#{from_locale}.yml")].each do |locale_path|
|
13
|
+
if File.basename(locale_path) == "#{from_locale}.yml"
|
14
|
+
dest_file = File.join(dir_path, "#{to_locale}.yml")
|
15
|
+
else
|
16
|
+
dest_file = File.join(dir_path, "#{File.basename(locale_path, ".#{from_locale}.yml")}.#{to_locale}.yml")
|
17
|
+
end
|
18
|
+
if File.exists?(dest_file)
|
19
|
+
puts "[skip] #{locale_path}"
|
20
|
+
next
|
21
|
+
end
|
22
|
+
puts "---#{locale_path}"
|
23
|
+
hash = YAML.load_file(locale_path)
|
24
|
+
result_hash = self.traverse_hash(hash, from_locale, to_locale)
|
25
|
+
Common::Util.write_file(dest_file, Common::Util.yaml(result_hash))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
def self.traverse_hash(hash, from_locale, to_locale)
|
31
|
+
hash.each do |key, value|
|
32
|
+
if value.is_a?(Hash)
|
33
|
+
self.traverse_hash(value, from_locale, to_locale)
|
34
|
+
elsif value.is_a?(String)
|
35
|
+
translation = Common::GoogleTranslator.translate(value, from_locale, to_locale)
|
36
|
+
translation = translation.gsub(/\n/, "").gsub(/% s/, "%s").gsub(/ /, " ").
|
37
|
+
gsub(/<(\/?) (\w+) >/, '<\1\2>').gsub(/ ([\.,!?])( |$)/, '\1')
|
38
|
+
|
39
|
+
translation_data = translation.scan(/{ ?{.*?} ?}/)
|
40
|
+
value_data = value.scan(/{{.*?}}/)
|
41
|
+
if translation_data.present?
|
42
|
+
translation_data.each_with_index do |trans, index|
|
43
|
+
translation = translation.gsub(trans, value_data[index])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
hash[key] = translation
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/lib/web_stuff.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web_optimizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: active_support
|
@@ -60,12 +60,17 @@ files:
|
|
60
60
|
- data/balance.js
|
61
61
|
- data/balance.js.bak
|
62
62
|
- data/screenshot.png
|
63
|
+
- lib/common/google_translator.rb
|
64
|
+
- lib/common/mechanize_helper.rb
|
65
|
+
- lib/common/util.rb
|
63
66
|
- lib/web_optimizer.rb
|
64
67
|
- lib/web_optimizer/common.rb
|
65
68
|
- lib/web_optimizer/compressor_css.rb
|
66
69
|
- lib/web_optimizer/compressor_js.rb
|
67
70
|
- lib/web_optimizer/optimize_images.rb
|
68
71
|
- lib/web_optimizer/version.rb
|
72
|
+
- lib/web_stuff.rb
|
73
|
+
- lib/web_stuff/yaml_translations.rb
|
69
74
|
- tmp.rb
|
70
75
|
- web_optimizer.gemspec
|
71
76
|
homepage: http://github.com/tamvo/web_optimizer
|