Module: Stylus

Extended by:
Runtime, Sprockets
Defined in:
lib/stylus.rb,
lib/stylus/railtie.rb,
lib/stylus/runtime.rb,
lib/stylus/version.rb,
lib/stylus/sprockets.rb,
lib/stylus/import_processor.rb,
lib/rails/generators/stylus/assets/assets_generator.rb,
lib/rails/generators/stylus/scaffold/scaffold_generator.rb

Overview

Based on the ImportProcessor from the less-rails gem, by @metaskills

Defined Under Namespace

Modules: Generators, Runtime, Sprockets Classes: ImportProcessor, Railtie

Constant Summary collapse

VERSION =
"0.7.0"
@@compress =
false
@@debug =
false
@@paths =
[]
@@imports =
[]
@@plugins =
{}

Class Method Summary collapse

Methods included from Sprockets

setup

Methods included from Runtime

exec

Class Method Details

.compile(source, options = {}) ⇒ Object

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.



101
102
103
104
105
106
107
108
# File 'lib/stylus.rb', line 101

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)
  exec('compile', source, options, plugins, imports)
end

.compressObject Also known as: compress?

Returns the global compress flag.



86
87
88
# File 'lib/stylus.rb', line 86

def compress
  @@compress
end

.compress=(val) ⇒ Object

Sets the global flag for the ‘compress` option.



92
93
94
# File 'lib/stylus.rb', line 92

def compress=(val)
  @@compress = val
end

.convert(source) ⇒ Object

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`.


112
113
114
115
# File 'lib/stylus.rb', line 112

def convert(source)
  source = source.read if source.respond_to?(:read)
  exec('convert', source)
end

.debugObject Also known as: debug?

Returns the ‘debug` flag used to set the `linenos` and `firebug` option for Stylus.



67
68
69
# File 'lib/stylus.rb', line 67

def debug
  @@debug
end

.debug=(val) ⇒ Object

Sets the ‘debug` flag.



81
82
83
# File 'lib/stylus.rb', line 81

def debug=(val)
  @@debug = val
end

.debug_optionsObject

Returns a Hash with the debug options to pass to Stylus.



139
140
141
# File 'lib/stylus.rb', line 139

def debug_options
  { :linenos => self.debug?, :firebug => self.debug? }
end

.defaultsObject

Returns the default ‘Hash` of options: the compress flag and the global load path.



133
134
135
# File 'lib/stylus.rb', line 133

def defaults
  { :compress => self.compress?, :paths => self.paths }
end

.import(*paths) ⇒ Object Also known as: imports

Stores a list of stylesheets to import on every compile process.



43
44
45
46
47
48
# File 'lib/stylus.rb', line 43

def import(*paths)
  if paths.any?
    @@imports = @@imports.concat(paths)
  end
@@imports
end

.merge_options(options) ⇒ Object

Returns a ‘Hash` of the given `options` merged with the default configuration. It also concats the global load path with a given `:paths` option.



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stylus.rb', line 119

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

.nib=(flag) ⇒ Object

Marks the ‘nib` plugin to be loaded and included on every stylesheet.



73
74
75
76
77
78
# File 'lib/stylus.rb', line 73

def nib=(flag)
  if flag
    use :nib
    import :nib
  end
end

.pathsObject

Returns the global load path ‘Array` for your stylesheets.



57
58
59
# File 'lib/stylus.rb', line 57

def paths
  @@paths
end

.paths=(val) ⇒ Object

Replaces the global load path ‘Array` of paths.



62
63
64
# File 'lib/stylus.rb', line 62

def paths=(val)
  @@paths = Array(val)
end

.pluginsObject

Retrieves all the registered plugins.



52
53
54
# File 'lib/stylus.rb', line 52

def plugins
  @@plugins
end

.use(*options) ⇒ Object Also known as: plugin

Stores a list of plugins to import inside ‘Stylus`, with an optional hash.



34
35
36
37
38
39
# File 'lib/stylus.rb', line 34

def use(*options)
  arguments = options.last.is_a?(Hash) ? options.pop : {}
  options.each do |plugin|
    @@plugins[plugin] = arguments
  end
end

.versionObject

Return the gem version alongside with the current ‘Stylus` version of your system.



144
145
146
# File 'lib/stylus.rb', line 144

def version
  "Stylus - gem #{VERSION} library #{exec('version')}"
end