Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Backup old ruby-stylus code suitable for Rails 3.2.0

  • Loading branch information...
commit 194e88b41f3420f92d4da6f05996b3147fba5997 1 parent 7e8cdb6
@x2002g authored
Showing with 906 additions and 3 deletions.
  1. +55 −0 CHANGELOG.md
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +104 −3 README.md
  5. +25 −0 Rakefile
  6. +13 −0 lib/rails/generators/stylus/assets/assets_generator.rb
  7. +3 −0  lib/rails/generators/stylus/assets/templates/stylesheet.css.styl
  8. +11 −0 lib/rails/generators/stylus/scaffold/scaffold_generator.rb
  9. +167 −0 lib/stylus.rb
  10. +25 −0 lib/stylus/compiler.js
  11. +28 −0 lib/stylus/railtie.rb
  12. +20 −0 lib/stylus/runner.js
  13. +30 −0 lib/stylus/tilt.rb
  14. +3 −0  lib/stylus/version.rb
  15. +1 −0  spec/cases/compressed.css
  16. +5 −0 spec/cases/import.css
  17. +5 −0 spec/cases/nib.css
  18. +3 −0  spec/cases/simple.css
  19. +3 −0  spec/cases/stylesheet.css
  20. +2 −0  spec/fixtures/compressed.styl
  21. +2 −0  spec/fixtures/debug.styl
  22. +4 −0 spec/fixtures/import.styl
  23. +4 −0 spec/fixtures/mixins/vendor.styl
  24. +4 −0 spec/fixtures/nib.styl
  25. +2 −0  spec/fixtures/simple.styl
  26. +3 −0  spec/fixtures/stylesheet.styl
  27. +11 −0 spec/generators/assets_generator_spec.rb
  28. +12 −0 spec/generators/controller_generator_spec.rb
  29. +17 −0 spec/generators/scaffold_generator_spec.rb
  30. +25 −0 spec/spec_helper.rb
  31. +42 −0 spec/sprockets_spec.rb
  32. +84 −0 spec/stylus_spec.rb
  33. +62 −0 spec/support/generators/test_case.rb
  34. +47 −0 spec/support/helpers.rb
  35. +5 −0 spec/support/matchers.rb
  36. +26 −0 spec/tilt_spec.rb
  37. +27 −0 stylus.gemspec
