rtlit 0.0.2

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.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in artieller.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Zohar Arad
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.
@@ -0,0 +1,62 @@
1
+ # RTLit
2
+
3
+ CSS left-to-right to right-to-left converter.
4
+
5
+ Takes CSS, LESS, SASS files and converts them from a left-to-right orientation to a right-to-left orientation. Useful when trying to convert English / Latin / LTR based stylesheets to support RTL languages like Hebrew / Arabic.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'rtlit'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rtlit
20
+
21
+ ## Usage
22
+
23
+ You can use RTLit in one of three ways
24
+
25
+ ### In your code
26
+
27
+ Include RTLit in your application and convert any CSS string to RTL
28
+
29
+ require 'rtlit'
30
+
31
+ ltr_css = File.open('/path/to/ltr/file.css','r') { |f| f.read }
32
+ rtl_css = RTLit::Converter.to_rtl ltr_css
33
+
34
+ ### As a Rake task
35
+
36
+ Convert a single file with RTLit
37
+
38
+ rake rtlit:convert[/path/to/src.css,/path/to/dest.css]
39
+
40
+ Convert a directory with RTLit
41
+
42
+ rake rtlit:convert[/path/to/src/,/path/to/dest/]
43
+
44
+ Convert files in a directory filtered by extension
45
+
46
+ rake rtlit:convert[/path/to/src/,/path/to/dest/, less] # will convert only *.less files in /path/to/src/
47
+
48
+ ### As a CLI command
49
+
50
+ $ rtlit /path/to/src/file.css /path/to/dest/file-rtl.css # convert /path/to/src/file.css and output to /path/to/dest/file-rtl.css
51
+
52
+ $ rtlit /path/to/src /path/to/dest # convert all files in /path/to/src/ and output to /path/to/dest
53
+
54
+ $ rtlit -x less /path/to/src /path/to/dest # convert only *.less files in /path/to/src/
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ $LOAD_PATH.unshift 'lib'
6
+ load 'tasks/rtlit.rake'
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/test*.rb']
11
+ t.verbose = true
12
+ end
13
+
14
+ desc 'Default: run tests'
15
+ task :default => [:test]
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
4
+ require 'rtlit'
5
+ require 'optparse'
6
+
7
+ module RTLit
8
+
9
+ module CLI
10
+
11
+ class << self #nodoc
12
+
13
+ # Convert file or files in directory to RTL
14
+ # Accepts source and destination paths and an optional file extension string
15
+ # to filter source directory by
16
+ def to_rtl
17
+
18
+ args = {}
19
+ OptionParser.new do |opts|
20
+ opts.banner = "Usage: rtlit [options]"
21
+
22
+ opts.on("-x EXTENSION", "--ext EXTENSION", "Parse only files matching extension") do |ext|
23
+ args[:ext] = ext
24
+ end
25
+
26
+ end.parse!
27
+
28
+ args[:src] = ARGV[0]
29
+ args[:dest] = ARGV[1]
30
+
31
+ RTLit::Util.process_file_or_directory(args[:src], args[:dest], args[:ext])
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ RTLit::CLI.to_rtl
@@ -0,0 +1,3 @@
1
+ require 'rtlit/converter'
2
+ require 'rtlit/util'
3
+ require 'rtlit/version'
@@ -0,0 +1,52 @@
1
+ module RTLit
2
+
3
+ module Converter
4
+
5
+ class << self
6
+
7
+ # Convert CSS properties and values from LTR to RTL.
8
+ # Accepts any valid CSS string.
9
+ # Returns a CSS string.
10
+ #
11
+ # @param [String, #read] css the css string to convert to RTL
12
+ # @return [String] the RTL version of the css
13
+ #
14
+ # Example:
15
+ #
16
+ # css = "
17
+ # body {
18
+ # text-align: left;
19
+ # padding-right: 10px;
20
+ # }"
21
+ #
22
+ # to_rtl(css)
23
+ #
24
+ def to_rtl(css) ''
25
+ place_holder = '|====|'
26
+ converted_css = css.gsub 'ltr', 'rtl'
27
+
28
+ ltr_matches = converted_css.scan /^([\n\s]*[^:\n\t\s\.\#\{\}]*(left|right)[^:]*:)|(:[^\w\.\#\{\}]*[^;\.\#\{\}]*(left|right)[^;]*;)/
29
+ css_to_replace = ltr_matches.flatten.delete_if{|a| a.nil? || ['left','right'].include?(a) }
30
+ css_to_replace.each do |match|
31
+ next if match.include? 'right' or match =~ /(left|right)\s(top|bottom|\d+[\w%]+)/
32
+ converted_css.gsub! match, (match.gsub 'left', place_holder)
33
+ end
34
+ css_to_replace.each do |match|
35
+ next if match.include? 'left' or match =~ /(left|right)\s(top|bottom|\d+[\w%]+)/
36
+ converted_css.gsub! match, (match.gsub 'right', 'left')
37
+ end
38
+ converted_css.gsub! place_holder, 'right'
39
+
40
+ quad_matches = converted_css.scan /\d+[\w%]+\s\d+[\w%]+\s\d+[\w%]+\s\d+[\w%]+/
41
+ quad_matches.each do |m|
42
+ t, r, b, l = m.split ' '
43
+ converted_css.gsub! m, [t,l,b,r].join(' ')
44
+ end
45
+ return converted_css
46
+ end #to_rtl
47
+
48
+ end # class << self
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,10 @@
1
+ require 'rtlit'
2
+
3
+ namespace :rtlit do
4
+
5
+ desc 'convert CSS file or files in directory from LTR to RTL'
6
+ task :convert, [:src, :dest, :ext] do |t, args|
7
+ RTLit::Util.process_file_or_directory(args[:src], args[:dest], args[:ext])
8
+ end
9
+
10
+ end
@@ -0,0 +1,54 @@
1
+ module RTLit
2
+
3
+ module Util
4
+
5
+ class << self
6
+
7
+ # Process file <src> and write RTL version to file <dest>
8
+ # Accepts source and destination file paths.
9
+ # Example:
10
+ # process_file('/path/to/file.css', '/path/to/file-rtl.css')
11
+ def process_file(src, dest)
12
+ puts 'Reading %s' % src
13
+ css = File.open(src,'r'){ |f| f.read }
14
+ rtl_css = RTLit::Converter.to_rtl css
15
+ puts 'writing %s' % dest
16
+ File.open(dest,'w'){ |f| f.write rtl_css }
17
+ end
18
+
19
+ # Process all files in directory, optionally filtered by extension <ext>
20
+ # Accepts source and destination directories and an optional file extension string
21
+ # Exmaple: process_directory('/tmp', '/tmp/rtl', 'less')
22
+ def process_directory(src, dest, ext = nil)
23
+ src_path = '%s/*.%s' % [File.expand_path(src), ext || '*']
24
+ dest_path = File.expand_path dest
25
+ Dir.glob(src_path) do |file|
26
+ dest_file = File.join(dest_path,File.basename(file))
27
+ process_file(file, dest_file)
28
+ end
29
+ end
30
+
31
+ # Process file or directory, which can optionally be filtered by file extension
32
+ # Accepts source and destination paths to file or directory to process and
33
+ # an optional file extension string
34
+ # Example:
35
+ # process_file_or_directory('/some/path', '/some/dest', 'less') # process only .less files directory
36
+ # process_file_or_directory('/some/file.css','/some/file-rtl.css')
37
+ def process_file_or_directory(src, dest, ext = nil)
38
+
39
+ raise 'Source not given' if src.nil? or not File.exists? src
40
+
41
+ raise 'Destination not given' if dest.nil? or not File.exists? dest
42
+
43
+ if File.directory? src
44
+ process_directory(src, dest, ext)
45
+ else
46
+ process_file(src, dest)
47
+ end
48
+ end
49
+
50
+ end #class << self
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ module RTLit
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rtlit/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Zohar Arad"]
6
+ gem.email = ["zohar@zohararad.com"]
7
+ gem.description = %q{Converts CSS files from left-to-right to right-to-left}
8
+ gem.summary = %q{CSS left-to-right to right-to-left converter}
9
+ gem.homepage = "https://github.com/zohararad/rtlit"
10
+ gem.rubyforge_project = "rtlit"
11
+
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "rtlit"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = RTLit::VERSION
18
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'rtlit/tasks'
@@ -0,0 +1,31 @@
1
+ body {
2
+ direction: ltr;
3
+ padding: 10px 15px 20px;
4
+ }
5
+
6
+ .left {
7
+ float: left;
8
+ text-align: left;
9
+ }
10
+
11
+ .right {
12
+ float: right;
13
+ text-align: right;
14
+ }
15
+
16
+ .pos_left {
17
+ position: absolute;
18
+ left: 0;
19
+ top: 0;
20
+ }
21
+
22
+ .pos_right {
23
+ position: absolute;
24
+ right: 0;
25
+ bottom: 0;
26
+ }
27
+
28
+ .nav {
29
+ margin: 10px 5px 10px 8px;
30
+ padding: 3px 5px 3px 8px;
31
+ }
@@ -0,0 +1,31 @@
1
+ body {
2
+ direction: rtl;
3
+ padding: 10px 15px 20px;
4
+ }
5
+
6
+ .left {
7
+ float: right;
8
+ text-align: right;
9
+ }
10
+
11
+ .right {
12
+ float: left;
13
+ text-align: left;
14
+ }
15
+
16
+ .pos_left {
17
+ position: absolute;
18
+ right: 0;
19
+ top: 0;
20
+ }
21
+
22
+ .pos_right {
23
+ position: absolute;
24
+ left: 0;
25
+ bottom: 0;
26
+ }
27
+
28
+ .nav {
29
+ margin: 10px 8px 10px 5px;
30
+ padding: 3px 8px 3px 5px;
31
+ }
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'test/unit'
3
+ require 'rtlit'
4
+
5
+ class FileConversion < Test::Unit::TestCase
6
+
7
+ def setup
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ def test_file_conversion
14
+ path = File.dirname(__FILE__)
15
+ ltr_css = File.open(File.join(path,'assets/ltr.css'),'r'){ |f| f.read }
16
+ rtl_css = File.open(File.join(path,'assets/rtl.css'),'r'){ |f| f.read }
17
+ converted_css = RTLit::Converter.to_rtl ltr_css
18
+ assert_equal rtl_css, converted_css, 'file conversion failed'
19
+ end
20
+
21
+ end
@@ -0,0 +1,97 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'test/unit'
3
+ require 'rtlit'
4
+
5
+ class LtrToRtl < Test::Unit::TestCase
6
+
7
+ def setup
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ def test_float_left_conversion
14
+ css = 'float: left;'
15
+ rtl_css = RTLit::Converter.to_rtl css
16
+ assert_equal 'float: right;', rtl_css, 'float left conversion failed'
17
+ end
18
+
19
+ def test_float_right_conversion
20
+ css = 'float: right;'
21
+ rtl_css = RTLit::Converter.to_rtl css
22
+ assert_equal 'float: left;', rtl_css, 'float right conversion failed'
23
+ end
24
+
25
+ def test_text_align_conversion
26
+ css = 'text-align: left;'
27
+ rtl_css = RTLit::Converter.to_rtl css
28
+ assert_equal 'text-align: right;', rtl_css, 'text-align conversion failed'
29
+ end
30
+
31
+ def test_left_position_conversion
32
+ css = 'left: 10px;'
33
+ rtl_css = RTLit::Converter.to_rtl css
34
+ assert_equal 'right: 10px;', rtl_css, 'left position conversion failed'
35
+ end
36
+
37
+ def test_right_position_conversion
38
+ css = 'right: 10px;'
39
+ rtl_css = RTLit::Converter.to_rtl css
40
+ assert_equal 'left: 10px;', rtl_css, 'right position conversion failed'
41
+ end
42
+
43
+ def test_bg_position_conversion
44
+ css = 'background-position: left top;'
45
+ rtl_css = RTLit::Converter.to_rtl css
46
+ assert_equal 'background-position: left top;', rtl_css, 'background position conversion failed'
47
+ end
48
+
49
+ def test_margin_left_conversion
50
+ css = 'margin-left: 10px;'
51
+ rtl_css = RTLit::Converter.to_rtl css
52
+ assert_equal 'margin-right: 10px;', rtl_css, 'margin left conversion failed'
53
+ end
54
+
55
+ def test_margin_right_conversion
56
+ css = 'margin-right: 10px;'
57
+ rtl_css = RTLit::Converter.to_rtl css
58
+ assert_equal 'margin-left: 10px;', rtl_css, 'margin right conversion failed'
59
+ end
60
+
61
+ def test_padding_left_conversion
62
+ css = 'padding-left: 10px;'
63
+ rtl_css = RTLit::Converter.to_rtl css
64
+ assert_equal 'padding-right: 10px;', rtl_css, 'padding left conversion failed'
65
+ end
66
+
67
+ def test_padding_right_conversion
68
+ css = 'padding-right: 10px;'
69
+ rtl_css = RTLit::Converter.to_rtl css
70
+ assert_equal 'padding-left: 10px;', rtl_css, 'padding right conversion failed'
71
+ end
72
+
73
+ def test_margin_quad_conversion
74
+ css = 'margin: 1px 2px 3px 4px;'
75
+ rtl_css = RTLit::Converter.to_rtl css
76
+ assert_equal 'margin: 1px 4px 3px 2px;', rtl_css, 'margin quad conversion failed'
77
+ end
78
+
79
+ def test_padding_quad_conversion
80
+ css = 'padding: 1px 2px 3px 4px;'
81
+ rtl_css = RTLit::Converter.to_rtl css
82
+ assert_equal 'padding: 1px 4px 3px 2px;', rtl_css, 'padding quad conversion failed'
83
+ end
84
+
85
+ def test_margin_tri_conversion
86
+ css = 'margin: 1px 2px 3px;'
87
+ rtl_css = RTLit::Converter.to_rtl css
88
+ assert_equal 'margin: 1px 2px 3px;', rtl_css, 'margin tri conversion failed'
89
+ end
90
+
91
+ def test_padding_tri_conversion
92
+ css = 'padding: 1px 2px 3px;'
93
+ rtl_css = RTLit::Converter.to_rtl css
94
+ assert_equal 'padding: 1px 2px 3px;', rtl_css, 'padding tri conversion failed'
95
+ end
96
+
97
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtlit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zohar Arad
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Converts CSS files from left-to-right to right-to-left
15
+ email:
16
+ - zohar@zohararad.com
17
+ executables:
18
+ - rtlit
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - bin/rtlit
28
+ - lib/rtlit.rb
29
+ - lib/rtlit/converter.rb
30
+ - lib/rtlit/tasks.rb
31
+ - lib/rtlit/util.rb
32
+ - lib/rtlit/version.rb
33
+ - rtlit.gemspec
34
+ - tasks/rtlit.rake
35
+ - test/assets/ltr.css
36
+ - test/assets/rtl.css
37
+ - test/test_file_conversion.rb
38
+ - test/test_ltr_to_rtl.rb
39
+ homepage: https://github.com/zohararad/rtlit
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project: rtlit
59
+ rubygems_version: 1.8.17
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: CSS left-to-right to right-to-left converter
63
+ test_files:
64
+ - test/assets/ltr.css
65
+ - test/assets/rtl.css
66
+ - test/test_file_conversion.rb
67
+ - test/test_ltr_to_rtl.rb
68
+ has_rdoc: