jekyll-sass-converter 1.5.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a89449e32d89721639e197984bc0ff57632a816ca8649d787d07c0b8b7e94cc0
4
- data.tar.gz: 0b4d273ad2a4463e679f70971a742fdb779376d8f37bb1ace8f7596fa3a27431
3
+ metadata.gz: c051522099eb9c977d893591246ad37863d0ff38feec276b8992c9f16cfa6b3b
4
+ data.tar.gz: 388b71b1d56dcde5f45b9860b28ba932291951cf2385e42560032b3c4e144c5f
5
5
  SHA512:
6
- metadata.gz: 3268e9b3fce2074122f16fc64882b75048847930ae7329d42268bfe7e519d92b2727b4564f6c587c95922075ca5cc0d2d39287ac8abbdad5f70f7b5b16431b78
7
- data.tar.gz: bfbf9cf8fc49ebfc788287ef5e5abd810c9cbf26634d96d43876375ac3ddd0923e5c381f7bed4d4a9cedb616d86e46766c7483bfb67b343a8c38e6731bfcc6da
6
+ metadata.gz: 0b2c4cd74ba4ee2612c92394011d98f6db389078d3668ccebe3cf96ef855c5a1a72c9dbe47057f3cc586b1dac2937379ee02356e289fdbf96869ad860e2ad1c3
7
+ data.tar.gz: 275e76080d121f8693b42d1ca46088a0328013b111c8eddc4a998baafd04990ed9ff6cad5ac7246198c79bf63aad7e3ff556a2bbe9d6ffd0a0582dc259daec68
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllSassConverter
4
- VERSION = "1.5.2".freeze
4
+ VERSION = "2.0.0"
5
5
  end
@@ -1,19 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "sass"
3
+ require "sassc"
4
4
  require "jekyll/utils"
5
5
  require "jekyll/converters/scss"
6
6
 
7
7
  module Jekyll
8
8
  module Converters
9
9
  class Sass < Scss
10
+ EXTENSION_PATTERN = %r!^\.sass$!i.freeze
11
+
10
12
  safe true
11
13
  priority :low
12
14
 
13
- def matches(ext)
14
- ext =~ %r!^\.sass$!i
15
- end
16
-
17
15
  def syntax
18
16
  :sass
19
17
  end
@@ -1,21 +1,71 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "sass"
3
+ require "sassc"
4
4
  require "jekyll/utils"
5
+ require "jekyll/source_map_page"
5
6
 
6
7
  module Jekyll
7
8
  module Converters
8
9
  class Scss < Converter
9
- BYTE_ORDER_MARK = %r!^\xEF\xBB\xBF!
10
+ BYTE_ORDER_MARK = %r!^\xEF\xBB\xBF!.freeze
11
+ EXTENSION_PATTERN = %r!^\.scss$!i.freeze
12
+
10
13
  SyntaxError = Class.new(ArgumentError)
11
14
 
12
15
  safe true
13
16
  priority :low
14
17
 
18
+ # This hook is triggered just before the method {#convert(content)} is executed, it
19
+ # associates the Scss (and Sass) converters with their respective sass_page objects.
20
+ Jekyll::Hooks.register :pages, :pre_render do |page|
21
+ page.converters.each do |converter|
22
+ converter.associate_page(page) if converter.is_a?(Jekyll::Converters::Scss)
23
+ end
24
+ end
25
+
26
+ # This hook is triggered just after the method {#convert(content)} has been executed, it
27
+ # dissociates the Scss (and Sass) converters with their respective sass_page objects.
28
+ Jekyll::Hooks.register :pages, :post_render do |page|
29
+ page.converters.each do |converter|
30
+ converter.dissociate_page(page) if converter.is_a?(Jekyll::Converters::Scss)
31
+ end
32
+ end
33
+
15
34
  ALLOWED_STYLES = %w(nested expanded compact compressed).freeze
16
35
 
