Class: Jekyll::Converters::Scss

Inherits:
Converter
  • Object
show all
Defined in:
lib/jekyll/converters/scss.rb

Direct Known Subclasses

Sass

Constant Summary collapse

BYTE_ORDER_MARK =
%r!^\xEF\xBB\xBF!
SyntaxError =
Class.new(ArgumentError)
ALLOWED_STYLES =
%w(nested expanded compact compressed).freeze

Instance Method Summary collapse

Instance Method Details

#add_charset?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/jekyll/converters/scss.rb', line 106

def add_charset?
  !!jekyll_sass_configuration["add_charset"]
end

#allow_caching?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/jekyll/converters/scss.rb', line 102

def allow_caching?
  !safe?
end

#convert(content) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/jekyll/converters/scss.rb', line 118

def convert(content)
  output = ::Sass.compile(content, sass_configs)
  replacement = add_charset? ? '@charset "UTF-8";' : ""
  output.sub(BYTE_ORDER_MARK, replacement)
rescue ::Sass::SyntaxError => e
  raise SyntaxError, "#{e} on line #{e.sass_line}"
end

#jekyll_sass_configurationObject



29
30
31
32
33
34
35
# File 'lib/jekyll/converters/scss.rb', line 29

def jekyll_sass_configuration
  options = @config["sass"] || {}
  unless options["style"].nil?
    options["style"] = options["style"].to_s.gsub(%r!\A:!, "").to_sym
  end
  options
end

#matches(ext) ⇒ Object



17
18
19
# File 'lib/jekyll/converters/scss.rb', line 17

def matches(ext)
  ext =~ %r!^\.scss$!i
end

#output_ext(_ext) ⇒ Object



21
22
23
# File 'lib/jekyll/converters/scss.rb', line 21

def output_ext(_ext)
  ".css"
end

#safe?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/jekyll/converters/scss.rb', line 25

def safe?
  !!@config["safe"]
end

#sass_build_configuration_options(overrides) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll/converters/scss.rb', line 37

def sass_build_configuration_options(overrides)
  if safe?
    {
      :load_paths => sass_load_paths,
      :syntax     => syntax,
      :style      => sass_style,
      :cache      => false,
    }
  else
    Jekyll::Utils.symbolize_hash_keys(
      Jekyll::Utils.deep_merge_hashes(
        jekyll_sass_configuration,
        overrides
      )
    )
  end
end

#sass_configsObject



110
111
112
113
114
115
116
# File 'lib/jekyll/converters/scss.rb', line 110

def sass_configs
  sass_build_configuration_options({
    "syntax"     => syntax,
    "cache"      => allow_caching?,
    "load_paths" => sass_load_paths,
  })
end

#sass_dirObject



59
60
61
62
# File 'lib/jekyll/converters/scss.rb', line 59

def sass_dir
  return "_sass" if jekyll_sass_configuration["sass_dir"].to_s.empty?
  jekyll_sass_configuration["sass_dir"]
end

#sass_dir_relative_to_site_sourceObject



73
74
75
# File 'lib/jekyll/converters/scss.rb', line 73

def sass_dir_relative_to_site_source
  Jekyll.sanitized_path(site_source, sass_dir)
end

#sass_load_pathsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jekyll/converters/scss.rb', line 77

def sass_load_paths
  paths = user_sass_load_paths + [sass_dir_relative_to_site_source]

  if safe?
    # Sanitize paths to prevent any attack vectors (.e.g. `/**/*`)
    paths.map! { |path| Jekyll.sanitized_path(site_source, path) }
  end

  # Expand file globs (e.g. `node_modules/*/node_modules` )
  Dir.chdir(site_source) do
    paths = paths.map { |path| Dir.glob(path) }.flatten.uniq

    paths.map! do |path|
      if safe?
        # Sanitize again in case globbing was able to do something crazy.
        Jekyll.sanitized_path(site_source, path)
      else
        File.expand_path(path)
      end
    end
  end

  paths.select { |path| File.directory?(path) }
end

#sass_styleObject



64
65
66
67
# File 'lib/jekyll/converters/scss.rb', line 64

def sass_style
  style = jekyll_sass_configuration.fetch("style", :compact)
  ALLOWED_STYLES.include?(style.to_s) ? style.to_sym : :compact
end

#syntaxObject



55
56
57
# File 'lib/jekyll/converters/scss.rb', line 55

def syntax
  :scss
end

#user_sass_load_pathsObject



69
70
71
# File 'lib/jekyll/converters/scss.rb', line 69

def user_sass_load_paths
  Array(jekyll_sass_configuration["load_paths"])
end