View
55 CHANGELOG.md
@@ -0,0 +1,55 @@
+## Changelog
+
+### 0.3.0 (Master)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.2.2...master)
+
+* Adds a `debug` configuration option, that enables the `linenos` and `firebug` flags on Stylus. Inside Rails, this configuration option will be copied from the `config.assets.debug`.
+
+### 0.2.2 (2011-09-14)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.2.1...v0.2.2)
+
+* ExecJS 1.2.5+ compatibility: Using a custom `ExternalRuntime`.
+
+### 0.2.1 (2011-08-30)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.2.0...v0.2.1)
+
+* Removed a hack for a Sprockets loading order issue;
+* Testing on 1.9.2, REE and Rubinius thanks to [Travis CI](travis-ci.org/#!/lucasmazza/ruby-stylus);
+* Enables compression if `Sprockets` is configured to do so;
+* Added Rails and Sprockets tests.
+
+### 0.2.0 (2011-07-14)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.1.2...v0.2.0)
+
+* Replaced `stylus:install` with proper generators for Rails 3.1 - now all hooks for the `stylesheet_engine` will generate `.styl` files.
+
+### 0.1.2 (2011-07-08)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.1.1...v0.1.2)
+
+* Fixes missing `require` for Rails apps.
+
+### 0.1.1 (2011-07-07)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.1.0...v0.1.1)
+
+* Requiring `stylus/tilt` outside the `Railtie`, by [DAddYE](https://github.com/DAddYE).
+
+### 0.1.0 (2011-06-21)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.0.3...v0.1.0)
+
+* Support for Stylus plugins via `Stylus.use`.
+* Docco documentation live at [http://lucasmazza.github.com/ruby-stylus](http://lucasmazza.github.com/ruby-stylus).
+
+
+### 0.0.3 (2011-06-06)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.0.2...v0.0.3)
+
+* Bugfix: stylus now works in production environment.
+
+### 0.0.2 (2011-06-02)
+[Compare view](https://github.com/lucasmazza/ruby-stylus/compare/v0.0.1...v0.0.2)
+
+* Added a Rails Generator called `stylus:install` to copy sample files on your `lib/` folder.
+* Added `Stylus.convert` to parse CSS back to Stylus syntax.
+
+### 0.0.1 (2011-05-18)
+* Initial Release.
View
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in stylus.gemspec
+gemspec
View
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2011 Lucas Mazza <luc4smazza@gmail.com>
+
+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.
View
107 README.md
@@ -1,4 +1,105 @@
-ruby-stylus
-===========
+# Ruby Stylus
-ruby-stylus
+[![Build Status](https://secure.travis-ci.org/lucasmazza/ruby-stylus.png)](http://travis-ci.org/lucasmazza/ruby-stylus)
+
+`stylus` is a bridge between your Ruby code and the [Stylus](https://github.com/LearnBoost/stylus) library that runs on [node.js](http://nodejs.org). It has support for the Rails 3.1 asset pipeline (thanks to a [Tilt](https://github.com/rtomayko/tilt) Template) and it's backed by the [ExecJS](https://github.com/sstephenson/execjs) gem.
+
+## Installation
+
+Be sure to have [node.js](http://nodejs.org) available on your system, and [npm](npmjs.org) to install the [Stylus](https://github.com/LearnBoost/stylus) package (but you can also do a manual installation).
+
+### With npm
+
+You can make a local install (inside your application folder, so the package will be at `./node_modules`) or a global one (with the `-g` flag). With a global install you will need to make sure that the global installation folder is present at the `NODE_PATH` env variable. A global install will also enable the [Stylus](https://github.com/LearnBoost/stylus) command line interface.
+
+### Manual
+
+You can clone Stylus [git repository](http://github.com/learnboost/stylus) into `node_modules/stylus`. Any `node` commands and/or shells from the current directory will be able to find the cloned package.
+
+Check your installation with `node -e "require('stylus')"`. It should print something like [this](https://gist.github.com/1182631) and exit successfully.
+
+## Usage
+
+The interaction is done by the `Stylus` module. You can compile stylus syntax to CSS, convert it back, enable plugins and tweak some other options:
+
+```ruby
+require 'stylus'
+
+# Accepts a raw string or an IO object (File, StringIO or anything that responds to 'read').
+Stylus.compile(File.new('application.styl')) # returns the compiled stylesheet.
+
+# Using the compress option, removing most newlines from the code.
+Stylus.compile(File.read('application.styl'), :compress => true)
+
+# Or use the global compress flag
+Stylus.compress = true
+Stylus.compile(File.read('application.styl'))
+
+# Converting old and boring CSS to awesome Stylus.
+Stylus.convert(File.new('file.css'))
+
+# Importing plugins directly from Node.js, like nib.
+Stylus.use :nib
+
+# Enabling debug info, which sends the 'linenos' and 'firebug' options to Stylus.
+# If you provide a raw content String to the `Stylus.compile` method, remember to send
+# a `:filename` option so Stylus can locate your stylesheet for proper inspection.
+Stylus.debug = true
+```
+### With the Rails 3.1 Asset Pipeline.
+
+Adding `stylus` to your Gemfile should let you work with `.styl` files with the Rails 3.1 Pipeline. Any asset generated with `rails generate` will be created with a `.css.styl` extension.
+
+While [Stylus](https://github.com/LearnBoost/stylus) has a `@import` directive, inside a Rails application you should use the `//= require` directive from Sprockets, so the caching mechanism will look after the changes made on the required file. If you're using mixins (for vendor prefixes or any common statements) or plugins inside your `.styl` files, you should use [Stylus](https://github.com/LearnBoost/stylus) `@import` **and** Sprockets `//= depend_on`. The latter is to ensure the proper dependency management on the Sprockets side.
+
+Any `.styl` file on the Sprockets load path (`app/assets`, `lib/assets`, `vendor/assets` or `assets` folder inside any other gem) will be available via the `@import` directive.
+
+If the `config.assets.debug` is turned on, Stylus will emit exta comments on your stylesheets to help debugging and inspection using the `linenos` and `firebug` options. Check the [FireStylus extension for Firebug](https://github.com/LearnBoost/stylus/blob/master/docs/firebug.md) for more info.
+
+## Plugins
+
+[Stylus](https://github.com/LearnBoost/stylus) exposes a nice API to create plugins written on [node.js](http://nodejs.org), like [nib](https://github.com/visionmedia/nib). The installation process should be same as described above for [Stylus](https://github.com/LearnBoost/stylus) (since they're all npm packages after all). You can hook them'up on your Ruby code with `Stylus.use`:
+
+```ruby
+Stylus.use :fingerprint, :literal => 'caa8c262e23268d2a7062c6217202343b84f472b'
+```
+
+Will run something like this on javascript:
+
+```javascript
+stylus(file).use(fingerprint({literal:'caa8c262e23268d2a7062c6217202343b84f472b'}));
+```
+
+## Question, Bugs or Support
+
+You can [submit an issue](https://github.com/lucasmazza/ruby-stylus/issues) or ping me at [GitHub](http://github.com/lucasmazza) or [twitter](http://twitter.com/lucasmazza).
+
+For more info about the [Stylus](https://github.com/LearnBoost/stylus) syntax and it's features you can check the [project repository](https://github.com/learnboost/stylus) and [GitHub page](learnboost.github.com/stylus).
+
+## Changelog
+[It's available here.](https://github.com/lucasmazza/ruby-stylus/blob/master/CHANGELOG.md)
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 Lucas Mazza &lt;luc4smazza@gmail.com&gt;
+
+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.
View
25 Rakefile
@@ -0,0 +1,25 @@
+require 'fileutils'
+require 'bundler'
+require 'rspec/core/rake_task'
+require 'rocco/tasks'
+Bundler::GemHelper.install_tasks
+
+desc "Run specs"
+RSpec::Core::RakeTask.new do |task|
+ task.rspec_opts = ["-c -b"]
+end
+
+task :default => :spec
+
+Rocco::make 'docs/'
+
+desc 'Builds Rocco docs'
+task :docs => :rocco do
+ Dir['docs/lib/**/*.html'].each do |file|
+ path = file.gsub(/lib/,'')
+ FileUtils.mkdir_p(File.dirname(path))
+ FileUtils.mv(file, path)
+ end
+ cp 'docs/stylus.html', 'docs/index.html', :preserve => true
+end
+directory 'docs/'
View
13 lib/rails/generators/stylus/assets/assets_generator.rb
@@ -0,0 +1,13 @@
+require "rails/generators/named_base"
+
+module Stylus
+ module Generators
+ class AssetsGenerator < ::Rails::Generators::NamedBase
+ source_root File.expand_path("../templates", __FILE__)
+
+ def copy_stylus
+ template "stylesheet.css.styl", File.join('app/assets/stylesheets', class_path, "#{file_name}.css.styl")
+ end
+ end
+ end
+end
View
3  lib/rails/generators/stylus/assets/templates/stylesheet.css.styl
@@ -0,0 +1,3 @@
+// Place all the styles related to the matching controller here.
+// They will automatically be included in application.css.
+// You can use Stylus syntax here: http://learnboost.github.com/stylus
View
11 lib/rails/generators/stylus/scaffold/scaffold_generator.rb
@@ -0,0 +1,11 @@
+require 'rails/generators/css/scaffold/scaffold_generator'
+
+module Stylus
+ module Generators
+ # Just inherit from the original Generator from Rails
+ # because `scaffold.css` it's just to help people to start up
+ # their Rails applications.
+ class ScaffoldGenerator < Css::Generators::ScaffoldGenerator
+ end
+ end
+end
View
167 lib/stylus.rb
@@ -0,0 +1,167 @@
+require 'execjs'
+require 'stylus/version'
+require 'stylus/tilt' if defined?(::Tilt)
+require 'stylus/railtie' if defined?(::Rails)
+require 'stylus/source'
+
+## Stylus
+#
+# `stylus` is a bridge between your Ruby code and the [Stylus](https://github.com/LearnBoost/stylus)
+# library that runs on Node.js. It's aims to be a replacement for the
+# [stylus_rails](https://github.com/lucasmazza/stylus_rails) gem and to support the Rails 3.1 asset pipeline
+# (via [Tilt](https://github.com/rtomayko/tilt)) and other scenarios,
+# backed by the [ExecJS](https://github.com/sstephenson/execjs) gem.
+#
+### Usage
+#
+# To compile a `.styl` file or an arbitrary String to .CSS using stylus, just use the `compile` method.
+#
+# `Stylus.compile(File.new('application.styl'))`
+#
+# A hash of options for the stylus API is accepted.
+#
+# `Stylus.compile(File.read('application.styl'), :compress => true)`
+#
+module Stylus
+ class << self
+ @@compress = false
+ @@debug = false
+ @@paths = []
+ @@plugins = {}
+ @@plugin_paths = []
+
+ # Stores a list of plugins to import inside `Stylus`, with an optional hash.
+ def use(*options)
+ arguments = options.last.is_a?(Hash) ? options.pop : {}
+ options.each do |plugin|
+ @@plugins[plugin] = arguments
+ end
+ end
+ alias :plugin :use
+
+ # Retrieves all the registered plugins.
+ def plugins
+ @@plugins
+ end
+
+ # Returns the global load path `Array` for your stylesheets.
+ def paths
+ @@paths
+ end
+
+ # Replaces the global load path `Array` of paths.
+ def paths=(val)
+ @@paths = Array(val)
+ end
+
+ # Returns the global plugin load path `Array`
+ def plugin_paths
+ @@plugin_paths
+ end
+
+ # Returns the `debug` flag used to set the `linenos` and `firebug` option for Stylus.
+ def debug
+ @@debug
+ end
+ alias :debug? :debug
+
+ # Sets the `debug` flag.
+ def debug=(val)
+ @@debug = val
+ end
+
+ # Returns the global compress flag.
+ def compress
+ @@compress
+ end
+ alias :compress? :compress
+
+ # Sets the global flag for the `compress` option.
+ def compress=(val)
+ @@compress = val
+ end
+
+ # Compiles a given input - a plain String, `File` or some sort of IO object that
+ # responds to `read`.
+ # It accepts a hash of options that will be merged with the global configuration.
+ # If the source has a `path`, it will be expanded and used as the :filename option
+ # So the debug options can be used.
+ def compile(source, options = {})
+ if source.respond_to?(:path) && source.path
+ options[:filename] ||= File.expand_path(source.path)
+ end
+ source = source.read if source.respond_to?(:read)
+ options = merge_options(options)
+ context.call('compiler', source, options, plugins)
+ end
+
+ # Converts back an input of plain CSS to the `Stylus` syntax. The source object can be
+ # a `File`, `StringIO`, `String` or anything that responds to `read`.
+ def convert(source)
+ source = source.read if source.respond_to?(:read)
+ context.call('convert', source)
+ end
+
+ # Returns a `Hash` of the given `options` merged with the default configuration.
+ # It also concats the global load path with a given `:paths` option.
+ def merge_options(options)
+ filename = options[:filename]
+
+ _paths = options.delete(:paths)
+ options = defaults.merge(options)
+ options[:paths] = paths.concat(Array(_paths))
+ if filename
+ options = options.merge(debug_options)
+ end
+ options
+ end
+
+ # Returns the default `Hash` of options:
+ # the compress flag and the global load path.
+ def defaults
+ { :compress => self.compress?, :paths => self.paths }
+ end
+
+ # Returns a Hash with the debug options to pass to
+ # Stylus.
+ def debug_options
+ { :linenos => self.debug?, :firebug => self.debug? }
+ end
+
+ # Return the gem version alongside with the current `Stylus` version of your system.
+ def version
+ "Stylus - gem #{VERSION} library #{context.call('version')}"
+ end
+
+ protected
+ # Returns the `ExecJS` execution context.
+ def context
+ @@_context ||= backend.compile(script)
+ end
+
+ # Reads the default compiler script that `ExecJS` will execute.
+ def script
+ js_code = ""
+ js_code << "require.paths.unshift('#{File.dirname(Stylus::Source.bundled_path)}');\n"
+ plugin_paths.each do |node_path|
+ js_code << "require.paths.unshift('#{node_path}');\n"
+ end
+ js_code << File.read(File.expand_path('../stylus/compiler.js',__FILE__))
+ js_code
+ end
+
+ # `ExecJS` 1.2.5+ doesn't support `require` statements on node anymore,
+ # so we use a new instance of the `ExternalRuntime` with the old runner script.
+ def backend
+ @@_backend ||= ExecJS::ExternalRuntime.new(
+ :name => 'Node.js (V8)',
+ :command => ["nodejs", "node"],
+ :runner_path => File.expand_path("../stylus/runner.js", __FILE__)
+ )
+ end
+ end
+
+ # Exports the `.node_modules` folder on the working directory so npm can
+ # require modules installed locally.
+ ENV['NODE_PATH'] = "#{File.expand_path('node_modules')}:#{ENV['NODE_PATH']}"
+end
View
25 lib/stylus/compiler.js
@@ -0,0 +1,25 @@
+var stylus = require('stylus');
+
+function compiler(str, options, plugins) {
+ var style = stylus(str, options);
+ var output = '';
+
+ for(var name in plugins) {
+ var fn = require(name);
+ style.use(fn(plugins[name]));
+ }
+
+ style.render(function(error, css) {
+ if(error) throw error;
+ output = css;
+ })
+ return output;
+}
+
+function convert(str) {
+ return stylus.convertCSS(str);
+}
+
+function version() {
+ return stylus.version;
+}
View
28 lib/stylus/railtie.rb
@@ -0,0 +1,28 @@
+require 'stylus/tilt'
+module Stylus
+ ### Stylus Railtie
+ #
+ # `Railtie` that hooks `stylus` inside a Rails application.
+ class Railtie < ::Rails::Railtie
+
+ config.app_generators.stylesheet_engine :stylus
+
+ initializer :setup_stylus, after: "sprockets.environment" do |app|
+ next unless app.assets
+ app.assets.register_engine '.styl', Tilt::StylusTemplate
+ end
+
+ # After initialization block to inspect the `Sprockets` configuration
+ # And reflect it on the `Stylus` module;
+ # It also includes the `Rails` asset load path into `Stylus` so any
+ # `.styl` file inside it can be imported by the `Stylus` API.
+ config.after_initialize do |app|
+ Stylus.compress = app.config.assets.compress
+ Stylus.debug = app.config.assets.debug
+ Stylus.paths ||= []
+ if app.assets
+ Stylus.paths.concat app.assets.paths
+ end
+ end
+ end
+end
View
20 lib/stylus/runner.js
@@ -0,0 +1,20 @@
+(function(program, execJS) { execJS(program) })(function() { #{source}
+}, function(program) {
+ var output, print = function(string) {
+ process.stdout.write('' + string);
+ };
+ try {
+ result = program();
+ if (typeof result == 'undefined' && result !== null) {
+ print('["ok"]');
+ } else {
+ try {
+ print(JSON.stringify(['ok', result]));
+ } catch (err) {
+ print('["err"]');
+ }
+ }
+ } catch (err) {
+ print(JSON.stringify(['err', '' + err]));
+ }
+});
View
30 lib/stylus/tilt.rb
@@ -0,0 +1,30 @@
+require 'tilt'
+
+module Tilt
+ ### stylus template implementation for `Tilt`.
+ #
+ # It can be used by the `Rails` 3.1 or `Sinatra` applications.
+ class StylusTemplate < Template
+ self.default_mime_type = 'text/css'
+
+ def self.engine_initialized?
+ defined? ::Stylus
+ end
+
+ def initialize_engine
+ require_template_library 'stylus'
+ end
+
+ def prepare
+ if self.file
+ options[:filename] ||= self.file
+ end
+ end
+
+ def evaluate(scope, locals, &block)
+ @output ||= Stylus.compile(data, options)
+ end
+ end
+end
+
+Tilt.register Tilt::StylusTemplate, 'styl'
View
3  lib/stylus/version.rb
@@ -0,0 +1,3 @@
+module Stylus
+ VERSION = "0.3.0"
+end
View
1  spec/cases/compressed.css
@@ -0,0 +1 @@
+body{padding:5px}
View
5 spec/cases/import.css
@@ -0,0 +1,5 @@
+a {
+ -moz-border-radius: 3px;
+ -webkit-border-radius: 3px;
+ border-radius: 3px;
+}
View
5 spec/cases/nib.css
@@ -0,0 +1,5 @@
+background {
+ line-height: 1;
+ color: #000;
+ background: #fff;
+}
View
3  spec/cases/simple.css
@@ -0,0 +1,3 @@
+body {
+ padding: 5px;
+}
View
3  spec/cases/stylesheet.css
@@ -0,0 +1,3 @@
+body {
+ padding: 5px;
+}
View
2  spec/fixtures/compressed.styl
@@ -0,0 +1,2 @@
+body
+ padding 5px
View
2  spec/fixtures/debug.styl
@@ -0,0 +1,2 @@
+body
+ color black
View
4 spec/fixtures/import.styl
@@ -0,0 +1,4 @@
+@import 'mixins/vendor'
+
+a
+ border-radius 3px
View
4 spec/fixtures/mixins/vendor.styl
@@ -0,0 +1,4 @@
+border-radius()
+ -moz-border-radius arguments
+ -webkit-border-radius arguments
+ border-radius arguments
View
4 spec/fixtures/nib.styl
@@ -0,0 +1,4 @@
+@import 'nib'
+
+background
+ reset-body()
View
2  spec/fixtures/simple.styl
@@ -0,0 +1,2 @@
+body
+ padding 5px
View
3  spec/fixtures/stylesheet.styl
@@ -0,0 +1,3 @@
+body
+ padding: 5px
+
View
11 spec/generators/assets_generator_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+require 'rails/generators/stylus/assets/assets_generator'
+
+describe Stylus::Generators::AssetsGenerator do
+ include Generators::TestCase
+ arguments %w(posts)
+
+ it 'generates a .styl file' do
+ file('app/assets/stylesheets/posts.css.styl').should exist
+ end
+end
View
12 spec/generators/controller_generator_spec.rb
@@ -0,0 +1,12 @@
+require 'spec_helper'
+require 'rails/generators/rails/controller/controller_generator'
+require 'rails/generators/stylus/assets/assets_generator'
+
+describe Rails::Generators::ControllerGenerator do
+ include Generators::TestCase
+ arguments %w(posts --stylesheet-engine=stylus)
+
+ it 'generates a .styl file' do
+ file('app/assets/stylesheets/posts.css.styl').should exist
+ end
+end
View
17 spec/generators/scaffold_generator_spec.rb
@@ -0,0 +1,17 @@
+require 'spec_helper'
+require 'rails/generators/rails/scaffold/scaffold_generator'
+require 'rails/generators/stylus/assets/assets_generator'
+require 'rails/generators/stylus/scaffold/scaffold_generator'
+
+describe Rails::Generators::ScaffoldGenerator do
+ include Generators::TestCase
+ arguments %w(posts --stylesheet-engine=stylus --orm=false)
+
+ it 'generates the default scaffold stylesheet' do
+ file('app/assets/stylesheets/scaffold.css').should exist
+ end
+
+ it 'generates a named .styl file' do
+ file('app/assets/stylesheets/posts.css.styl').should exist
+ end
+end
View
25 spec/spec_helper.rb
@@ -0,0 +1,25 @@
+require 'rails'
+require 'rails/test_help'
+require 'rails/generators/test_case'
+
+require 'active_support/railtie'
+require 'action_controller/railtie'
+require 'sprockets/railtie'
+require 'stylus/railtie'
+
+require 'active_support/core_ext/class/attribute_accessors'
+
+require 'support/helpers'
+require 'support/matchers'
+require 'support/generators/test_case'
+
+RSpec.configure do |config|
+ config.include Helpers
+
+ config.after :each do
+ Stylus.compress = false
+ Stylus.debug = false
+ Stylus.paths = []
+ Stylus.plugins.clear
+ end
+end
View
42 spec/sprockets_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe 'Sprockets and Rails integration' do
+
+ it 'copies the asset paths into the Stylus load path' do
+ app = create_app
+ Stylus.paths.should =~ app.assets.paths
+ end
+
+ it 'process .styl files with the asset pipeline' do
+ result = fixture(:simple).last
+
+ app = create_app
+ app.assets['simple'].to_s.should == result
+ end
+
+ it 'enables @import definitions' do
+ result = fixture(:import).last
+
+ app = create_app
+ app.assets['import'].to_s.should == result
+ end
+
+ it 'skips debug info by default' do
+ app = create_app
+ asset = app.assets['simple']
+ asset.to_s.should_not match(/line 1 : #{asset.pathname}/)
+ end
+
+ it 'provides debug info if required' do
+ app = create_app(:debug => true)
+ asset = app.assets['simple']
+ asset.to_s.should match(/line 1 : #{asset.pathname}/)
+ end
+
+ it 'compress the output if Rails is configured to compress them too' do
+ result = fixture(:compressed).last
+
+ app = create_app(:compress => true)
+ app.assets['compressed'].to_s.should == result
+ end
+end
View
84 spec/stylus_spec.rb
@@ -0,0 +1,84 @@
+require 'stylus'
+require 'spec_helper'
+
+describe Stylus do
+
+ it "compiles the given source" do
+ input, output = fixture :simple
+ Stylus.compile(input).should == output
+ end
+
+ it "accepts an IO object" do
+ input, output = fixture :simple
+ input = StringIO.new(input)
+ Stylus.compile(input).should == output
+ end
+
+ it "compress the file when the 'compress' flag is given" do
+ input, output = fixture :compressed
+ Stylus.compile(input, :compress => true).should == output
+ end
+
+ it "handles the compress flag globally" do
+ Stylus.compress = true
+ input, output = fixture :compressed
+ Stylus.compile(input).should == output
+ end
+
+ it "imports the given paths" do
+ path = fixture_root
+ input, output = fixture :import
+ Stylus.compile(input, :paths => path).should == output
+ end
+
+ it "handles the import paths globally" do
+ Stylus.paths << fixture_root
+ input, output = fixture :import
+ Stylus.compile(input).should == output
+ end
+
+ it "outputs both gem and library version" do
+ Stylus.version.should =~ /Stylus - gem .+ library .+/
+ end
+
+ it "converts CSS to Stylus" do
+ stylus, css = fixture :stylesheet
+ Stylus.convert(css).should == stylus
+ end
+
+ it "stores the given plugins" do
+ Stylus.use :one, :two, :argument => true
+ Stylus.should have(2).plugins
+ end
+
+ it "includes the given plugins" do
+ Stylus.use :nib
+ input, output = fixture :nib
+ Stylus.compile(input).should == output
+ end
+
+ describe "The debug flag" do
+
+ let(:path) { fixture_path(:debug) }
+ let(:fixture) { File.read(path) }
+ let(:file) { File.new(path) }
+
+ before { Stylus.debug = true }
+
+ it "turns the 'linenos' option on" do
+ Stylus.compile(file).should match(/line 1 : #{path}/)
+ end
+
+ it "skips the 'linenos' option if no filename is given" do
+ Stylus.compile(fixture).should_not match(/line 1 : #{path}/)
+ end
+
+ it "turns the 'firebug' option on" do
+ Stylus.compile(file).should match(/@media -stylus-debug-info/)
+ end
+
+ it "skips the 'firebug' option if no filename is given" do
+ Stylus.compile(fixture).should_not match(/@media -stylus-debug-info/)
+ end
+ end
+end
View
62 spec/support/generators/test_case.rb
@@ -0,0 +1,62 @@
+# Extracted from generator_spec https://github.com/stevehodgkiss/generator_spec
+# But `RSpec::Rails::RailsExampleGroup` loads a truckload of things from rails and rspec-rails
+# That we don't need.
+
+module Generators
+ module TestCase
+ extend ActiveSupport::Concern
+ include FileUtils
+
+ included do
+ cattr_accessor :test_case, :test_case_instance
+
+ self.test_case = Class.new(Rails::Generators::TestCase) do
+ def fake_test_case; end
+ def add_assertion; end
+ end
+ self.test_case_instance = self.test_case.new(:fake_test_case)
+ self.test_case.tests described_class
+
+ before do
+ prepare_destination
+ create_routes
+ run_generator
+ end
+
+ destination File.expand_path("../tmp", __FILE__)
+ end
+
+ module ClassMethods
+ def tests(klass)
+ self.test_case.generator_class = klass
+ end
+
+ def arguments(array)
+ self.test_case.default_arguments = array
+ end
+
+ def destination(path)
+ self.test_case.destination_root = path
+ end
+ end
+
+ module InstanceMethods
+
+ def file(relative)
+ File.expand_path(relative, destination_root)
+ end
+
+ def method_missing(method_sym, *arguments, &block)
+ self.test_case_instance.send(method_sym, *arguments, &block)
+ end
+
+ def respond_to?(method_sym, include_private = false)
+ if self.test_case_instance.respond_to?(method_sym)
+ true
+ else
+ super
+ end
+ end
+ end
+ end
+end
View
47 spec/support/helpers.rb
@@ -0,0 +1,47 @@
+module Helpers
+ def create_routes
+ destination = File.join(destination_root, "config")
+
+ FileUtils.mkdir_p(destination)
+ FileUtils.touch File.join(destination, 'routes.rb')
+ end
+
+ def create_app(options = {})
+ Rails.application = nil
+
+ Class.new(Rails::Application).tap do |app|
+ assets = app.config.assets
+ assets.cache_store = :memory_store
+ assets.enabled = true
+ assets.compress = true if options[:compress]
+ assets.debug = true if options[:debug]
+ assets.paths << fixture_root
+ app.config.active_support.deprecation = :log
+ app.initialize!
+ end
+ end
+
+ def fixture_root
+ File.expand_path('../../fixtures', __FILE__)
+ end
+
+ def output_root
+ File.expand_path('../../cases', __FILE__)
+ end
+
+ def fixture(name)
+ source = fixture_path(name)
+ output = css_path(name)
+ [source, output].map do |path|
+ File.read(path) if File.file?(path)
+ end
+ end
+
+ def css_path(name)
+ File.join(output_root, "#{name}.css")
+ end
+
+ def fixture_path(name)
+ File.join(fixture_root, "#{name}.styl")
+ end
+end
View
5 spec/support/matchers.rb
@@ -0,0 +1,5 @@
+RSpec::Matchers.define :exist do
+ match do |file_path|
+ File.exists?(file_path)
+ end
+end
View
26 spec/tilt_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+require 'stylus/tilt'
+
+describe Tilt::StylusTemplate do
+
+ it "registers the template for .styl files" do
+ Tilt['application.styl'].should == Tilt::StylusTemplate
+ end
+
+ it "has a content-type" do
+ Tilt::StylusTemplate.default_mime_type.should == 'text/css'
+ end
+
+ it "compiles the given source" do
+ input, output = fixture :simple
+ template = Tilt::StylusTemplate.new { |t| input }
+ template.render.should == output
+ end
+
+ it "compiles with the compress option" do
+ input, output = fixture :compressed
+ template = Tilt::StylusTemplate.new(:compress => true) { |t| input }
+ template.render.should == output
+ end
+
+end
View
27 stylus.gemspec
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "stylus/version"
+
+Gem::Specification.new do |s|
+ s.name = "stylus"
+ s.version = Stylus::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Lucas Mazza"]
+ s.email = ["luc4smazza@gmail.com"]
+ s.homepage = "https://github.com/lucasmazza/ruby-stylus"
+ s.summary = %q{Ruby Stylus Compiler}
+ s.description = %q{Bridge library to compile .styl stylesheets from ruby code.}
+
+ s.add_dependency 'stylus-source'
+ s.add_dependency 'execjs'
+ s.add_development_dependency 'rspec', '~> 2.0'
+ s.add_development_dependency 'railties', '~> 3.1.0'
+ s.add_development_dependency 'tzinfo'
+ s.add_development_dependency 'yajl-ruby'
+ s.add_development_dependency 'rocco'
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
Please sign in to comment.
Something went wrong with that request. Please try again.