36
+ # Associate this Converter with the "page" object that manages input and output files for
37
+ # this converter.
38
+ #
39
+ # Note: changing the associated sass_page during the live time of this Converter instance
40
+ # may result in inconsistent results.
41
+ #
42
+ # @param [Jekyll:Page] page The sass_page for which this object acts as converter.
43
+ def associate_page(page)
44
+ if @sass_page
45
+ Jekyll.logger.debug "Sass Converter:",
46
+ "sass_page re-assigned: #{@sass_page.name} to #{page.name}"
47
+ dissociate_page(page)
48
+ return
49
+ end
50
+ @sass_page = page
51
+ end
52
+
53
+ # Dissociate this Converter with the "page" object.
54
+ #
55
+ # @param [Jekyll:Page] page The sass_page for which this object has acted as a converter.
56
+ def dissociate_page(page)
57
+ unless page.equal?(@sass_page)
58
+ Jekyll.logger.debug "Sass Converter:",
59
+ "dissociating a page that was never associated #{page.name}"
60
+ end
61
+
62
+ @source_map_page = nil
63
+ @sass_page = nil
64
+ @site = nil
65
+ end
66
+
17
67
  def matches(ext)
18
- ext =~ %r!^\.scss$!i
68
+ ext =~ self.class::EXTENSION_PATTERN
19
69
  end
20
70
 
21
71
  def output_ext(_ext)
@@ -27,21 +77,18 @@ module Jekyll
27
77
  end
28
78
 
29
79
  def jekyll_sass_configuration
30
- options = @config["sass"] || {}
31
- unless options["style"].nil?
32
- options["style"] = options["style"].to_s.gsub(%r!\A:!, "").to_sym
80
+ @jekyll_sass_configuration ||= begin
81
+ options = @config["sass"] || {}
82
+ unless options["style"].nil?
83
+ options["style"] = options["style"].to_s.gsub(%r!\A:!, "").to_sym
84
+ end
85
+ options
33
86
  end
34
- options
35
87
  end
36
88
 
37
89
  def sass_build_configuration_options(overrides)
38
90
  if safe?
39
- {
40
- :load_paths => sass_load_paths,
41
- :syntax => syntax,
42
- :style => sass_style,
43
- :cache => false,
44
- }
91
+ overrides
45
92
  else
