glimmer-dsl-css 1.2.3 → 1.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 967caae689a729dcf6c70d97a761bc48da8e8d285cd19348aada3dee83ffc32a
4
- data.tar.gz: 8f896b942182779e573bed0a4c039efbabe724b40b3d82dff6ded1d5d0ebe8fd
3
+ metadata.gz: 63ac41960245ceacb5eaec363a41bf8f80e56bdea6e150f68a16266b88b96937
4
+ data.tar.gz: 40ead61962a906cd7ecf1703afcb5b0998b369416e026e17a0b2cebe3aa770f8
5
5
  SHA512:
6
- metadata.gz: b5faae751daf2f5e6c608682fd8c035993f4fef13ce7fa2c8ff21100b30f09338939cb7d3e9b33f225632ee203a5f858610c1992bf14954dc1e0efc1b135dbf6
7
- data.tar.gz: '018c7875e4fcbd9c32012f0a489c5524de5cd76be406e1349ce0665204f5f4dac27e27ad0e98e30924816467f2c3ba66aa768533adbb26fa68f5ba506c0b8def'
6
+ metadata.gz: 07ef93287f2769c303d1159d451468d33d5c6cff4aa81679fa5ff559bc733fbc834d26daf909a7db5f03928b9ced56efef39f34b83d2ffd65a7b5fbb219918ce
7
+ data.tar.gz: e9fee33a86f2ba56bc8e9f43071a437e440a0b68f1e9f3bd0755f4497138188118e7d7812860c2f7590f59b23008e2b473dec3ac49746fcb27b10d8becaac51b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.4.0
4
+
5
+ - `css_to_glimmer` converter command for automatically converting CSS to Glimmer DSL Ruby code
6
+ - `css_to_glimmer` -r=rule_keyword option to customize rule keyword (rule has aliases: rul, ru, r, s, _)
7
+ - `minifier` converter command for automatically minifying CSS
8
+ - Fix issue with `display` CSS property not getting interpreted successfully in Glimmer DSL Ruby code
9
+
10
+ ## 1.3.0
11
+
12
+ - Support media queries
13
+
3
14
  ## 1.2.3
4
15
 
5
16
  - Add `r`, `ru`, `rul`, `rule`, `_` alternatives to `s` for specifying a CSS rule selector with a block of expressions (e.g. `r('body > h1') { color: red; }`)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for CSS 1.2.3
1
+ # [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for CSS 1.4.0
2
2
  ## Ruby Programmable Cascading Style Sheets
