Module: ThemeBandit::CSSParser

Includes:
URLFormatter
Included in:
DocumentWriter
Defined in:
lib/theme_bandit/parser/css.rb

Constant Summary collapse

RE_IMPORT =
/\@import\s*(?:url\s*)?(?:\()?(?:\s*)["']?([^'"\s\)]*)["']?\)?([\w\s\,^\]\(\))]*)\)?[;\n]?/

Instance Method Summary collapse

Methods included from URLFormatter

#cdn_to_fqd, #get_absolute_path, #path_with_leading_slash, #strip_query_string

Instance Method Details

#get_css_filesObject



7
8
9
# File 'lib/theme_bandit/parser/css.rb', line 7

def get_css_files
  link_tag_values
end

#get_import_urls(file, file_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/theme_bandit/parser/css.rb', line 33

def get_import_urls(file, file_name)
  imports = file.scan(RE_IMPORT).map(&:first).select { |import| import[/\.css/] }
  imports_arr = []
  imports.each do |import|
    dot_dots_length = import.split('/').select { |x| x[/\.\./] }.length
    if dot_dots_length > 0
      file_uri  = URI.parse(file_name)
      matcher   = file_uri.path.split('/').last(dot_dots_length + 1).join('/')
      destination = import.split('/').last(2).join('/')
      dest_file = file_name.gsub(matcher, destination)
      imports_arr << { destination: dest_file, rule: file.match(/\@import.*#{import}(.*\;)?/)[0] }
    else
      file_uri  = URI.parse(file_name)
      matcher   = file_uri.path.split('/').last
      destination = import
      dest_file = file_name.gsub(matcher, destination)
      imports_arr << { destination: dest_file, rule: file.match(/\@import.*#{import}(.*\;)?/)[0] }
    end
  end
  imports_arr.length > 0 ? imports_arr : nil
end


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/theme_bandit/parser/css.rb', line 17

def link_tag_values
  link_tags.map do |tag|
    href = tag.attribute('href').to_s
    href = cdn_to_fqd(href)
    href = URI.parse(href)
    if href.absolute?
      strip_query_string href.to_s
    else
      new_href = strip_query_string(href.to_s)
      absolute_path = get_absolute_path "#{@url.path}", new_href
      absolute = "#{@url.host}#{absolute_path}"
      "#{@url.scheme}://#{absolute}"
    end
  end
end


11
12
13
14
15
# File 'lib/theme_bandit/parser/css.rb', line 11

def link_tags
  document.css('link').select do |tag|
    tag.attribute('rel').value == 'stylesheet'
  end
end