Class: Scribo::SassC::Importer

Inherits:
SassC::Importer
  • Object
show all
Defined in:
lib/scribo/sassc/importer.rb

Instance Method Summary collapse

Instance Method Details

#imports(path, parent_path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/scribo/sassc/importer.rb', line 6

def imports(path, parent_path)
  content = options[:content]

  import_path = ''
  if path.start_with?('/')
    import_path += path
  else
    import_path += content.site.sass_dir
    import_path += '/' unless import_path.end_with?('/')

    import_path += path
    import_path += File.extname(content.path)
    import_path = File.expand_path(import_path, content.site.sass_dir)
    unless import_path.start_with?(content.site.sass_dir)
      # import_path always starts with /
      import_path = content.site.sass_dir + import_path[1..-1]
    end
  end

  include_content = content.site.contents.where(kind: 'text').located(import_path, restricted: false)
  unless include_content.present?
    # Look for /_file.ext alternative
    import_path = File.dirname(import_path) + '/_' + File.basename(import_path)
    include_content = content.site.contents.where(kind: 'text').located(import_path, restricted: false)
  end

  # Look in parent's folder
  if include_content.blank? && File.dirname(parent_path) != '.'
    import_path = content.site.sass_dir + File.dirname(parent_path) + '/' + File.basename(import_path)
    include_content = content.site.contents.where(kind: 'text').located(import_path, restricted: false)
  end

  if include_content.empty? && content.site.properties.value_at_keypath('sass.sass_dir')
    alternate_path = content.site.properties.value_at_keypath('sass.sass_dir')
    # alternate_path += '/' unless alternate_path.end_with?('/')
    alternate_path = '/' + alternate_path unless alternate_path.start_with?('/')

    import_path = File.expand_path(path, alternate_path)
    import_path += File.extname(content.path)

    include_content = content.site.contents.where(kind: 'text').located(import_path, restricted: false)

    unless include_content.present?
      # Look for /_file.ext alternative
      import_path = File.dirname(import_path) + '/_' + File.basename(import_path)
      include_content = content.site.contents.located(import_path, restricted: false)
    end

  end

  Scribo.config.logger.warn "SassC::Importer: No import found: #{import_path}" unless include_content.first
  # FIXME: Add context
  # Here we don't use a filter
  source = ContentRenderService.new(include_content.first, {}, filter: nil).call || ''
  ::SassC::Importer::Import.new(path, source: source)
end