3
3
  [![Gem Version](https://badge.fury.io/rb/glimmer-dsl-css.svg)](http://badge.fury.io/rb/glimmer-dsl-css)
4
4
  [![Travis CI](https://travis-ci.com/AndyObtiva/glimmer-dsl-css.svg?branch=master)](https://travis-ci.com/github/AndyObtiva/glimmer-dsl-css)
@@ -6,30 +6,41 @@
6
6
  [![Maintainability](https://api.codeclimate.com/v1/badges/c7365cdb8556be433115/maintainability)](https://codeclimate.com/github/AndyObtiva/glimmer-dsl-css/maintainability)
7
7
  [![Join the chat at https://gitter.im/AndyObtiva/glimmer](https://badges.gitter.im/AndyObtiva/glimmer.svg)](https://gitter.im/AndyObtiva/glimmer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
8
8
 
9
- [Glimmer](https://github.com/AndyObtiva/glimmer) DSL for CSS provides Ruby syntax for building CSS (Cascading Style Sheets). It used to be part of the [Glimmer](https://github.com/AndyObtiva/glimmer) library (created in 2007), but eventually got extracted into its own project.
9
+ [Glimmer](https://github.com/AndyObtiva/glimmer) DSL for CSS provides Ruby syntax for building CSS (Cascading Style Sheets). It used to be part of the [Glimmer](https://github.com/AndyObtiva/glimmer) library (created in 2007), but eventually got extracted into its own project. The Ruby gem also includes a [CSS to Glimmer converter](#css-to-glimmer-converter) (`css_to_glimmer`) to automatically convert legacy CSS code into Glimmer DSL syntax.
10
10
 
11
11
  Example (you can try in IRB):
12
12
 
13
13
  ```ruby
14
14
  require 'glimmer-dsl-css'
15
+
15
16
  include Glimmer
17
+
16
18
  @css = css {
17
19
  body {
18
20
  font_size '1.1em'
19
21
  background 'white'
20
22
  }
21
- r('body > h1') {
23
+
24
+ rule('body > h1') {
22
25
  background_color :red
23
26
  font_size 24
24
27
  }
28
+
29
+ media('screen and (min-width: 30em) and (orientation: landscape)') {
30
+ rule('body#app h1#title') {
31
+ font_size 16
32
+ font_family '"Times New Roman", Times, serif'
33
+ }
34
+ }
25
35
  }
36
+
26
37
  puts @css
27
38
  ```
28
39
 
29
40
  Output (minified CSS):
30
41
 
31
42
  ```css
32
- body{font-size:1.1em;background:white}body > h1{background-color:red;font-size:24px}
43
+ body{font-size:1.1em;background:white}body > h1{background-color:red;font-size:24px}@media screen and (min-width: 30em) and (orientation: landscape){body#app h1#title{font-size:16px;font-family:"Times New Roman", Times, serif}}
33
44
  ```
34
45
 
35
46
  The key reason for using the CSS DSL instead of actual CSS is Ruby programmability without getting lost in string concatenations. The CSS DSL helps in including conditional CSS with `if` or ternery expressions as well as looping from lists while building CSS.
@@ -53,7 +64,7 @@ Please follow these instructions to make the `glimmer` command available on your
53
64
 
54
65
  Run this command to install directly:
55
66
  ```
56
- gem install glimmer-dsl-css -v 1.2.3
67
+ gem install glimmer-dsl-css -v 1.4.0
57
68
  ```
58
69
 
59
70
  Note: In case you are using JRuby, `jgem` is JRuby's version of the `gem` command. RVM allows running `gem` as an alias in JRuby. Otherwise, you may also run `jruby -S gem install ...`
@@ -68,7 +79,7 @@ That's it! Requiring the gem activates the Glimmer CSS DSL automatically.
68
79
 
69
80
  Add the following to `Gemfile` (after `glimmer-dsl-swt` and/or `glimmer-dsl-opal` if included too):
70
81
  ```
71
- gem 'glimmer-dsl-css', '~> 1.2.3'
82
+ gem 'glimmer-dsl-css', '~> 1.4.0'
72
83
  ```
73
84
 
74
85
  And, then run:
@@ -107,17 +118,21 @@ Example (you can try in IRB):
107
118
 
108
119
  ```ruby
109
120
  require 'glimmer-dsl-css'
121
+
110
122
  include Glimmer
123
+
111
124
  @css = css {
112
125
  body {
113
126
  font_size '1.1em'
114
127
  background 'white'
115
128
  }
129
+
116
130
  r('body > h1') {
117
131
  background_color :red
118
132
  font_size 24
119
133
  }
120
134
  }
135
+
121
136
  puts @css
122
137
  ```
123
138
 
@@ -163,7 +178,9 @@ As you saw above, numeric values (e.g. `24` in `font_size 24`) automatically get
163
178
 
164
179
  ```ruby
165
180
  require 'glimmer-dsl-css'
181
+
166
182
  include Glimmer
183
+
167
184
  @css = css {
168
185
  body {
169
186
  font_size '1.1em'
@@ -174,6 +191,7 @@ include Glimmer
174
191
  font_size 24
175
192
  end
176
193
  }
194
+
177
195
  puts @css
178
196
  ```
179
197
 
@@ -183,6 +201,93 @@ Output (minified CSS):
183
201
  body{font-size:1.1em;background:white}body > h1{background-color:red;font-size:24px}
184
202
  ```
185
203
 
204
+ ### CSS to Glimmer Converter
205
+
206
+ The Ruby gem includes a CSS to Glimmer converter (`css_to_glimmer`) to automatically convert legacy CSS code into Glimmer DSL syntax.
207
+
208
+ Script:
209
+
210
+ [bin/css_to_glimmer](/bin/css_to_glimmer)
211
+
212
+ Usage:
213
+
214
+ ```
215
+ css_to_glimmer [-r=rule_keyword] path_to_css_file
216
+ ```
217
+
218
+ Example:
219
+
220
+ ```
221
+ css_to_glimmer input.css
222
+ ```
223
+
224
+ Prints:
225
+
226
+ ```
227
+ Converting from CSS syntax to Glimmer DSL Ruby syntax for input file: input.css
228
+ Converted output file: input.css.glimmer.rb
229
+ ```
230
+
231
+ Output file (`input.css.glimmer.rb`) is a runnable Ruby file containing Glimmer DSL for CSS syntax:
232
+
233
+ ```
234
+ require 'glimmer-dsl-css'
235
+
236
+ include Glimmer
237
+
238
+ style_sheet = css {
239
+ rule('html,body') {
240
+ margin '0'
241
+ padding '0'
242
+ }
243
+
244
+ media('(max-width: 430px)') {
245
+ rule('.footer') {
246
+ height '50px'
247
+ }
248
+
249
+ rule('.filters') {
250
+ bottom '10px'
251
+ }
252
+ }
253
+ }
254
+
255
+ puts style_sheet.to_s
256
+ ```
257
+
258
+ If you would rather customize the `rule` keyword with a shorter alias like `rul`, `ru`, `r`, `s` (for selector), or `_`, you can run the following command:
259
+
260
+ ```
261
+ css_to_glimmer -r=ru input.css
262
+ ```
263
+
264
+ Output file (`input.css.glimmer.rb`) is a runnable Ruby file containing Glimmer DSL for CSS syntax:
265
+
266
+ ```
267
+ require 'glimmer-dsl-css'
268
+
269
+ include Glimmer
270
+
271
+ style_sheet = css {
272
+ ru('html,body') {
273
+ margin '0'
274
+ padding '0'
275
+ }
276
+
277
+ media('(max-width: 430px)') {
278
+ ru('.footer') {
279
+ height '50px'
280
+ }
281
+
282
+ ru('.filters') {
283
+ bottom '10px'
284
+ }
285
+ }
286
+ }
287
+
288
+ puts style_sheet.to_s
289
+ ```
290
+
186
291
  ## Multi-DSL Support
187
292
 
188
293
  Learn more about how to use this DSL alongside other Glimmer DSLs:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
1
+ 1.4.0
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/glimmer/css/css_to_glimmer_converter'
4
+
5
+ usage = "***Glimmer DSL for CSS - CSS to Glimmer Converter***\nUsage: css_to_glimmer path_to_css_file"
6
+
7
+ if ARGV.empty?
8
+ puts usage
9
+ else
10
+ css_file_path = ARGV.last
11
+ puts "Converting from CSS syntax to Glimmer DSL Ruby syntax for input file: #{css_file_path}"
12
+ css = File.read(css_file_path)
13
+ rule_keyword = ARGV.find { |arg| arg.start_with?("-r=") }.to_s.split('=')[1].to_s
14
+ converter = rule_keyword.empty? ? Glimmer::CSS::CSSToGlimmerConverter.new : Glimmer::CSS::CSSToGlimmerConverter.new(rule_keyword: rule_keyword)
15
+ glimmer = converter.convert(css)
16
+ glimmer_file_path = "#{css_file_path}.glimmer.rb"
17
+ File.write(glimmer_file_path, glimmer)
18
+ puts "Converted output file: #{glimmer_file_path}"
19
+ end
data/bin/minify_css ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/glimmer/css/css_minifier'
4
+
5
+ usage = "***Glimmer DSL for CSS - CSS Minifier***\nUsage: minify_css path_to_css_file"
6
+
7
+ # TODO support option to select rule, or rul, or r
8
+
9
+ if ARGV.empty?
10
+ puts usage
11
+ else
12
+ css_file_path = ARGV.first
13
+ puts "Minifying CSS for input file: #{css_file_path}"
14
+ css = File.read(css_file_path)
15
+ converter = Glimmer::CSS::CSSMinifier.new
16
+ minified_css = converter.convert(css)
17
+ minified_css_file_path = "#{File.basename(css_file_path, '.css')}.minified.css"
18
+ File.write(minified_css_file_path, minified_css)
19
+ puts "Converted output file: #{minified_css_file_path}"
20
+ end
@@ -2,18 +2,19 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: glimmer-dsl-css 1.2.3 ruby lib
5
+ # stub: glimmer-dsl-css 1.4.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "glimmer-dsl-css".freeze
9
- s.version = "1.2.3".freeze
9
+ s.version = "1.4.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["AndyMaleh".freeze]
14
- s.date = "2024-01-08"
14
+ s.date = "2024-05-18"
15
15
  s.description = "Glimmer DSL for CSS (Ruby Programmable Cascading Style Sheets)".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
+ s.executables = ["css_to_glimmer".freeze, "minify_css".freeze]
17
18
  s.extra_rdoc_files = [
18
19
  "CHANGELOG.md",
19
20
  "LICENSE.txt",
@@ -25,15 +26,23 @@ Gem::Specification.new do |s|
25
26
  "LICENSE.txt",
26
27
  "README.md",
27
28
  "VERSION",
29
+ "bin/css_to_glimmer",
30
+ "bin/minify_css",
28
31
  "glimmer-dsl-css.gemspec",
29
32
  "lib/glimmer-dsl-css.rb",
33
+ "lib/glimmer/css/css_minifier.rb",
34
+ "lib/glimmer/css/css_to_glimmer_converter.rb",
35
+ "lib/glimmer/css/media_query.rb",
30
36
  "lib/glimmer/css/rule.rb",
37
+ "lib/glimmer/css/rule_composite.rb",
31
38
  "lib/glimmer/css/style_sheet.rb",
32
39
  "lib/glimmer/dsl/css/css_expression.rb",
40
+ "lib/glimmer/dsl/css/display_expression.rb",
33
41
  "lib/glimmer/dsl/css/dsl.rb",
34
42
  "lib/glimmer/dsl/css/dynamic_property_expression.rb",
35
43
  "lib/glimmer/dsl/css/element_rule_expression.rb",
36
44
  "lib/glimmer/dsl/css/general_rule_expression.rb",
45
+ "lib/glimmer/dsl/css/media_expression.rb",
37
46
  "lib/glimmer/dsl/css/property_expression.rb",
38
47
  "lib/glimmer/dsl/css/pv_expression.rb",
39
48
  "lib/glimmer/dsl/css/ru_expression.rb",
@@ -43,21 +52,22 @@ Gem::Specification.new do |s|
43
52
  ]
44
53
  s.homepage = "http://github.com/AndyObtiva/glimmer-dsl-css".freeze
45
54
  s.licenses = ["MIT".freeze]
46
- s.rubygems_version = "3.5.3".freeze
55
+ s.rubygems_version = "3.4.10".freeze
47
56
  s.summary = "Glimmer DSL for CSS (Ruby Programmable Cascading Style Sheets)".freeze
48
57
 
49
58
  s.specification_version = 4
50
59
 
51
- s.add_runtime_dependency(%q<glimmer>.freeze, [">= 2.0.0".freeze, "< 3.0.0".freeze])
52
- s.add_development_dependency(%q<rspec-mocks>.freeze, ["~> 3.0".freeze])
53
- s.add_development_dependency(%q<rspec>.freeze, ["~> 3.0".freeze])
54
- s.add_development_dependency(%q<puts_debuggerer>.freeze, [">= 0".freeze])
55
- s.add_development_dependency(%q<rake>.freeze, [">= 10.1.0".freeze, "< 14.0.0".freeze])
56
- s.add_development_dependency(%q<jeweler>.freeze, [">= 2.3.9".freeze, "< 3.0.0".freeze])
57
- s.add_development_dependency(%q<rdoc>.freeze, [">= 6.2.1".freeze, "< 7.0.0".freeze])
58
- s.add_development_dependency(%q<coveralls>.freeze, ["= 0.8.23".freeze])
59
- s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.16.1".freeze])
60
- s.add_development_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0".freeze])
61
- s.add_development_dependency(%q<rake-tui>.freeze, [">= 0".freeze])
60
+ s.add_runtime_dependency(%q<glimmer>.freeze, [">= 2.0.0", "< 3.0.0"])
61
+ s.add_runtime_dependency(%q<css_parser>.freeze, [">= 1.0.0", "< 2.0.0"])
62
+ s.add_development_dependency(%q<rspec-mocks>.freeze, ["~> 3.0"])
63
+ s.add_development_dependency(%q<rspec>.freeze, ["~> 3.0"])
64
+ s.add_development_dependency(%q<puts_debuggerer>.freeze, [">= 0"])
65
+ s.add_development_dependency(%q<rake>.freeze, [">= 10.1.0", "< 14.0.0"])
66
+ s.add_development_dependency(%q<jeweler>.freeze, [">= 2.3.9", "< 3.0.0"])
67
+ s.add_development_dependency(%q<rdoc>.freeze, [">= 6.2.1", "< 7.0.0"])
68
+ s.add_development_dependency(%q<coveralls>.freeze, ["= 0.8.23"])
69
+ s.add_development_dependency(%q<simplecov>.freeze, ["~> 0.16.1"])
70
+ s.add_development_dependency(%q<simplecov-lcov>.freeze, ["~> 0.7.0"])
71
+ s.add_development_dependency(%q<rake-tui>.freeze, [">= 0"])
62
72
  end
63
73
 
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2020-2024 - Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module Glimmer
23
+ module CSS
24
+ class CSSMinifier
25
+ def convert(css)
26
+ css.lines.map(&:chomp).map(&:strip).join
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,121 @@
1
+ # Copyright (c) 2020-2024 - Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'css_parser'
23
+ require_relative 'css_minifier'
24
+
25
+ module Glimmer
26
+ module CSS
27
+ class CSSToGlimmerConverter
28
+ include CssParser
29
+
30
+ RULE_KEYWORDS = %w[rule rul ru r s _]
31
+
32
+ # This option specifies if we want to use the default rule keyword or an alias like rul or r
33
+ attr_reader :rule_keyword
34
+
35
+ def initialize(rule_keyword: 'rule')
36
+ @rule_keyword = rule_keyword.to_s
37
+ validate_rule_keyword
38
+ end
39
+
40
+ def convert(css)
41
+ glimmer = ''
42
+ glimmer += "require 'glimmer-dsl-css'\n\n"
43
+ glimmer += "include Glimmer\n\n"
44
+ glimmer += "style_sheet = css {\n"
45
+ glimmer += convert_css(css).lines.map { |line| " #{line}" }.join
46
+ glimmer += "}\n\n"
47
+ glimmer += "puts style_sheet.to_s\n"
48
+ end
49
+
50
+ def minify(css)
51
+ CSSMinifier.new.convert(css)
52
+ end
53
+
54
+ def remove_charset(css)
55
+ css.sub(/@charset[^;]*;/, '')
56
+ end
57
+
58
+ def remove_comments(css)
59
+ output = ''
60
+ comment = false
61
+ current_two_characters = [];
62
+ css.chars.each do |character|
63
+ output << character if !comment
64
+ current_two_characters.shift if current_two_characters.size == 2
65
+ current_two_characters << character
66
+ if !comment && current_two_characters.join == '/*'
67
+ comment = true
68
+ output = output[0...-2]
69
+ elsif comment && current_two_characters.join == '*/'
70
+ comment = false
71
+ end
72
+ end
73
+ output
74
+ end
75
+
76
+ private
77
+
78
+ def convert_css(css)
79
+ parser = CssParser::Parser.new
80
+ parser.load_string!(css)
81
+ parser.rules_by_media_query.map do |media_query, rule_set|
82
+ if media_query == :all
83
+ convert_rule_set_to_glimmer(rule_set)
84
+ else
85
+ convert_media_query_rule_set_to_glimmer(rule_set, media_query)
86
+ end
87
+ end.join("\n")
88
+ end
89
+
90
+ def convert_rule_set_to_glimmer(rule_set)
91
+ glimmer = ''
92
+ rule_set.each_with_index do |rule, index|
93
+ glimmer += "\n" if index > 0
94
+ rule_selector = rule.selectors.join(', ').gsub("'", '"')
95
+ glimmer += "#{rule_keyword}('#{rule_selector}') {\n"
96
+ rule.each_declaration do |declaration, value|
97
+ property_name = declaration.gsub('-', '_')
98
+ property_value = value.sub(/^("|')/, '').sub(/("|')$/, '').gsub("'", '"')
99
+ glimmer += " #{property_name} '#{property_value}'\n"
100
+ end
101
+ glimmer += "}\n"
102
+ end
103
+ glimmer
104
+ end
105
+
106
+ def convert_media_query_rule_set_to_glimmer(rule_set, media_query)
107
+ glimmer = ''
108
+ glimmer += "media('#{media_query.to_s.gsub("'", '"')}') {\n"
109
+ glimmer += convert_rule_set_to_glimmer(rule_set).lines.map { |line| " #{line}" }.join
110
+ glimmer += "}\n"
111
+ glimmer
112
+ end
113
+
114
+ private
115
+
116
+ def validate_rule_keyword
117
+ raise "rule_keyword '#{rule_keyword}' is invalid! It must be 'rule', 'rul', 'ru', 'r', 's', or '_'" if !RULE_KEYWORDS.include?(rule_keyword)
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2020-2024 - Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/css/rule_composite'
23
+ require 'glimmer/css/rule'
24
+
25
+ module Glimmer
26
+ module CSS
27
+ class MediaQuery < Rule
28
+ include RuleComposite
29
+
30
+ def to_css
31
+ css = "@media #{selector}{"
32
+ css += rules.map(&:to_css).join
33
+ css += "}"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -24,9 +24,11 @@ module Glimmer
24
24
  class Rule
25
25
  attr_reader :selector, :properties
26
26
 
27
- def initialize(selector)
27
+ def initialize(selector, parent:)
28
28
  @selector = selector
29
29
  @properties = {}
30
+ @parent = parent
31
+ parent.rules << self
30
32
  end
31
33
 
32
34
  def add_property(keyword, *args)
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2020-2024 - Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module Glimmer
23
+ module CSS
24
+ module RuleComposite
25
+ def rules
26
+ @rules ||= []
27
+ end
28
+ end
29
+ end
30
+ end
@@ -19,16 +19,13 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
+ require 'glimmer/css/rule_composite'
22
23
  require 'glimmer/css/rule'
23
24
 
24
25
  module Glimmer
25
26
  module CSS
26
27
  class StyleSheet
27
- attr_reader :rules
28
-
29
- def initialize
30
- @rules = []
31
- end
28
+ include RuleComposite
32
29
 
33
30
  def to_css
34
31
  rules.map(&:to_css).join
@@ -0,0 +1,33 @@
1
+ # Copyright (c) 2020-2024 - Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/dsl/static_expression'
23
+ require 'glimmer/dsl/css/property_expression'
24
+
25
+ module Glimmer
26
+ module DSL
27
+ module CSS
28
+ class DisplayExpression < StaticExpression
29
+ include PropertyExpression
30
+ end
31
+ end
32
+ end
33
+ end
@@ -29,6 +29,8 @@ require 'glimmer/dsl/css/rul_expression'
29
29
  require 'glimmer/dsl/css/ru_expression'
30
30
  require 'glimmer/dsl/css/s_expression'
31
31
  require 'glimmer/dsl/css/pv_expression'
32
+ require 'glimmer/dsl/css/display_expression'
33
+ require 'glimmer/dsl/css/media_expression'
32
34
 
33
35
  module Glimmer
34
36
  module DSL
@@ -36,6 +38,7 @@ module Glimmer
36
38
  Engine.add_dynamic_expressions(
37
39
  CSS,
38
40
  %w[
41
+ media
39
42
  element_rule
40
43
  dynamic_property
41
44
  ]
@@ -37,9 +37,7 @@ module Glimmer
37
37
  end
38
38
 
39
39
  def interpret(parent, keyword, *args, &block)
40
- Glimmer::CSS::Rule.new(keyword.to_s.downcase).tap do |rule|
41
- parent.rules << rule
42
- end
40
+ Glimmer::CSS::Rule.new(keyword.to_s.downcase, parent: parent)
43
41
  end
44
42
  end
45
43
  end
@@ -31,15 +31,16 @@ module Glimmer
31
31
 
32
32
  def can_interpret?(parent, keyword, *args, &block)
33
33
  super(parent, keyword, *args, &block) and
34
- parent.is_a?(Glimmer::CSS::StyleSheet) and
34
+ (
35
+ parent.is_a?(Glimmer::CSS::StyleSheet) or
36
+ parent.is_a?(Glimmer::CSS::MediaQuery)
37
+ ) and
35
38
  block_given? and
36
39
  !args.empty?
37
40
  end
38
41
 
39
42
  def interpret(parent, keyword, *args, &block)
40
- Glimmer::CSS::Rule.new(args.first.to_s).tap do |rule|
41
- parent.rules << rule
42
- end
43
+ Glimmer::CSS::Rule.new(args.first.to_s, parent: parent)
43
44
  end
44
45
  end
45
46
  end
@@ -0,0 +1,47 @@
1
+ # Copyright (c) 2020-2024 - Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/dsl/parent_expression'
23
+ require 'glimmer/dsl/static_expression'
24
+
25
+ require 'glimmer/css/style_sheet'
26
+ require 'glimmer/css/media_query'
27
+
28
+ module Glimmer
29
+ module DSL
30
+ module CSS
31
+ class MediaExpression < StaticExpression
32
+ include ParentExpression
33
+
34
+ def can_interpret?(parent, keyword, *args, &block)
35
+ super(parent, keyword, *args, &block) and
36
+ parent.is_a?(Glimmer::CSS::StyleSheet) and
37
+ block_given? and
38
+ !args.empty?
39
+ end
40
+
41
+ def interpret(parent, keyword, *args, &block)
42
+ Glimmer::CSS::MediaQuery.new(args.first.to_s, parent: parent)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-css
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - AndyMaleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-08 00:00:00.000000000 Z
11
+ date: 2024-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -30,6 +30,26 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: css_parser
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.0.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: 2.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.0.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.0
33
53
  - !ruby/object:Gem::Dependency
34
54
  name: rspec-mocks
35
55
  requirement: !ruby/object:Gem::Requirement
@@ -190,7 +210,9 @@ dependencies:
190
210
  version: '0'
191
211
  description: Glimmer DSL for CSS (Ruby Programmable Cascading Style Sheets)
192
212
  email: andy.am@gmail.com
193
- executables: []
213
+ executables:
214
+ - css_to_glimmer
215
+ - minify_css
194
216
  extensions: []
195
217
  extra_rdoc_files:
196
218
  - CHANGELOG.md
@@ -202,15 +224,23 @@ files:
202
224
  - LICENSE.txt
203
225
  - README.md
204
226
  - VERSION
227
+ - bin/css_to_glimmer
228
+ - bin/minify_css
205
229
  - glimmer-dsl-css.gemspec
206
230
  - lib/glimmer-dsl-css.rb
231
+ - lib/glimmer/css/css_minifier.rb
232
+ - lib/glimmer/css/css_to_glimmer_converter.rb
233
+ - lib/glimmer/css/media_query.rb
207
234
  - lib/glimmer/css/rule.rb
235
+ - lib/glimmer/css/rule_composite.rb
208
236
  - lib/glimmer/css/style_sheet.rb
209
237
  - lib/glimmer/dsl/css/css_expression.rb
238
+ - lib/glimmer/dsl/css/display_expression.rb
210
239
  - lib/glimmer/dsl/css/dsl.rb
211
240
  - lib/glimmer/dsl/css/dynamic_property_expression.rb
212
241
  - lib/glimmer/dsl/css/element_rule_expression.rb
213
242
  - lib/glimmer/dsl/css/general_rule_expression.rb
243
+ - lib/glimmer/dsl/css/media_expression.rb
214
244
  - lib/glimmer/dsl/css/property_expression.rb
215
245
  - lib/glimmer/dsl/css/pv_expression.rb
216
246
  - lib/glimmer/dsl/css/ru_expression.rb
@@ -236,7 +266,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
266
  - !ruby/object:Gem::Version
237
267
  version: '0'
238
268
  requirements: []
239
- rubygems_version: 3.5.3
269
+ rubygems_version: 3.4.10
240
270
  signing_key:
241
271
  specification_version: 4
242
272
  summary: Glimmer DSL for CSS (Ruby Programmable Cascading Style Sheets)