sass-buddy 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0862e7491cb3f26b426c45c0fb94e7e91649053
4
+ data.tar.gz: 43b48af7a82676150db685a36f04d299fa0beebc
5
+ SHA512:
6
+ metadata.gz: 4be2c16565657ab90c564cc0af3f0fd1b8ad78d2e750625ead611d2c6c2d3806bd9e8d1897db7b6cd958b54e19550e2987e6d841dbc35aece261e1d74e44280c
7
+ data.tar.gz: 264c56527043ffeecdb8ac71aa6eab75c7fa85756fee3a1c214ad987d35db01429c37b0b69223e1d60efbbda70a39de1cf9ebe0537180389774780df159f395e
@@ -0,0 +1,92 @@
1
+ # rubocop:disable Style/FileName
2
+ require 'json'
3
+ require 'sassc'
4
+
5
+ # Pass either a file or a string containing scss and
6
+ # recieve a compiled string of css with or without
7
+ # style tags.
8
+ class SassBuddy
9
+ def initialize(opts)
10
+ @file = opts[:file]
11
+ @string, = opts[:string]
12
+ @container = opts[:container]
13
+ @additions = opts[:additions]
14
+ @require_paths = opts[:require_paths]
15
+ @manifest = opts[:rev_manifest] ? get_manifest(opts[:rev_manifest]) : {}
16
+ @syntax = opts[:sass] ? :sass : :scss
17
+ end
18
+ attr_reader :file, :string, :additions, :container, :require_paths, :manifest
19
+
20
+ def compile
21
+ revved(compiled_css) if compiled_css
22
+ end
23
+
24
+ def compile_html
25
+ "<style>#{revved(compiled_css)}</style>" if compiled_css
26
+ end
27
+
28
+ private
29
+
30
+ def get_manifest(file)
31
+ JSON.parse(File.open(file).read)
32
+ end
33
+
34
+ def revved(css)
35
+ manifest.each do |k, v|
36
+ css.gsub!(k, v)
37
+ end
38
+
39
+ css
40
+ end
41
+
42
+ def compiled_css
43
+ SassC::Engine.new(
44
+ stringed_additions + contents,
45
+ syntax: @syntax,
46
+ importer: ImportBuddy,
47
+ filename: file,
48
+ require_paths: require_paths
49
+ ).render
50
+ rescue SassC::SyntaxError
51
+ false
52
+ end
53
+
54
+ def stringed_additions
55
+ return '' unless additions
56
+
57
+ additions.inject('') do |out, (k, v)|
58
+ out + "$#{k}:##{v};"
59
+ end
60
+ end
61
+
62
+ def contents
63
+ @contents ||= begin
64
+ scss = file ? File.open(file).read : string
65
+ scss = "#{container} {#{scss}}" if container
66
+ scss
67
+ end
68
+ end
69
+ end
70
+
71
+ # :nodoc:
72
+ class ImportBuddy < SassC::Importer
73
+ def imports(path, _parent_path)
74
+ if path_match(path)
75
+ SassC::Importer::Import.new(path_match(path))
76
+ else
77
+ SassC::Importer::Import.new(path)
78
+ end
79
+ end
80
+
81
+ def path_match(path)
82
+ custom_require_path.each do |k, v|
83
+ return File.join(v, path) unless (Regexp.new(k.to_s) =~ path).nil?
84
+ end
85
+
86
+ false
87
+ end
88
+
89
+ def custom_require_path
90
+ @custom_require_path ||= options[:require_paths]
91
+ end
92
+ end
@@ -0,0 +1,140 @@
1
+ require_relative '../lib/sass-buddy.rb'
2
+ require 'tmpdir'
3
+ require 'pry'
4
+
5
+ RSpec.describe SassBuddy do
6
+ it 'compiles scss with given headers' do
7
+ styles = Tempfile.new("style.scss")
8
+ begin
9
+ styles.write(".hi { color: $var1; }")
10
+ styles.rewind
11
+
12
+ subject = described_class.new({
13
+ file: styles.path,
14
+ additions: {
15
+ "var1": "14a2d4"
16
+ }
17
+ })
18
+
19
+ expect(subject.compile).to eq(".hi {\n color: #14a2d4; }\n")
20
+ ensure
21
+ styles.close
22
+ styles.unlink
23
+ end
24
+ end
25
+
26
+ it 'compiles scss from obscure paths' do
27
+ begin
28
+ tmp_dir = File.expand_path "#{Dir.pwd}/#{Time.now.to_i}#{rand(1000)}/"
29
+ FileUtils.mkdir_p tmp_dir
30
+
31
+ styles = Tempfile.new("style.scss")
32
+ external = Tempfile.new(['external', '.scss'], tmp_dir)
33
+ FileUtils.mv external.path, "#{tmp_dir}/external.scss"
34
+
35
+ styles.write("@import 'external'")
36
+ external.write(".hi { color: $var1; }")
37
+
38
+ external.rewind
39
+ styles.rewind
40
+
41
+ subject = described_class.new({
42
+ file: styles.path,
43
+ additions: {
44
+ "var1": "14a2d4"
45
+ },
46
+ require_paths: {
47
+ "external": tmp_dir + '/'
48
+ }
49
+ })
50
+
51
+ expect(subject.compile).to eq(".hi {\n color: #14a2d4; }\n")
52
+ ensure
53
+ styles.close
54
+ styles.unlink
55
+ external.close
56
+ external.unlink
57
+ FileUtils.rm_rf( tmp_dir ) if File.exists?( tmp_dir )
58
+ end
59
+ end
60
+
61
+ it 'revves according to a manifest' do
62
+ styles = Tempfile.new("style.scss")
63
+ manifest = Tempfile.new("manifest.json")
64
+ begin
65
+ manifest.write("{\"images/fart.jpg\":\"images/fart-1234.jpg\"}")
66
+ manifest.rewind
67
+ styles.write(".hi { color: $var1; background-image: image-url('images/fart.jpg'); }")
68
+ styles.rewind
69
+
70
+ subject = described_class.new({
71
+ file: styles.path,
72
+ additions: {
73
+ "var1": "14a2d4"
74
+ },
75
+ rev_manifest: manifest.path
76
+ })
77
+
78
+ expect(subject.compile).to eq(".hi {\n color: #14a2d4;\n background-image: image-url(\"images/fart-1234.jpg\"); }\n")
79
+ ensure
80
+ styles.close
81
+ styles.unlink
82
+ manifest.close
83
+ manifest.unlink
84
+ end
85
+ end
86
+
87
+ it 'compiles strings and contains them' do
88
+ string = 'h1 { color: blue }'
89
+ container = '#content'
90
+
91
+ subject = described_class.new({
92
+ string: string,
93
+ container: container
94
+ })
95
+
96
+ expect(subject.compile).to eq("#content h1 {\n color: blue; }\n")
97
+ end
98
+
99
+ it 'creates html tags' do
100
+ string = 'h1 { color: blue }'
101
+ container = '#content'
102
+
103
+ subject = described_class.new({
104
+ string: string,
105
+ container: container
106
+ })
107
+
108
+ expect(subject.compile_html).to eq("<style>#content h1 {\n color: blue; }\n</style>")
109
+ end
110
+
111
+ it 'compiles sass' do
112
+ string = "$color: red
113
+
114
+ =my-border($color)
115
+ border: 1px solid $color
116
+
117
+ body
118
+ background: $color
119
+ +my-border(green)"
120
+
121
+ subject = described_class.new({
122
+ string: string,
123
+ sass: true
124
+ })
125
+
126
+ expect(subject.compile).to eq("body {\n background: red;\n border: 1px solid green; }\n")
127
+ end
128
+
129
+ it 'returns false for bad css' do
130
+ string = 'h1 { color: blue } adi8q3d9n'
131
+ container = '#content'
132
+
133
+ subject = described_class.new({
134
+ string: string,
135
+ container: container
136
+ })
137
+
138
+ expect(subject.compile_html).to eq(nil)
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sass-buddy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jesse Abramson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sassc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: SassBuddy is a Sass compiler that uses the SassC engine to dynamically
42
+ assemble css
43
+ email: jesse@influitive.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/sass-buddy.rb
49
+ - spec/sass-buddy-spec.rb
50
+ homepage:
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.4.8
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: SassBuddy is a Sass compiler that uses the SassC engine to dynamically assemble
74
+ css
75
+ test_files: []