css_convertor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in css_convertor.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 nour
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # CssConvertor
2
+
3
+ Flips the layout of your CSS file. You can use it if you're designing a website that supports both LTR and RTL languages: CssConvertor will create a seperate CSS file of the new layout.
4
+ It was originally based on https://github.com/ded/R2
5
+
6
+ ## Explained
7
+
8
+ CssConvertor reads your CSS file, and when it finds something like this:
9
+
10
+ ```css
11
+ p {
12
+ margin-right: 2px;
13
+ padding: 1px 2px 3px 4px;
14
+ left: 5px;
15
+ }
16
+ #container {
17
+ float:right;
18
+ text-align: right;
19
+ }
20
+ ```
21
+
22
+ It will change it to:
23
+
24
+ ```css
25
+ p {
26
+ margin-left: 2px;
27
+ padding: 1px 4px 3px 2px;
28
+ right: 5px;
29
+ }
30
+ #container {
31
+ float:left;
32
+ text-align: left;
33
+ }
34
+ ```
35
+
36
+ ## Installation
37
+
38
+ Add this line to your application's Gemfile:
39
+
40
+ gem "css_convertor"
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install css_convertor
49
+
50
+ ## Usage
51
+
52
+ ```
53
+ $ css_convertor file.css
54
+ ```
55
+ And if you didn't have this, add it to your CSS file
56
+ ```css
57
+ body {
58
+ direction: rtl;
59
+ }
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ end
8
+ desc "Run tests"
9
+ task :default => :test
data/bin/css_convertor ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'css_convertor'
4
+
5
+ case ARGV.size
6
+ when 1 then CssConvertor.convert(ARGV[0])
7
+ else puts CssConvertor.style("Usage:\n $ css_convertor file.css", :color=>:cyan); exit
8
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/css_convertor/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nour"]
6
+ gem.email = ["nour.fwh@gmail.com"]
7
+ gem.description = %q{css_convertor flips the layout of your CSS file. You can use it if you're designing a website that supports both LTR and RTL languages: css_convertor will create a seperate CSS file of the new layout.}
8
+ gem.summary = %q{CSS LTR-RTL layout converter}
9
+ gem.homepage = "http://rubygems.org/gems/css_convertor"
10
+ gem.homepage = ""
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "css_convertor"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = CssConvertor::VERSION
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'test-unit'
20
+ end
@@ -0,0 +1,32 @@
1
+ module CssConvertor
2
+ class Command
3
+ def initialize(filepath)
4
+ @filepath = filepath
5
+ end
6
+ def rtl_filename
7
+ @filepath.gsub(/(.*?).css/) {"#{$1}-rtl.css"}
8
+ end
9
+ def convert
10
+ css = ''
11
+
12
+ puts CssConvertor.style("\nOpening file..", :color=>:yellow)
13
+ File.open(@filepath, 'r') {|f| css = f.read}
14
+ puts CssConvertor.style("Opening file..OK", :color=>:green)
15
+
16
+ puts CssConvertor.style("\nScanning and replacing..", :color=>:yellow)
17
+ css = CssConvertor.replace(css)
18
+ puts CssConvertor.style("Scanning and replacing..OK", :color=>:green)
19
+
20
+ puts CssConvertor.style("\nWriting to new file..", :color=>:yellow)
21
+ new_filename = rtl_filename
22
+ File.open(new_filename, 'w') {|f| f << css}
23
+ puts CssConvertor.style("Writing to new file..OK", :color=>:green)
24
+ puts CssConvertor.style("\nDONE. Check: #{new_filename}",
25
+ :color=>:green, :bold=>true)
26
+ rescue Exception => e
27
+ puts CssConvertor.style("\nTerminating because of these errors:", :color=>:red)
28
+ puts CssConvertor.style(e.message, :color=>:red)
29
+ puts e.backtrace
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,91 @@
1
+ module CssConvertor
2
+ module Convertor
3
+ PROPERTY_MAP = {
4
+ 'margin-left'=> 'margin-right',
5
+ 'margin-right'=> 'margin-left',
6
+
7
+ 'padding-left'=> 'padding-right',
8
+ 'padding-right'=> 'padding-left',
9
+
10
+ 'border-left'=> 'border-right',
11
+ 'border-right'=> 'border-left',
12
+
13
+ 'border-left-width'=> 'border-right-width',
14
+ 'border-right-width'=> 'border-left-width',
15
+
16
+ 'border-left-color'=> 'border-right-color',
17
+ 'border-right-color'=> 'border-left-color',
18
+
19
+ 'border-left-style'=> 'border-right-style',
20
+ 'border-right-style'=> 'border-left-style',
21
+
22
+ 'border-radius-bottomleft'=> 'border-radius-bottomright',
23
+ 'border-radius-bottomright'=> 'border-radius-bottomleft',
24
+ 'border-bottom-right-radius'=> 'border-bottom-left-radius',
25
+ 'border-bottom-left-radius'=> 'border-bottom-right-radius',
26
+ '-webkit-border-bottom-right-radius'=> '-webkit-border-bottom-left-radius',
27
+ '-webkit-border-bottom-left-radius'=> '-webkit-border-bottom-right-radius',
28
+ '-moz-border-radius-bottomright'=> '-moz-border-radius-bottomleft',
29
+ '-moz-border-radius-bottomleft'=> '-moz-border-radius-bottomright',
30
+
31
+ 'border-radius-topleft'=> 'border-radius-topright',
32
+ 'border-radius-topright'=> 'border-radius-topleft',
33
+ 'border-top-right-radius'=> 'border-top-left-radius',
34
+ 'border-top-left-radius'=> 'border-top-right-radius',
35
+ '-webkit-border-top-right-radius'=> '-webkit-border-top-left-radius',
36
+ '-webkit-border-top-left-radius'=> '-webkit-border-top-right-radius',
37
+ '-moz-border-radius-topright'=> '-moz-border-radius-topleft',
38
+ '-moz-border-radius-topleft'=> '-moz-border-radius-topright',
39
+
40
+ 'left'=> 'right',
41
+ 'right'=> 'left'
42
+ }
43
+ FLIP_MAP = {'rtl'=>'ltr', 'ltr'=>'rtl', 'right'=>'left', 'left'=>'right'}
44
+ VALUE_MAP = {
45
+ 'padding'=> :quad,
46
+ 'margin'=> :quad,
47
+ 'border-color'=> :quad,
48
+ 'border-width'=> :quad,
49
+ 'border-style'=> :quad,
50
+ '-webkit-border-radius' => :quad_radius,
51
+ '-moz-border-radius'=> :quad_radius,
52
+ 'border-radius'=> :quad_radius,
53
+ 'text-align'=> :direction,
54
+ 'float'=> :direction,
55
+ 'clear'=> :direction,
56
+ 'direction'=> :direction
57
+ }
58
+ def scan_and_replace(str)
59
+ str.gsub(/([a-z\-]+)\s*:\s*(.+?)(?=\s*[;}])/) {"#{property($1)}: #{value($1, $2)}"}
60
+ end
61
+ def property(property_name)
62
+ PROPERTY_MAP[property_name] || property_name
63
+ end
64
+ def value(property_name, property_value)
65
+ VALUE_MAP[property_name] ? new_value(VALUE_MAP[property_name], property_value) : property_value
66
+ end
67
+ def new_value(type, value)
68
+ full_value = value
69
+ value = get_actual_value(value)
70
+ arr = split_property_value(value)
71
+
72
+ if type==:direction && FLIP_MAP[value]
73
+ full_value.gsub!(/#{Regexp.escape(value)}/, FLIP_MAP[value])
74
+ elsif type==:quad && arr.length==4
75
+ arr[1], arr[3] = arr[3], arr[1]
76
+ full_value.gsub!(/#{Regexp.escape(value)}/, arr.join(' '))
77
+ elsif type==:quad_radius && arr.length==4
78
+ arr[0], arr[1], arr[2], arr[3] = arr[1], arr[0], arr[3], arr[2]
79
+ full_value.gsub!(/#{Regexp.escape(value)}/, arr.join(' '))
80
+ end
81
+ full_value
82
+ end
83
+ def get_actual_value(value)
84
+ match_data = %r{(.+?) ?(?=!important|\\9|\\0/)}.match(value)
85
+ match_data ? match_data[1] : value
86
+ end
87
+ def split_property_value(value)
88
+ value.scan(/rgba?\(.*?\)|hsla?\(.*?\)|#[0-9a-f]{3,6}|[\w.%-]+/)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,21 @@
1
+ module CssConvertor
2
+ class SimpleStyler
3
+ CODES = {:reset=>0, :red=>31, :green=>32, :yellow=>33, :cyan=>36, :bold => 1}
4
+ CODES.default = CODES[:reset]
5
+ def initialize(text)
6
+ @text = text
7
+ end
8
+ def style_text(options)
9
+ exec_code(CODES[options[:color]])
10
+ exec_code(CODES[:bold]) if options[:bold] == true
11
+ reset
12
+ end
13
+ private
14
+ def exec_code(code)
15
+ @text = "\e[#{code}m#{@text}"
16
+ end
17
+ def reset
18
+ "#{@text}\e[#{CODES[:reset]}m"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module CssConvertor
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "css_convertor/convertor"
2
+ require "css_convertor/simple_styler"
3
+ require "css_convertor/command"
4
+ require "css_convertor/version"
5
+
6
+ include CssConvertor::Convertor
7
+ module CssConvertor
8
+ def self.replace(css)
9
+ scan_and_replace(css)
10
+ end
11
+ def self.style(text,options={:color=>:reset, :bold=>false})
12
+ SimpleStyler.new(text).style_text(options)
13
+ end
14
+ def self.convert(filepath)
15
+ Command.new(filepath).convert
16
+ end
17
+ end
@@ -0,0 +1,67 @@
1
+ require 'test/unit'
2
+ require 'css_convertor'
3
+
4
+ class ConvertorTest < Test::Unit::TestCase
5
+ include CssConvertor::Convertor
6
+
7
+ # get_actual_value(value)
8
+ def test_must_get_value_without_additions
9
+ assert_equal '3px 5px 3px 10px', get_actual_value('3px 5px 3px 10px !important')
10
+ assert_equal 'rgba(255,0,0)', get_actual_value('rgba(255,0,0) \0/')
11
+ assert_equal '#fff #000 #fff #000', get_actual_value('#fff #000 #fff #000\9')
12
+ end
13
+ # split_property_value(value)
14
+ def test_must_split_hex_values
15
+ assert_equal ['#eeeeee', '#fff'], split_property_value('#eeeeee #fff')
16
+ assert_equal ['#fff','#000','#fff','#000'], split_property_value('#fff #000 #fff #000')
17
+ end
18
+ def test_must_split_rgba_values
19
+ assert_equal ['rgb(0, 0, 0)','rgba(0, 0, 0, 0.15)'], split_property_value('rgb(0, 0, 0) rgba(0, 0, 0, 0.15)')
20
+ assert_equal ['rgba(0, 0, 0, 0.15)','rgb(0, 0, 0)','rgba(0, 0, 0, 0.15)','rgb(0, 0, 0)'], split_property_value('rgba(0, 0, 0, 0.15)rgb(0, 0, 0)rgba(0, 0, 0, 0.15)rgb(0, 0, 0)')
21
+ end
22
+ def test_must_split_hlsa_values
23
+ assert_equal ['hsl(10%, 40%, 20%)'], split_property_value('hsl(10%, 40%, 20%)')
24
+ assert_equal ['hsl(10%, 40%, 20%)','hsla(10%, 40%, 20%,0)', 'hsla(10%, 40%, 20%,0)'], split_property_value('hsl(10%, 40%, 20%) hsla(10%, 40%, 20%,0) hsla(10%, 40%, 20%,0)')
25
+ end
26
+ def test_must_split_length_values
27
+ assert_equal ['10px', '20%', '1.3em'], split_property_value('10px 20% 1.3em')
28
+ end
29
+ def test_must_split_word_values
30
+ assert_equal ['right'], split_property_value('right')
31
+ assert_equal ['red','blue','green','yellow'], split_property_value('red blue green yellow')
32
+ end
33
+ def test_must_split_mixed_values
34
+ assert_equal ['#ffff','rgba(0,0,0,0)','red','hsl(10%,10%,10%)'], split_property_value('#ffff rgba(0,0,0,0) red hsl(10%,10%,10%)')
35
+ end
36
+ # new_value(type, value)
37
+ def test_must_get_correct_new_value_with_quad
38
+ assert_equal '1px 2px 3px', new_value(:quad,'1px 2px 3px')
39
+ assert_equal '1px 4px 3px 2px', new_value(:quad,'1px 2px 3px 4px')
40
+ assert_equal '1px 4px 3px 2px !important', new_value(:quad,'1px 2px 3px 4px !important')
41
+ end
42
+ def test_must_get_correct_new_value_with_quad_radius
43
+ assert_equal '10% 20%', new_value(:quad_radius,'10% 20%')
44
+ assert_equal '10% 20% 3.3em 10px', new_value(:quad_radius,'20% 10% 10px 3.3em')
45
+ assert_equal '10% 20% 3.3em 10px\9', new_value(:quad_radius,'20% 10% 10px 3.3em\9')
46
+ end
47
+ def test_must_get_correct_new_value_with_direction
48
+ assert_equal 'left', new_value(:direction,'right')
49
+ assert_equal 'rtl !important', new_value(:direction,'ltr !important')
50
+ end
51
+ # value(property_name, property_value)
52
+ def test_must_get_correct_value
53
+ assert_equal 'justify', value('text-align','justify')
54
+ assert_equal '10px 30px 10px', value('margin','10px 30px 10px')
55
+ assert_equal 'right', value('float','left')
56
+ assert_equal '10px 25.5% 3.0em 20% !important', value('padding','10px 20% 3.0em 25.5% !important')
57
+ end
58
+ # property(property_name)
59
+ def test_must_get_correct_property_name
60
+ assert_equal 'margin', property('margin')
61
+ assert_equal 'margin-right', property('margin-left')
62
+ end
63
+ # scan_and_replace(str)
64
+ def test_must_scan_and_replace_correctly_or_else!
65
+ assert_equal 'p {margin-left: 10px;float: right}', scan_and_replace('p {margin-right: 10px;float: left}')
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css_convertor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nour
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: test-unit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! 'css_convertor flips the layout of your CSS file. You can use it if
47
+ you''re designing a website that supports both LTR and RTL languages: css_convertor
48
+ will create a seperate CSS file of the new layout.'
49
+ email:
50
+ - nour.fwh@gmail.com
51
+ executables:
52
+ - css_convertor
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - bin/css_convertor
62
+ - css_convertor.gemspec
63
+ - lib/css_convertor.rb
64
+ - lib/css_convertor/command.rb
65
+ - lib/css_convertor/convertor.rb
66
+ - lib/css_convertor/simple_styler.rb
67
+ - lib/css_convertor/version.rb
68
+ - test/test_convertor.rb
69
+ homepage: ''
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
82
+ - 0
83
+ hash: 533531759
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
91
+ - 0
92
+ hash: 533531759
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.24
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: CSS LTR-RTL layout converter
99
+ test_files:
100
+ - test/test_convertor.rb