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
-
.compile(source, options = {}) ⇒ Object
Compiles a given input - a plain String, ‘File` or some sort of IO object that responds to `read`.
-
.compress ⇒ Object
(also: compress?)
Returns the global compress flag.
-
.compress=(val) ⇒ Object
Sets the global flag for the ‘compress` option.
-
.convert(source) ⇒ Object
Converts back an input of plain CSS to the ‘Stylus` syntax.
-
.debug ⇒ Object
(also: debug?)
Returns the ‘debug` flag used to set the `linenos` and `firebug` option for Stylus.
-
.debug=(val) ⇒ Object
Sets the ‘debug` flag.
-
.debug_options ⇒ Object
Returns a Hash with the debug options to pass to Stylus.
-
.defaults ⇒ Object
Returns the default ‘Hash` of options: the compress flag and the global load path.
-
.define(variable, value, options = {}) ⇒ Object
Stores a list of defined variables to create on every compile process.
-
.definitions ⇒ Object
Retrieves all the registered variables.
-
.detect_template_hander(options = {}) ⇒ Object
Internal: Gets the desired Tilt template handler to the current configuration.
-
.import(*paths) ⇒ Object
(also: imports)
Stores a list of stylesheets to import on every compile process.
-
.merge_options(options) ⇒ Object
Returns a ‘Hash` of the given `options` merged with the default configuration.
-
.nib=(flag) ⇒ Object
Marks the ‘nib` plugin to be loaded and included on every stylesheet.
-
.paths ⇒ Object
Returns the global load path ‘Array` for your stylesheets.
-
.paths=(val) ⇒ Object
Replaces the global load path ‘Array` of paths.
-
.plugins ⇒ Object
Retrieves all the registered plugins.
-
.setup(environment, options = {}) ⇒ Object
Public: Configure a Sprockets environment with Stylus Tilt engine and the ImportProcessor.
-
.use(*options) ⇒ Object
(also: plugin)
Stores a list of plugins to import inside ‘Stylus`, with an optional hash.
-
.version ⇒ Object
Return the gem version alongside with the current ‘Stylus` version of your system.
Methods included from Runtime
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, = {}) if source.respond_to?(:path) && source.path [:filename] ||= File.(source.path) end source = source.read if source.respond_to?(:read) = () exec('compile', source, , plugins, imports, definitions) end |
.compress ⇒ Object 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 |
.debug ⇒ Object 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_options ⇒ Object
Returns a Hash with the debug options to pass to Stylus.
151 152 153 |
# File 'lib/stylus.rb', line 151 def { linenos: self.debug?, firebug: self.debug? } end |
.defaults ⇒ Object
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, = {}) literal = true if [:literal] @@definitions[variable] = { value: value, literal: literal } end |
.definitions ⇒ Object
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( = {}) if [: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 () filename = [:filename] _paths = .delete(:paths) = defaults.merge() [:paths] = paths.concat(Array(_paths)) if filename = .merge() end 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 |
.paths ⇒ Object
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 |
.plugins ⇒ Object
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, = {}) paths = [:paths] || environment.paths Stylus.paths.concat(paths) Stylus.debug = .fetch(:debug, Stylus.debug) Stylus.compress = .fetch(:compress, Stylus.compress) template = detect_template_hander() 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(*) arguments = .last.is_a?(Hash) ? .pop : {} .each do |plugin| @@plugins[plugin] = arguments end end |
.version ⇒ Object
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 |