Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
upgrading less/more plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rick committed Jul 24, 2010
1 parent 3c32f39 commit 4cda3f4
Show file tree
Hide file tree
Showing 14 changed files with 680 additions and 0 deletions.
1 change: 1 addition & 0 deletions vendor/plugins/more/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.gem
20 changes: 20 additions & 0 deletions vendor/plugins/more/MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2009 Logan Raarup, August Lilleaas

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
130 changes: 130 additions & 0 deletions vendor/plugins/more/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
More
====

*LESS on Rails.*

More is a plugin for Ruby on Rails applications. It automatically parses your applications `.less` files through LESS and outputs CSS files.

In details, More does the following:

* Recursively looks for LESS (`.less`) files in `app/stylesheets`
* Ignores partials (prefixed with underscore: `_partial.less`) - these can be included with `@import` in your LESS files
* Saves the resulting CSS files to `public/stylesheets` using the same directory structure as `app/stylesheets`

LESS
----

LESS extends CSS with: variables, mixins, operations and nested rules. For more information, see [http://lesscss.org](http://lesscss.org).

Upgrading from less-for-rails
=======================================

The old `less-for-rails` plugin looked for `.less` files in `public/stylesheets`. This plugin looks in `app/stylesheets`.

To migrate, you can either set `Less::More.source_path = Rails.root + "/public/stylesheets"`, or move your `.less` files to `app/stylesheets`.


Installation
============

More depends on the LESS gem. Please install LESS first:

$ gem install less

Rails Plugin
------------

Use this to install as a plugin in a Ruby on Rails app:

$ script/plugin install git://github.com/cloudhead/more.git

Rails Plugin (using git submodules)
-----------------------------------

Use this if you prefer to use git submodules for plugins:

$ git submodule add git://github.com/cloudhead/more.git vendor/plugins/more
$ script/runner vendor/plugins/more/install.rb


Usage
=====

Upon installation, a new directory will be created in `app/stylesheets`. Any LESS file placed in this directory, including subdirectories, will
automatically be parsed through LESS and saved as a corresponding CSS file in `public/stylesheets`. Example:

app/stylesheets/clients/screen.less => public/stylesheets/clients/screen.css

If you prefix a file with an underscore, it is considered to be a partial, and will not be parsed unless included in another file. Example:

<file: app/stylesheets/clients/partials/_form.less>
@text_dark: #222;

<file: app/stylesheets/clients/screen.less>
@import "partials/_form";

input { color: @text_dark; }

The example above will result in a single CSS file in `public/stylesheets/clients/screen.css`.

Any `.css` file placed in `app/stylesheets` will be copied into `public/stylesheets` without being parsed through LESS.


Configuration
=============

Source path: the location of your LESS files (default: app/stylesheets)

Less::More.source_path = "public/stylesheets/less"

Destination Path: where the css goes (public/destination_path) (default: stylesheets)

Less::More.destination_path = "css"

More can compress your files by removing extra line breaks (default: true)

Less::More.compression = false

More inserts headers in the generated CSS files, letting people know that the file is in fact generated and shouldn't be edited directly. (default: true)

Less::More.header = false

To configure More for a specific environment, add configuration options into the environment file, such as `config/environments/development.rb`.

If you wish to apply the configuration to all environments, place them in `config/environment.rb`.


Tasks
=====

More provides a set of Rake tasks to help manage your CSS files.

To parse all LESS files and save the resulting CSS files to the destination path, run:

$ rake more:generate

To delete all generated CSS files, run:

$ rake more:clean

This task will not delete any CSS files from the destination path, that does not have a corresponding LESS file in the source path.


Git / SVN
=========

Check in all the generated css(destination path), they are only generated in development

Documentation
=============

To view the full RDoc documentation, go to [http://rdoc.info/projects/cloudhead/more](http://rdoc.info/projects/cloudhead/more)


Contributors
============
* August Lilleaas ([http://github.com/augustl](http://github.com/augustl))
* Logan Raarup ([http://github.com/logandk](http://github.com/logandk))
* Michael Grosser ([http://github.com/grosser](http://github.com/grosser))

LESS is maintained by Alexis Sellier [http://github.com/cloudhead](http://github.com/cloudhead)
23 changes: 23 additions & 0 deletions vendor/plugins/more/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the more plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the more plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'More'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.markdown')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions vendor/plugins/more/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), 'rails', 'init')
4 changes: 4 additions & 0 deletions vendor/plugins/more/install.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "fileutils"
include FileUtils::Verbose

mkdir_p File.join(Rails.root, "app", "stylesheets")
7 changes: 7 additions & 0 deletions vendor/plugins/more/lib/less/controller_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ActionController::Base
before_filter :generate_css_from_less

def generate_css_from_less
Less::More.generate_all
end
end
142 changes: 142 additions & 0 deletions vendor/plugins/more/lib/less/more.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Less::More provides methods for parsing LESS files in a rails application to CSS target files.
#
# When Less::More.parse is called, all files in Less::More.source_path will be parsed using LESS
# and saved as CSS files in Less::More.destination_path. If Less::More.compression is set to true,
# extra line breaks will be removed to compress the CSS files.
#
# By default, Less::More.parse will be called for each request in `development` environment and on
# application initialization in `production` environment.

begin
require 'less'
rescue LoadError => e
e.message << " (You may need to install the less gem)"
raise e
end

class Less::More
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 << self
# Less::More.compression = true/false --- compress generated css ? (default: false)
# Less::More.header = true/false --- insert editing warning into css ? (default: true)
# Less::More.destination_path = 'css' --- put css into public/??? (default: stylesheets)
# Less::More.source_path = 'public/stylesheets/less' --- where do less files live? (default: app/stylesheets)
attr_writer :compression, :header, :destination_path, :source_path

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

def destination_path
@destination_path || 'stylesheets'
end

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

def compression
@compression
end

# 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")
def generate(source)
generated = to_dot_css(path_to_destination(source))
path_to_source = File.join(Rails.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

# Generates all the .css files
def generate_all
all_less_files.each do |path|
generate(relative_to_source_path(path))
end
end

# Removes all generated css files.
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

# Array of paths of less source files.
def all_less_files
all = Dir[File.join(Rails.root, source_path, "**", "*.{css,less,lss}")]
all.reject{|path| File.basename(path) =~ /^_/ }
end

private

def mtime(file)
return 0 unless File.file?(file)
File.mtime(file).to_i
end

# consider imports for mtime
# just 1 level deep so we do not get any looping/nesting errors
def mtime_including_imports(file)
mtimes = [mtime(file)]
File.readlines(file).each do |line|
if line =~ /^\s*@import ['"]([^'"]+)/
imported = File.join(File.dirname(file), $1)
mtimes << if imported =~ /\.le?ss$/ # complete path given ?
mtime(imported)
else # we need to add .less or .lss
[mtime("#{imported}.less"), mtime("#{imported}.lss")].max
end
end
end
mtimes.max
end

def compile(file)
begin
engine = File.open(file){|f| Less::Engine.new(f) }
engine.to_css
rescue Exception => e
e.message << "\nFrom #{file}"
raise e
end
end

def to_dot_css(path)
path.to_s.sub(/(le?|c)ss$/, "css")
end

def path_to_destination(path)
File.join(Rails.root, "public", destination_path, path)
end

def relative_to_source_path(path)
path.to_s.sub(File.join(Rails.root, source_path), '')[1..-1]
end
end
end
16 changes: 16 additions & 0 deletions vendor/plugins/more/lib/tasks/more_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace :more do
desc "Generate CSS files from LESS files"
task :generate => :environment do
puts "Generating css from less files in #{Less::More.source_path}."
Less::More.generate_all
puts "Done."

end

desc "Remove generated CSS files"
task :clean => :environment do
puts "Deleting all generated css files in #{Less::More.destination_path}"
Less::More.remove_all_generated
puts "Done."
end
end
17 changes: 17 additions & 0 deletions vendor/plugins/more/more.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

require 'rake'

SPEC = Gem::Specification.new do |s|
s.name = "more"
s.summary = "LESS on Rails"
s.homepage = "http://github.com/cloudhead/more"
s.description = <<-EOS
More is a plugin for Ruby on Rails applications. It automatically
parses your applications .less files through LESS and outputs CSS files.
EOS
s.authors = ["August Lilleaas", "Logan Raarup"]
s.version = "0.1.0"
s.files = FileList["README.markdown", "MIT-LICENSE", "Rakefile", "init.rb", "lib/*.rb", "rails/init.rb", "tasks/*", "test/*"]
s.has_rdoc = true
s.add_dependency "less"
end
1 change: 1 addition & 0 deletions vendor/plugins/more/rails/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), '..', 'lib', 'less', 'controller_extension') if RAILS_ENV == 'development'
Loading

0 comments on commit 4cda3f4

Please sign in to comment.