Skip to content

Commit

Permalink
Sass-only support
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Nov 14, 2013
1 parent 263dc71 commit c58415e
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 86 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ require 'bootstrap-sass'
```

```console
compass install bootstrap
bundle exec compass install bootstrap
```

If you are creating a new Compass project, you can generate it with bootstrap-sass support:

```console
compass create my-new-project -r bootstrap-sass --using bootstrap
bundle exec compass create my-new-project -r bootstrap-sass --using bootstrap
```

This will create a new Compass project with the following files in it:
Expand All @@ -51,6 +51,43 @@ This will create a new Compass project with the following files in it:
* [styles.scss](/templates/project/styles.scss) - main project SCSS file, import `variables` and `bootstrap`.


### c. Sass-only (no Compass, nor Rails)

Require the gem, and load paths and Sass helpers will be configured automatically:

```ruby
require 'bootstrap-sass'
```

#### JS and fonts

If you are using Rails or Sprockets, see Usage.

If none of Rails/Sprockets/Compass were detected the fonts will be referenced as:

```sass
"#{$icon-font-path}/#{$icon-font-name}.eot"
```

`$icon-font-path` defaults to `bootstrap/`.

When not using an asset pipeline, you have to copy fonts and javascripts from the gem.

```bash
mkdir public/fonts
cp -r $(bundle show bootstrap-sass)/vendor/assets/fonts/ public/fonts/
mkdir public/javascripts
cp -r $(bundle show bootstrap-sass)/vendor/assets/javascripts/ public/javascripts/
```

In ruby you can get the assets' location in the filesystem like this:

```ruby
Bootstrap.stylesheets_path
Bootstrap.fonts_path
Bootstrap.javascripts_path
```

## Usage

### Sass
Expand Down
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ end
desc 'Dumps output to a CSS file for testing'
task :debug do
require 'sass'
require './lib/bootstrap-sass/compass_functions'
require './lib/bootstrap-sass/sass_functions'
path = './vendor/assets/stylesheets'
path = Bootstrap.stylesheets_path
%w(bootstrap).each do |file|
engine = Sass::Engine.for_file("#{path}/#{file}.scss", syntax: :scss, load_paths: [path])
File.open("./#{file}.css", 'w') { |f| f.write(engine.render) }
Expand All @@ -29,7 +28,6 @@ task :compile, :css_path do |t, args|
lib_path = File.join(File.dirname(__FILE__), 'lib')
$:.unshift(lib_path) unless $:.include?(lib_path)
require 'sass'
require 'bootstrap-sass/compass_functions'
require 'bootstrap-sass/sass_functions'
require 'term/ansicolor'

Expand Down
53 changes: 29 additions & 24 deletions lib/bootstrap-sass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,49 @@ class FrameworkNotFound < StandardError; end
class << self
# Inspired by Kaminari
def load!
if compass?
require 'bootstrap-sass/compass_functions'
register_compass_extension
elsif asset_pipeline?
require 'bootstrap-sass/sass_functions'
end
require 'bootstrap-sass/sass_functions'
register_compass_extension if compass?

if rails?
require 'sass-rails'
register_rails_engine
end

unless rails? || compass?
raise Bootstrap::FrameworkNotFound,
'bootstrap-sass requires either Rails > 3.1 or Compass, neither of which are loaded'
end

configure_sass
end

# Paths
def gem_path
@gem_path ||= File.expand_path '..', File.dirname(__FILE__)
end

def stylesheets_path
@stylesheets_path ||= File.join gem_path, 'vendor', 'assets', 'stylesheets'
File.join assets_path, 'stylesheets'
end

def fonts_path
File.join assets_path, 'fonts'
end

def javascripts_path
File.join assets_path, 'javascripts'
end

def assets_path
@assets_path ||= File.join gem_path, 'vendor', 'assets'
end

# Environment detection helpers
def asset_pipeline?
defined?(::Sprockets)
end

def compass?
defined?(::Compass)
end

def rails?
defined?(::Rails)
end

private
Expand All @@ -53,18 +70,6 @@ def register_compass_extension
def register_rails_engine
require 'bootstrap-sass/engine'
end

def asset_pipeline?
defined?(::Sprockets)
end

def compass?
defined?(::Compass)
end

def rails?
defined?(::Rails)
end
end
end

Expand Down
24 changes: 0 additions & 24 deletions lib/bootstrap-sass/compass_functions.rb

This file was deleted.

40 changes: 38 additions & 2 deletions lib/bootstrap-sass/sass_functions.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
require 'sass'
require 'bootstrap-sass'

module Sass::Script::Functions
def twbs_font_path(source)
twbs_asset_path source, :font
end
declare :twbs_font_path, [:source]

def twbs_image_path(source)
twbs_asset_path source, :image
end
declare :twbs_image_path, [:source]

def twbs_asset_path(source, type)
url = if Bootstrap.asset_pipeline? && (context = sprockets_context)
context.send(:"#{type}_path", source.value)
elsif Bootstrap.compass?
send(:"#{type}_url", source, Sass::Script::Bool.new(true)).value
end

# sass-only
url ||= source.value.gsub('"', '')
Sass::Script::String.new(url, :string)
end
declare :twbs_asset_path, [:source]

# LARS: Snatched from compass - 2011-11-29 - used for gradients in IE6-9
# returns an IE hex string for a color with an alpha channel
# suitable for passing to IE filters.
def ie_hex_str(color)
def twbs_ie_hex_str(color)
assert_type color, :Color
alpha = (color.alpha * 255).round
alphastr = alpha.to_s(16).rjust(2, '0')
Sass::Script::String.new("##{alphastr}#{color.send(:hex_str)[1..-1]}".upcase)
end
declare :ie_hex_str, [:color]
declare :twbs_ie_hex_str, [:color]

protected

def sprockets_context
# Modern way to get context:
if options.key?(:sprockets)
options[:sprockets][:context]
# Compatibility with sprockets pre 2.10.0:
elsif (importer = options[:importer]) && importer.respond_to?(:context)
importer.context
end
end
end
2 changes: 1 addition & 1 deletion lib/bootstrap-sass/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Bootstrap
VERSION = '3.0.2.1'
BOOTSTRAP_SHA = '4cbc8d49b10f707029019aaa5eba50e56390a3c5'
BOOTSTRAP_SHA = '27b62d39e2ba62e2d70ef2f5322edd584adc726e'
end
14 changes: 10 additions & 4 deletions tasks/converter/less_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# This module transforms LESS into SCSS.
# It is implemented via lots of string manipulation: scanning back and forwards for regexps and doing substitions.
# Since it does not parse the LESS into an AST, bits of it may assume LESS to be formatted a certain way, and only limited,
# static analysis can be performed. This approach has so far been enough to automatically convert all of twbs/bootstrap.
# static analysis can be performed. This approach has so far been mostly enough to automatically convert most all of twbs/bootstrap.
# There is some bootstrap-specific to make up for lack of certain features in Sass 3.2 (recursion, mixin namespacing)
# and vice versa in LESS (vararg mixins).
class Converter
module LessConversion
# Some regexps for matching bits of SCSS:
Expand Down Expand Up @@ -85,7 +87,7 @@ def process_stylesheet_assets
when 'glyphicons.less'
file = replace_rules(file, '@font-face') { |rule|
rule = replace_all rule, /(\$icon-font-\w+)/, '#{\1}'
replace_all rule, /url\(/, 'font-url('
replace_asset_url rule, :font
}
end

Expand Down Expand Up @@ -117,6 +119,10 @@ def convert_to_scss(file)
file
end

def replace_asset_url(rule, type)
replace_all rule, /url\((.*?)\)/, "url(twbs-#{type}-path(\\1))"
end

# convert grid mixins LESS when => SASS @if
def convert_grid_mixins(file)
file = replace_rules file, /@mixin make-grid-columns/, comments: false do |css, pos|
Expand Down Expand Up @@ -349,7 +355,7 @@ def replace_ms_filters(file)
log_transform
file.gsub(
/filter: e\(%\("progid:DXImageTransform.Microsoft.gradient\(startColorstr='%d', endColorstr='%d', GradientType=(\d)\)",argb\(([\-$\w]+)\),argb\(([\-$\w]+)\)\)\);/,
%Q(filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='\#{ie-hex-str(\\2)}', endColorstr='\#{ie-hex-str(\\3)}', GradientType=\\1);)
%Q(filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='\#{twbs-ie-hex-str(\\2)}', endColorstr='\#{twbs-ie-hex-str(\\3)}', GradientType=\\1);)
)
end

Expand Down Expand Up @@ -391,7 +397,7 @@ def replace_spin(less)
end

def replace_image_urls(less)
less.gsub(/background-image: url\("?(.*?)"?\);/) { |s| "background-image: image-url(\"#{$1}\");" }
less.gsub(/background-image: url\("?(.*?)"?\);/) { |s| replace_asset_url s, :image }
end

def replace_image_paths(less)
Expand Down
13 changes: 11 additions & 2 deletions templates/project/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
stylesheet 'styles.scss'

# SCSS:
bs_stylesheets = "../../vendor/assets/stylesheets/bootstrap"

assets = "../../vendor/assets"

bs_stylesheets = "#{assets}/stylesheets/bootstrap"
stylesheet '_variables.scss.erb', :to => '_variables.scss', :erb => true,
:bs_variables_path => File.expand_path("#{bs_stylesheets}/_variables.scss", File.dirname(__FILE__))

# JS:
bs_javascripts = "../../vendor/assets/javascripts/bootstrap"
bs_javascripts = "#{assets}/javascripts/bootstrap"
Dir.glob File.expand_path("#{bs_javascripts}/*.js", File.dirname(__FILE__)) do |path|
file = File.basename(path)
javascript "#{bs_javascripts}/#{file}", :to => "bootstrap/#{file}"
end

bs_fonts = "#{assets}/fonts/bootstrap"
Dir.glob File.expand_path("#{bs_fonts}/*", File.dirname(__FILE__)) do |path|
file = File.basename(path)
font "#{bs_fonts}/#{file}", :to => "bootstrap/#{file}"
end
4 changes: 4 additions & 0 deletions test/dummy_sass_only/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'sass', '~> 3.2'
gem 'bootstrap-sass', path: '../..'
13 changes: 13 additions & 0 deletions test/dummy_sass_only/compile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'sass'
require 'bootstrap-sass'

css = Sass.compile(
File.read(File.expand_path('./import_all.sass', File.dirname(__FILE__))),
:syntax => 'sass'
)

if ARGV[0]
File.open(ARGV[0], 'w') { |f| f.write css }
else
puts css
end
2 changes: 2 additions & 0 deletions test/dummy_sass_only/import_all.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import 'bootstrap'
@import 'bootstrap/theme'
23 changes: 23 additions & 0 deletions test/sass_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class SassTest < Test::Unit::TestCase
DUMMY_PATH = 'test/dummy_sass_only'

def test_font_helper
assert_match %r(url\(['"]?.*eot['"]?\)), @css
end

def setup
Dir.chdir DUMMY_PATH do
%x[bundle]
end
css_path = File.join Bootstrap.gem_path, 'tmp/bootstrap-sass-only.css'
command = "bundle exec ruby compile.rb #{css_path}"
Dir.chdir DUMMY_PATH do
assert silence_stream(STDOUT) {
system(command)
}, 'Sass-only compilation failed'
end
@css = File.read(css_path)
end
end
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'test/unit'

require 'sass'
require 'lib/bootstrap-sass/compass_functions'
require 'lib/bootstrap-sass/sass_functions'

require 'rails/test_help'
Expand Down
10 changes: 5 additions & 5 deletions vendor/assets/stylesheets/bootstrap/_glyphicons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
// Import the fonts
@font-face {
font-family: 'Glyphicons Halflings';
src: font-url('#{$icon-font-path}#{$icon-font-name}.eot');
src: font-url('#{$icon-font-path}#{$icon-font-name}.eot?#iefix') format('embedded-opentype'),
font-url('#{$icon-font-path}#{$icon-font-name}.woff') format('woff'),
font-url('#{$icon-font-path}#{$icon-font-name}.ttf') format('truetype'),
font-url('#{$icon-font-path}#{$icon-font-name}.svg#glyphicons_halflingsregular') format('svg');
src: url(twbs-font-path('#{$icon-font-path}#{$icon-font-name}.eot'));
src: url(twbs-font-path('#{$icon-font-path}#{$icon-font-name}.eot?#iefix')) format('embedded-opentype'),
url(twbs-font-path('#{$icon-font-path}#{$icon-font-name}.woff')) format('woff'),
url(twbs-font-path('#{$icon-font-path}#{$icon-font-name}.ttf')) format('truetype'),
url(twbs-font-path('#{$icon-font-path}#{$icon-font-name}.svg#glyphicons_halflingsregular')) format('svg');
}

// Catchall baseclass
Expand Down
Loading

0 comments on commit c58415e

Please sign in to comment.