Skip to content

Commit

Permalink
Add api_calls to shell out to stylus' javascript api.
Browse files Browse the repository at this point in the history
I needed this method to create variables for custom theming. It's essentially just a simple wrapper that shells out to commands on the stylus object. Works as a class method on Stylus, e.g:

`Stylus.api "define", "mycolor", "red"`

which uses the `define` api to create a `mycolor` global variable.

Also included a passing test.  I'd like to see this in the next ruby-stylus version in some form or another, so let me know what you think!
  • Loading branch information
joeellis committed Mar 5, 2014
1 parent b654ff0 commit 49859ac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
24 changes: 18 additions & 6 deletions lib/stylus.rb
Expand Up @@ -23,11 +23,12 @@
module Stylus
extend Runtime
class << self
@@compress = false
@@debug = false
@@paths = []
@@imports = []
@@plugins = {}
@@compress = false
@@debug = false
@@paths = []
@@imports = []
@@api_calls = {}
@@plugins = {}

# Stores a list of plugins to import inside `Stylus`, with an optional hash.
def use(*options)
Expand All @@ -47,11 +48,22 @@ def import(*paths)
end
alias :imports :import


# Stores a list of api calls to execute on every compile process.
def api(command, *args)
@@api_calls[command] = args
end

# Retrieves all the registered plugins.
def plugins
@@plugins
end

# Retrieves all the registered api calls.
def api_calls
@@api_calls
end

# Returns the global load path `Array` for your stylesheets.
def paths
@@paths
Expand Down Expand Up @@ -103,7 +115,7 @@ def compile(source, options = {})
end
source = source.read if source.respond_to?(:read)
options = merge_options(options)
exec('compile', source, options, plugins, imports)
exec('compile', source, options, plugins, imports, api_calls)
end

# Converts back an input of plain CSS to the `Stylus` syntax. The source object can be
Expand Down
7 changes: 6 additions & 1 deletion lib/stylus/runtime/compiler.js
@@ -1,17 +1,22 @@
var stylus = require('stylus');

function compile(str, options, plugins, imports) {
function compile(str, options, plugins, imports, apiCalls) {
var style = stylus(str, options);
var output = '';

for(var name in plugins) {
var fn = require(name);
style.use(fn(plugins[name]));
}

imports.forEach(function(path) {
style.import(path);
})

for(var command in apiCalls) {
style[command].apply(style, apiCalls[command]);
}

style.render(function(error, css) {
if(error) throw error;
output = css;
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -21,6 +21,7 @@
Stylus.debug = false
Stylus.paths = []
Stylus.plugins.clear
Stylus.api_calls.clear
Stylus.imports.clear
end
end
2 changes: 2 additions & 0 deletions spec/stylesheets/api_call.styl
@@ -0,0 +1,2 @@
body
color: mycolor
11 changes: 11 additions & 0 deletions spec/stylus_spec.rb
Expand Up @@ -62,6 +62,17 @@
expect(Stylus.compile(input)).to eq(output)
end

it 'stores the api calls' do
Stylus.api "define", "mycolor", "red"
expect(Stylus).to have(1).api_calls
end

it 'executes the given api call' do
Stylus.api "define", "mycolor", "red"
input, output = fixture(:api_call)
expect(Stylus.compile(input)).to match(/color: 'red'/)
end

it 'includes and imports "nib" automatically' do
Stylus.nib = true
input, output = fixture(:nib)
Expand Down

0 comments on commit 49859ac

Please sign in to comment.