Module: Stylus

Extended by:
Runtime
Defined in:
lib/stylus.rb,
lib/stylus/railtie.rb,
lib/stylus/runtime.rb,
lib/stylus/version.rb,
lib/stylus/sprockets.rb,
lib/stylus/tilt/rails.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, Rails, Runtime Classes: ImportProcessor, Railtie

Constant Summary collapse

VERSION =
'1.0.2'
@@compress =
false
@@debug =
false
@@paths =
[]
@@imports =
[]
@@definitions =
{}
@@plugins =
{}

Class Method Summary collapse

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.



113
114
115
116
117
118
119
120
# File 'lib/stylus.rb', line 113

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, definitions)
end

.compressObject Also known as: compress?

Returns the global compress flag.



98
99
100
# File 'lib/stylus.rb', line 98

def compress
  @@compress
end

.compress=(val) ⇒ Object

Sets the global flag for the ‘compress` option.



104
105
106
# File 'lib/stylus.rb', line 104

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


124
125
126
127
# File 'lib/stylus.rb', line 124

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.



79
80
81
# File 'lib/stylus.rb', line 79

def debug
  @@debug
end

.debug=(val) ⇒ Object

Sets the ‘debug` flag.



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

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

.debug_optionsObject

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



151
152
153
# File 'lib/stylus.rb', line 151

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.



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

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

.define(variable, value, options = {}) ⇒ Object

Stores a list of defined variables to create on every compile process.



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

def define(variable, value, options = {})
  literal = true if options[:literal]
  @@definitions[variable] = { value: value, literal: literal }
end

.definitionsObject

Retrieves all the registered variables.



64
65
66
# File 'lib/stylus.rb', line 64

def definitions
  @@definitions
end

.detect_template_hander(options = {}) ⇒ Object

Internal: Gets the desired Tilt template handler to the current configuration. If a ‘rails’ option is present then the Rails specific template will be returned instead of the default Stylus Tilt template.

Returns a Tilt::Template children class.



51
52
53
54
55
56
57
# File 'lib/stylus/sprockets.rb', line 51

def self.detect_template_hander(options = {})
  if options[:rails]
    Stylus::Rails::StylusTemplate
  else
    Tilt::StylusTemplate
  end
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.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/stylus.rb', line 131

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.



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

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

.pathsObject

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



69
70
71
# File 'lib/stylus.rb', line 69

def paths
  @@paths
end

.paths=(val) ⇒ Object

Replaces the global load path ‘Array` of paths.



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

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

.pluginsObject

Retrieves all the registered plugins.



59
60
61
# File 'lib/stylus.rb', line 59

def plugins
  @@plugins
end

.setup(environment, options = {}) ⇒ Object

Public: Configure a Sprockets environment with Stylus Tilt engine and the ImportProcessor. It also accept a configuration Hash to setup the load path and flags of the Stylus module.

environment - A instance of Sprockets::Environment. options - The configuration Hash (default: {})

:rails - a flag to inform that the current application is a Rails app.
:paths - An Array of paths to use the '@import' directive, defaults
         to the `paths` attribute on the environment object.
:debug - The Boolean value for the debug flag.
:compress - The Boolean value for the debug compress.

Example

assets = Sprockets::Environment.new
Stylus.setup(assets, compress: settings.production?)

Returns nothing.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stylus/sprockets.rb', line 34

def self.setup(environment, options = {})
  paths = options[:paths] || environment.paths

  Stylus.paths.concat(paths)

  Stylus.debug = options.fetch(:debug, Stylus.debug)
  Stylus.compress = options.fetch(:compress, Stylus.compress)
  template = detect_template_hander(options)
  environment.register_engine('.styl', template)
  environment.register_preprocessor('text/css', Stylus::ImportProcessor)
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.



156
157
158
# File 'lib/stylus.rb', line 156

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