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.1'
@@compress =
false
@@debug =
false
@@paths =
[]
@@imports =
[]
@@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.



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

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.



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

def compress
  @@compress
end

.compress=(val) ⇒ Object

Sets the global flag for the ‘compress` option.



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

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


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

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.



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

def debug
  @@debug
end

.debug=(val) ⇒ Object

Sets the ‘debug` flag.



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

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

.debug_optionsObject

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



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

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.



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

def defaults
  { compress: self.compress?, paths: self.paths }
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.



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

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.



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

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.



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

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

.pathsObject

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



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

def paths
  @@paths
end

.paths=(val) ⇒ Object

Replaces the global load path ‘Array` of paths.



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

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

.pluginsObject

Retrieves all the registered plugins.



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

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.



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

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.



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

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