46
93
  Jekyll::Utils.symbolize_hash_keys(
47
94
  Jekyll::Utils.deep_merge_hashes(
@@ -58,6 +105,7 @@ module Jekyll
58
105
 
59
106
  def sass_dir
60
107
  return "_sass" if jekyll_sass_configuration["sass_dir"].to_s.empty?
108
+
61
109
  jekyll_sass_configuration["sass_dir"]
62
110
  end
63
111
 
@@ -74,8 +122,10 @@ module Jekyll
74
122
  Jekyll.sanitized_path(site_source, sass_dir)
75
123
  end
76
124
 
125
+ # rubocop:disable Metrics/AbcSize
77
126
  def sass_load_paths
78
127
  paths = user_sass_load_paths + [sass_dir_relative_to_site_source]
128
+ paths << site.theme.sass_path if site.theme&.sass_path
79
129
 
80
130
  if safe?
81
131
  # Sanitize paths to prevent any attack vectors (.e.g. `/**/*`)
@@ -84,7 +134,7 @@ module Jekyll
84
134
 
85
135
  # Expand file globs (e.g. `node_modules/*/node_modules` )
86
136
  Dir.chdir(site_source) do
87
- paths = paths.map { |path| Dir.glob(path) }.flatten.uniq
137
+ paths = paths.flat_map { |path| Dir.glob(path) }.uniq
88
138
 
89
139
  paths.map! do |path|
90
140
  if safe?
@@ -98,6 +148,7 @@ module Jekyll
98
148
 
99
149
  paths.select { |path| File.directory?(path) }
100
150
  end
151
+ # rubocop:enable Metrics/AbcSize
101
152
 
102
153
  def allow_caching?
103
154
  !safe?
@@ -108,24 +159,125 @@ module Jekyll
108
159
  end
109
160
 
110
161
  def sass_configs
111
- sass_build_configuration_options({
112
- "syntax" => syntax,
113
- "cache" => allow_caching?,
114
- "load_paths" => sass_load_paths,
115
- })
162
+ sass_build_configuration_options(
163
+ :style => sass_style,
164
+ :syntax => syntax,
165
+ :filename => filename,
166
+ :output_path => output_path,
167
+ :source_map_file => source_map_file,
168
+ :load_paths => sass_load_paths,
169
+ :omit_source_map_url => !sourcemap_required?,
170
+ :source_map_contents => true,
171
+ :line_comments_option => line_comments_option
172
+ )
116
173
  end
117
174
 
118
175
  def convert(content)
119
- output = ::Sass.compile(content, sass_configs)
176
+ config = sass_configs
177
+ engine = SassC::Engine.new(content.dup, config)
178
+ output = engine.render
179
+ generate_source_map(engine) if sourcemap_required?
120
180
  replacement = add_charset? ? '@charset "UTF-8";' : ""
121
181
  output.sub(BYTE_ORDER_MARK, replacement)
122
- rescue ::Sass::SyntaxError => e
123
- raise SyntaxError, "#{e} on line #{e.sass_line}"
182
+ rescue SassC::SyntaxError => e
183
+ raise SyntaxError, e.to_s
124
184
  end
125
185
 
126
186
  private
187
+
188
+ # The Page instance for which this object acts as a converter.
189
+ attr_reader :sass_page
190
+
191
+ def associate_page_failed?
192
+ !sass_page
193
+ end
194
+
195
+ # The name of the input scss (or sass) file. This information will be used for error
196
+ # reporting and will written into the source map file as main source.
197
+ #
198
+ # Returns the name of the input file or "stdin" if #associate_page failed
199
+ def filename
200
+ return "stdin" if associate_page_failed?
201
+
202
+ sass_page.name
203
+ end
204
+
205
+ # The value of the `line_comments` option.
206
+ # When set to `true` causes the line number and filename of the source be emitted into the
207
+ # compiled CSS-file. Useful for debugging when the source-map is not available.
208
+ #
209
+ # Returns the value of the `line_comments`-option chosen by the user or 'false' by default.
210
+ def line_comments_option
211
+ jekyll_sass_configuration.fetch("line_comments", false)
212
+ end
213
+
214
+ # The value of the `sourcemap` option chosen by the user.
215
+ #
216
+ # This option controls when sourcemaps shall be generated or not.
217
+ #
218
+ # Returns the value of the `sourcemap`-option chosen by the user or ':always' by default.
219
+ def sourcemap_option
220
+ jekyll_sass_configuration.fetch("sourcemap", :always).to_sym
221
+ end
222
+
223
+ # Determines whether a sourcemap shall be generated or not.
224
+ #
225
+ # Returns `true` if a sourcemap shall be generated, `false` otherwise.
226
+ def sourcemap_required?
227
+ return false if associate_page_failed? || sourcemap_option == :never
228
+ return true if sourcemap_option == :always
229
+
230
+ !(sourcemap_option == :development && Jekyll.env != "development")
231
+ end
232
+
233
+ # The name of the generated css file. This information will be written into the source map
234
+ # file as a backward reference to the input.
235
+ #
236
+ # Returns the name of the css file or "stdin.css" if #associate_page failed
237
+ def output_path
238
+ return "stdin.css" if associate_page_failed?
239
+
240
+ sass_page.basename + ".css"
241
+ end
242
+
243
+ # The name of the generated source map file. This information will be written into the
244
+ # css file to reference to the source map.
245
+ #
246
+ # Returns the name of the css file or "" if #associate_page failed
247
+ def source_map_file
248
+ return "" if associate_page_failed?
249
+
250
+ sass_page.basename + ".css.map"
251
+ end
252
+
253
+ def source_map_page
254
+ return if associate_page_failed?
255
+
256
+ @source_map_page ||= SourceMapPage.new(sass_page)
257
+ end
258
+
259
+ # Reads the source-map from the engine and adds it to the source-map-page.
260
+ #
261
+ # @param [::SassC::Engine] engine The sass Compiler engine.
262
+ def generate_source_map(engine)
263
+ return if associate_page_failed?
264
+
265
+ source_map_page.source_map(engine.source_map)
266
+ site.pages << source_map_page
267
+ rescue ::SassC::NotRenderedError => e
268
+ Jekyll.logger.warn "Could not generate source map #{e.message} => #{e.cause}"
269
+ end
270
+
271
+ def site
272
+ if associate_page_failed?
273
+ Jekyll.sites.last
274
+ else
275
+ sass_page.site
276
+ end
277
+ end
278
+
127
279
  def site_source
128
- @site_source ||= File.expand_path(@config["source"]).freeze
280
+ site.source
129
281
  end
130
282
  end
131
283
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ # A Jekyll::Page subclass to manage the source map file associated with
5
+ # a given scss / sass page.
6
+ class SourceMapPage < Page
7
+ # Initialize a new SourceMapPage.
8
+ #
9
+ # @param [Jekyll::Page] css_page The Page object that manages the css file.
10
+ def initialize(css_page)
11
+ @site = css_page.site
12
+ @dir = css_page.dir
13
+ @data = css_page.data
14
+ @name = css_page.basename + ".css.map"
15
+
16
+ process(@name)
17
+ Jekyll::Hooks.trigger :pages, :post_init, self
18
+ end
19
+
20
+ def source_map(map)
21
+ self.content = map
22
+ end
23
+
24
+ def ext
25
+ ".map"
26
+ end
27
+
28
+ def asset_file?
29
+ true
30
+ end
31
+
32
+ # @return[String] the object as a debug String.
33
+ def inspect
34
+ "#<#{self.class} @name=#{name.inspect}>"
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,57 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-sass-converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Moore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-03 00:00:00.000000000 Z
11
+ date: 2019-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: sass
14
+ name: sassc
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.1
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: '3.4'
22
+ version: '3.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3.4'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
27
+ - - ">"
32
28
  - !ruby/object:Gem::Version
33
- version: '1.5'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
29
+ version: 2.0.1
30
+ - - "<"
39
31
  - !ruby/object:Gem::Version
40
- version: '1.5'
32
+ version: '3.0'
41
33
  - !ruby/object:Gem::Dependency
42
- name: jekyll
34
+ name: bundler
43
35
  requirement: !ruby/object:Gem::Requirement
44
36
  requirements:
45
37
  - - ">="
46
38
  - !ruby/object:Gem::Version
47
- version: '2.0'
39
+ version: '0'
48
40
  type: :development
49
41
  prerelease: false
50
42
  version_requirements: !ruby/object:Gem::Requirement
51
43
  requirements:
52
44
  - - ">="
53
45
  - !ruby/object:Gem::Version
54
- version: '2.0'
46
+ version: '0'
55
47
  - !ruby/object:Gem::Dependency
56
48
  name: rake
57
49
  requirement: !ruby/object:Gem::Requirement
@@ -81,19 +73,19 @@ dependencies:
81
73
  - !ruby/object:Gem::Version
82
74
  version: '0'
83
75
  - !ruby/object:Gem::Dependency
84
- name: rubocop
76
+ name: rubocop-jekyll
85
77
  requirement: !ruby/object:Gem::Requirement
86
78
  requirements:
87
- - - '='
79
+ - - "~>"
88
80
  - !ruby/object:Gem::Version
89
- version: '0.51'
81
+ version: '0.4'
90
82
  type: :development
91
83
  prerelease: false
92
84
  version_requirements: !ruby/object:Gem::Requirement
93
85
  requirements:
94
- - - '='
86
+ - - "~>"
95
87
  - !ruby/object:Gem::Version
96
- version: '0.51'
88
+ version: '0.4'
97
89
  description:
98
90
  email:
99
91
  - parkrmoore@gmail.com
@@ -105,6 +97,7 @@ files:
105
97
  - lib/jekyll-sass-converter/version.rb
106
98
  - lib/jekyll/converters/sass.rb
107
99
  - lib/jekyll/converters/scss.rb
100
+ - lib/jekyll/source_map_page.rb
108
101
  homepage: https://github.com/jekyll/jekyll-sass-converter
109
102
  licenses:
110
103
  - MIT
@@ -117,15 +110,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
110
  requirements:
118
111
  - - ">="
119
112
  - !ruby/object:Gem::Version
120
- version: '0'
113
+ version: 2.4.0
121
114
  required_rubygems_version: !ruby/object:Gem::Requirement
122
115
  requirements:
123
116
  - - ">="
124
117
  - !ruby/object:Gem::Version
125
118
  version: '0'
126
119
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.7.4
120
+ rubygems_version: 3.0.4
129
121
  signing_key:
130
122
  specification_version: 4
131
123
  summary: A basic Sass converter for Jekyll.