css2stylus 0.0.1

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.
Files changed (3) hide show
  1. data/bin/css2stylus +11 -0
  2. data/lib/css2stylus.rb +65 -0
  3. metadata +49 -0
data/bin/css2stylus ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require 'css2stylus'
3
+
4
+ file = File.basename(ARGV[0])
5
+ filename = File.basename(file, File.extname(file))
6
+ css = File.read(file)
7
+
8
+ converter = Css2Stylus::Converter.new(css)
9
+ converter.process
10
+
11
+ File.open(filename + '.styl', 'w') { |file| file.write(converter.get) }
data/lib/css2stylus.rb ADDED
@@ -0,0 +1,65 @@
1
+ module Css2Stylus
2
+
3
+ class Converter
4
+
5
+ def initialize(css=nil)
6
+ if not css.nil?
7
+ @css = css
8
+ end
9
+ end
10
+
11
+ def process
12
+ if @css.nil? || @css.empty?
13
+ return false
14
+ end
15
+ @tree = {}
16
+ @stylus = ''
17
+ generate_tree
18
+ render
19
+ return true
20
+ end
21
+
22
+ def get
23
+ return @stylus
24
+ end
25
+
26
+ private
27
+
28
+ def add_rule(tree, selectors, style)
29
+ return if style.nil? || style.empty?
30
+ if selectors.empty?
31
+ (tree[:style] ||= '') << style
32
+ else
33
+ first, rest = selectors.first, selectors[1..-1]
34
+ node = (tree[first] ||= {})
35
+ add_rule(node, rest, style)
36
+ end
37
+ end
38
+
39
+ def generate_tree
40
+ @css.split("\n").map { |l| l.strip }.join.gsub(/\/\*+[^\*]*\*+\//, '').split(/[\{\}]/).each_slice(2) do |style|
41
+ rules = style[0].strip
42
+ if rules.include?(',')
43
+ add_rule(@tree, [rules], style[1])
44
+ else
45
+ add_rule(@tree, rules.split(/\s+/), style[1])
46
+ end
47
+ end
48
+ end
49
+
50
+ def render(tree=nil, indent=0)
51
+ if tree.nil?
52
+ tree = @tree
53
+ end
54
+ tree.each do |element, children|
55
+ @stylus = @stylus + ' ' * indent + element + "\n"
56
+ style = children.delete(:style)
57
+ if style
58
+ @stylus = @stylus + style.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| ' ' * (indent + 2) + s.gsub(/:/, '') }.join("\n") + "\n"
59
+ end
60
+ render(children, indent + 2)
61
+ end
62
+ end
63
+
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: css2stylus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Denis Ciccale
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A ruby library to convert CSS into Stylus.
15
+ email:
16
+ - dciccale@gmail.com
17
+ executables:
18
+ - css2stylus
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/css2stylus.rb
23
+ - bin/css2stylus
24
+ homepage: https://github.com/dciccale/css2stylus
25
+ licenses:
26
+ - MIT
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.24
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: CSS to Stylus converter
49
+ test_files: []