Class: Less::More

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-less-more/more.rb

Constant Summary collapse

HEADER =
%{/*\n\n\n\n\n\tThis file was auto generated by Less (http://lesscss.org). To change the contents of this file, edit %s instead.\n\n\n\n\n*/}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.compressionObject



39
40
41
# File 'lib/merb-less-more/more.rb', line 39

def compression
  @compression
end

.destination_pathObject



31
32
33
# File 'lib/merb-less-more/more.rb', line 31

def destination_path
  @destination_path || 'stylesheets'
end

.headerObject



27
28
29
# File 'lib/merb-less-more/more.rb', line 27

def header
  @header.nil? ? true : @header
end

.source_pathObject



35
36
37
# File 'lib/merb-less-more/more.rb', line 35

def source_path
  @source_path || 'app/stylesheets'
end

Class Method Details

.all_less_filesObject

Array of paths of less source files.



91
92
93
94
# File 'lib/merb-less-more/more.rb', line 91

def all_less_files
  all = Dir[File.join(Merb.root, source_path, "**", "*.{css,less,lss}")]
  all.reject{|path| File.basename(path) =~ /^_/ }
end

.generate(source) ⇒ Object

Generates the .css from a .less or .lss file in Less::More.source_path matching the given parameters.

Less::More.generate("screen.less")
Less::More.generate("subdirectories/here/homepage.less")


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/merb-less-more/more.rb', line 48

def generate(source)
  generated = to_dot_css(path_to_destination(source))
  path_to_source = File.join(Merb.root, source_path, source)

  # check if the destination file exists, and compare the modified times to see if it needs to be written
  if mtime(generated) >= mtime_including_imports(path_to_source)
    # up to date, nothing to do!
  else
    # css file does not exist or is out of date
    css = if File.extname(path_to_source) == ".css"
      # vanilla css nothing to do!
      File.read(path_to_source)
    else
      # less or lss file, compile it
      css = compile(path_to_source)
      css.delete!("\n") if compression # TODO: use real compression !
      css = (HEADER % [File.join(source_path, source)]) << css if header
      css
    end

    # write the css
    FileUtils.mkdir_p File.dirname(generated)
    File.open(generated, "w"){|f| f.write css }
  end
end

.generate_allObject

Generates all the .css files



75
76
77
78
79
# File 'lib/merb-less-more/more.rb', line 75

def generate_all
  all_less_files.each do |path|
    generate(relative_to_source_path(path))
  end
end

.remove_all_generatedObject

Removes all generated css files.



82
83
84
85
86
87
88
# File 'lib/merb-less-more/more.rb', line 82

def remove_all_generated
  all_less_files.each do |path|
    css_path  = to_dot_css(relative_to_source_path(path))
    css_file = path_to_destination(css_path)
    File.delete(css_file) if File.file?(css_file)
  end
end