css2less 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/css2less +9 -0
  2. data/lib/css2less.rb +100 -0
  3. metadata +51 -0
data/bin/css2less ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'css2less'
4
+
5
+ css = File.read(ARGV[0])
6
+
7
+ converter = Css2Less::Converter.new(css)
8
+ converter.process_less
9
+ puts converter.get_less
data/lib/css2less.rb ADDED
@@ -0,0 +1,100 @@
1
+ # Copyright 2012 Thomas Pierson <contact@thomaspierson.fr> ,
2
+ # Marcin Kulik <m@ku1ik.com>
3
+ #
4
+ # This file is part of Css2Less Library.
5
+ #
6
+ # Css2Less Library is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Css2Less Library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ module Css2Less
20
+
21
+ require 'enumerator'
22
+
23
+ class Converter
24
+
25
+ def initialize(css=nil)
26
+ if not css.nil?
27
+ @css = css
28
+ end
29
+ @tree = {}
30
+ @less = ''
31
+ end
32
+
33
+ def process_less
34
+ cleanup
35
+ if @css.nil?
36
+ return false
37
+ end
38
+ if @css.empty?
39
+ return false
40
+ end
41
+ generate_tree
42
+ render_less
43
+ return true
44
+ end
45
+
46
+ def get_less
47
+ return @less
48
+ end
49
+
50
+ def set_css(css)
51
+ @css = css
52
+ end
53
+
54
+ private
55
+
56
+ def cleanup
57
+ @tree = {}
58
+ @less = ''
59
+ end
60
+
61
+ def add_rule(tree, selectors, style)
62
+ return if style.nil? || style.empty?
63
+ if selectors.empty?
64
+ (tree[:style] ||= ';') << style
65
+ else
66
+ first, rest = selectors.first, selectors[1..-1]
67
+ node = (tree[first] ||= {})
68
+ add_rule(node, rest, style)
69
+ end
70
+ end
71
+
72
+ def generate_tree
73
+ @css.split("\n").map { |l| l.strip }.join.gsub(/\/\*+[^\*]*\*+\//, '').split(/[\{\}]/).each_slice(2) do |style|
74
+ rules = style[0].strip
75
+ if rules.include?(',') # leave multiple rules alone
76
+ add_rule(@tree, [rules], style[1])
77
+ else
78
+ add_rule(@tree, rules.split(/\s+/), style[1])
79
+ end
80
+ end
81
+ end
82
+
83
+ def render_less(tree=nil, indent=0)
84
+ if tree.nil?
85
+ tree = @tree
86
+ end
87
+ tree.each do |element, children|
88
+ @less = @less + ' ' * indent + element + " {\n"
89
+ style = children.delete(:style)
90
+ if style
91
+ @less = @less + style.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| ' ' * (indent+4) + s + ";" }.join("\n") + "\n"
92
+ end
93
+ render_less(children, indent + 4)
94
+ @less = @less + ' ' * indent + "}\n"
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css2less
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Pierson
9
+ - Marcin Kulik
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-04-23 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: A ruby library which convert old CSS stylesheet into LESS dynamic stylesheet.
16
+ email:
17
+ - contact@thomaspierson.fr
18
+ - m@ku1ik.com
19
+ executables:
20
+ - css2less
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/css2less.rb
25
+ - bin/css2less
26
+ homepage: https://github.com/thomaspierson/libcss2less
27
+ licenses:
28
+ - GPL-3
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.23
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: CSS to LESS converter
51
+ test_files: []