2013-07-30 22:08:49 +02:00
|
|
|
# coding: utf-8
|
2013-05-23 14:51:56 -07:00
|
|
|
# Based on convert script from vwall/compass-twitter-bootstrap gem.
|
|
|
|
# https://github.com/vwall/compass-twitter-bootstrap/blob/master/build/convert.rb
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this work except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License in the LICENSE file, or at:
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
require 'open-uri'
|
|
|
|
require 'json'
|
2013-07-31 02:27:29 +02:00
|
|
|
require 'strscan'
|
2013-08-04 23:22:54 +02:00
|
|
|
require 'forwardable'
|
|
|
|
require 'term/ansicolor'
|
2013-08-05 00:44:05 +02:00
|
|
|
require 'fileutils'
|
2013-05-23 14:51:56 -07:00
|
|
|
|
|
|
|
class Converter
|
2013-08-04 23:22:54 +02:00
|
|
|
extend Forwardable
|
|
|
|
|
2013-08-03 09:22:54 +02:00
|
|
|
GIT_DATA = 'https://api.github.com/repos'
|
2013-08-04 23:22:54 +02:00
|
|
|
GIT_RAW = 'https://raw.github.com'
|
|
|
|
|
2013-06-13 13:58:34 -07:00
|
|
|
def initialize(branch)
|
2013-08-03 09:22:54 +02:00
|
|
|
@repo = 'twbs/bootstrap'
|
2013-08-03 09:31:57 +02:00
|
|
|
@repo_url = "https://github.com/#@repo"
|
2013-08-03 09:22:54 +02:00
|
|
|
@branch = branch || 'master'
|
|
|
|
@branch_sha = get_branch_sha
|
|
|
|
@mixins = get_mixins_name
|
2013-08-05 00:44:05 +02:00
|
|
|
@save_at = { js: 'vendor/assets/javascripts/bootstrap', scss: 'vendor/assets/stylesheets/bootstrap' }
|
|
|
|
@save_at.each { |_,v| FileUtils.mkdir_p(v) }
|
|
|
|
@logger = Logger.new(repo: @repo_url, branch: @branch, branch_sha: @branch_sha, save_at: @save_at)
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
2013-08-05 00:21:17 +02:00
|
|
|
def_delegators :@logger, :log_status, :log_downloading, :log_processing, :log_transform, :log_processed
|
2013-08-04 23:22:54 +02:00
|
|
|
|
2013-05-23 14:51:56 -07:00
|
|
|
def process
|
2013-05-24 16:59:48 -07:00
|
|
|
process_stylesheet_assets
|
|
|
|
process_javascript_assets
|
2013-08-03 09:22:54 +02:00
|
|
|
store_version
|
2013-05-24 16:59:48 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_stylesheet_assets
|
2013-08-04 23:22:54 +02:00
|
|
|
log_status "Processing stylesheets..."
|
2013-07-30 22:08:49 +02:00
|
|
|
read_files('less', bootstrap_less_files).each do |name, file|
|
2013-08-05 00:21:17 +02:00
|
|
|
log_processing name
|
2013-06-15 16:30:44 -07:00
|
|
|
case name
|
|
|
|
when 'bootstrap.less'
|
|
|
|
file = replace_file_imports(file)
|
|
|
|
when 'mixins.less'
|
|
|
|
file = replace_vars(file)
|
|
|
|
file = replace_escaping(file)
|
|
|
|
file = replace_mixin_file(file)
|
|
|
|
file = replace_mixins(file)
|
2013-07-31 18:45:17 +02:00
|
|
|
file = flatten_mixins(file, '#gradient', 'gradient')
|
2013-07-31 17:04:30 +02:00
|
|
|
file = parameterize_mixin_parent_selector(file, 'responsive-(in)?visibility')
|
2013-08-04 23:37:34 +02:00
|
|
|
file = replace_ms_filters(file)
|
2013-07-31 18:02:23 +02:00
|
|
|
when 'responsive-utilities.less'
|
|
|
|
file = convert_to_scss(file)
|
2013-07-31 18:06:22 +02:00
|
|
|
file = apply_mixin_parent_selector(file, '\.(visible|hidden)')
|
2013-06-15 16:30:44 -07:00
|
|
|
when 'utilities.less'
|
|
|
|
file = replace_mixin_file(file)
|
|
|
|
file = convert_to_scss(file)
|
|
|
|
when 'variables.less'
|
|
|
|
file = convert_to_scss(file)
|
|
|
|
file = insert_default_vars(file)
|
2013-07-31 18:45:17 +02:00
|
|
|
when 'close.less'
|
|
|
|
file = convert_to_scss(file)
|
|
|
|
# extract .close { button& {...} } rule
|
2013-07-31 19:10:50 +02:00
|
|
|
file = extract_nested_rule(file, '\s*button&', 'button.close')
|
2013-08-01 01:29:45 +02:00
|
|
|
when 'forms.less'
|
|
|
|
file = convert_to_scss(file)
|
|
|
|
file = extract_nested_rule(file, '\s*textarea&', 'textarea.form-control')
|
2013-06-15 16:30:44 -07:00
|
|
|
else
|
|
|
|
file = convert_to_scss(file)
|
|
|
|
end
|
|
|
|
|
2013-08-05 00:44:05 +02:00
|
|
|
name = name.sub(/\.less$/, '.scss')
|
|
|
|
save_at = @save_at[:scss]
|
|
|
|
path = "#{save_at}/#{'_' unless name == 'bootstrap.scss'}#{name}"
|
2013-06-15 16:30:44 -07:00
|
|
|
save_file(path, file)
|
2013-08-05 00:44:05 +02:00
|
|
|
log_processed File.basename(path)
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-03 09:22:54 +02:00
|
|
|
def store_version
|
|
|
|
path = 'lib/bootstrap-sass/version.rb'
|
2013-08-04 03:55:35 +02:00
|
|
|
content = File.read(path).sub(/BOOTSTRAP_SHA\s*=\s*['"][\w]+['"]/, "BOOTSTRAP_SHA = '#@branch_sha'")
|
2013-08-03 09:22:54 +02:00
|
|
|
File.open(path, 'w') { |f| f.write(content) }
|
|
|
|
end
|
|
|
|
|
2013-05-24 16:59:48 -07:00
|
|
|
def process_javascript_assets
|
2013-08-04 23:22:54 +02:00
|
|
|
log_status "Processing javascripts..."
|
2013-08-05 00:44:05 +02:00
|
|
|
save_at = @save_at[:js]
|
2013-07-30 22:08:49 +02:00
|
|
|
read_files('js', bootstrap_js_files).each do |name, file|
|
2013-08-05 00:21:17 +02:00
|
|
|
save_file("#{save_at}/#{name}", file)
|
2013-05-24 16:59:48 -07:00
|
|
|
end
|
2013-08-05 00:44:05 +02:00
|
|
|
log_processed "#{bootstrap_js_files * ' '}"
|
2013-05-24 16:59:48 -07:00
|
|
|
|
2013-08-05 01:46:33 +02:00
|
|
|
log_status "Updating javascript manifest"
|
2013-05-24 16:59:48 -07:00
|
|
|
content = ''
|
|
|
|
bootstrap_js_files.each do |name|
|
|
|
|
name = name.gsub(/\.js$/, '')
|
|
|
|
content << "//= require bootstrap/#{name}\n"
|
|
|
|
end
|
|
|
|
path = "vendor/assets/javascripts/bootstrap.js"
|
|
|
|
save_file(path, content)
|
2013-08-05 00:21:17 +02:00
|
|
|
log_processed path
|
2013-05-24 16:59:48 -07:00
|
|
|
end
|
|
|
|
|
2013-08-03 00:22:57 +02:00
|
|
|
private
|
2013-05-23 14:51:56 -07:00
|
|
|
|
2013-07-30 22:08:49 +02:00
|
|
|
def read_files(path, files)
|
2013-08-04 23:22:54 +02:00
|
|
|
contents = {}
|
2013-08-03 09:22:54 +02:00
|
|
|
full_path = "#{GIT_RAW}/#@repo/#@branch_sha/#{path}"
|
2013-08-05 00:21:17 +02:00
|
|
|
log_downloading files
|
2013-07-30 22:08:49 +02:00
|
|
|
files.map do |name|
|
|
|
|
Thread.start {
|
2013-08-03 00:22:57 +02:00
|
|
|
content = open("#{full_path}/#{name}").read
|
2013-07-30 22:08:49 +02:00
|
|
|
Thread.exclusive {
|
|
|
|
contents[name] = content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end.each(&:join)
|
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2013-08-03 09:22:54 +02:00
|
|
|
# get sha of the branch (= the latest commit)
|
|
|
|
def get_branch_sha
|
2013-08-03 09:31:57 +02:00
|
|
|
@branch_sha ||= %x[git ls-remote '#@repo_url' | awk '/#@branch/ {print $1}'].chomp
|
2013-08-03 09:22:54 +02:00
|
|
|
end
|
|
|
|
|
2013-05-24 16:59:48 -07:00
|
|
|
# Get the sha of a dir
|
|
|
|
def get_tree_sha(dir)
|
2013-08-03 09:22:54 +02:00
|
|
|
get_trees['tree'].find { |t| t['path'] == dir }['sha']
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_trees
|
2013-08-03 09:31:57 +02:00
|
|
|
@trees ||= get_json("#{GIT_DATA}/#@repo/git/trees/#@branch_sha")
|
2013-05-24 16:59:48 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def bootstrap_less_files
|
2013-07-30 22:08:49 +02:00
|
|
|
@bootstrap_less_files ||= begin
|
2013-08-03 09:31:57 +02:00
|
|
|
files = get_json "#{GIT_DATA}/#@repo/git/trees/#{get_tree_sha('less')}"
|
2013-07-30 22:08:49 +02:00
|
|
|
files['tree'].select{|f| f['type'] == 'blob' && f['path'] =~ /.less$/ }.map{|f| f['path'] }
|
|
|
|
end
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
2013-05-24 16:59:48 -07:00
|
|
|
def bootstrap_js_files
|
2013-07-30 22:08:49 +02:00
|
|
|
@bootstrap_js_files ||= begin
|
2013-08-03 09:31:57 +02:00
|
|
|
files = get_json "#{GIT_DATA}/#@repo/git/trees/#{get_tree_sha('js')}"
|
2013-07-30 22:08:49 +02:00
|
|
|
files = files['tree'].select { |f| f['type'] == 'blob' && f['path'] =~ /.js$/ }.map { |f| f['path'] }
|
|
|
|
files.sort_by { |f|
|
|
|
|
case f
|
|
|
|
# tooltip depends on popover and must be loaded earlier
|
2013-08-03 09:31:57 +02:00
|
|
|
when /tooltip/ then 1
|
|
|
|
when /popover/ then 2
|
2013-07-30 22:08:49 +02:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_mixins_name
|
|
|
|
mixins = []
|
2013-08-03 09:22:54 +02:00
|
|
|
less_mixins = open("#{GIT_RAW}/#@repo/#@branch_sha/less/mixins.less").read
|
2013-05-23 14:51:56 -07:00
|
|
|
|
|
|
|
less_mixins.scan(/\.([\w-]+)\(.*\)\s?{?/) do |mixin|
|
2013-07-28 18:14:12 +02:00
|
|
|
mixins << mixin.first
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
mixins
|
|
|
|
end
|
|
|
|
|
2013-05-24 16:59:48 -07:00
|
|
|
def convert_to_scss(file)
|
2013-05-23 14:51:56 -07:00
|
|
|
file = replace_vars(file)
|
|
|
|
file = replace_mixins(file)
|
|
|
|
file = replace_less_extend(file)
|
|
|
|
file = replace_spin(file)
|
|
|
|
file = replace_image_urls(file)
|
|
|
|
file = replace_image_paths(file)
|
|
|
|
file = replace_escaping(file)
|
|
|
|
file = convert_less_ampersand(file)
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
2013-05-24 16:59:48 -07:00
|
|
|
def save_file(path, content, mode='w')
|
|
|
|
File.open(path, mode) { |file| file.write(content) }
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
2013-06-15 16:30:44 -07:00
|
|
|
def replace_file_imports(less)
|
|
|
|
less.gsub(/@import ["|']([\w-]+).less["|'];/, '@import "bootstrap/\1";');
|
|
|
|
end
|
|
|
|
|
2013-07-31 17:04:30 +02:00
|
|
|
# @mixin a() { tr& { color:white } }
|
|
|
|
# to:
|
|
|
|
# @mixin a($parent) { tr#{$parent} { color: white } }
|
|
|
|
def parameterize_mixin_parent_selector(file, rule_sel)
|
2013-08-04 23:37:34 +02:00
|
|
|
log_transform rule_sel
|
2013-07-31 17:04:30 +02:00
|
|
|
param = '$parent'
|
2013-08-03 00:22:57 +02:00
|
|
|
replace_rules(file, '^[ \t]*@mixin\s*' + rule_sel) do |mxn_css|
|
|
|
|
mxn_css.sub! /(?=@mixin)/, "// [converter] $parent hack\n"
|
|
|
|
# insert param into mixin def
|
2013-07-31 17:04:30 +02:00
|
|
|
mxn_css.sub! /(@mixin [\w-]+\()/, "\\1#{param}"
|
2013-08-03 00:22:57 +02:00
|
|
|
# wrap properties in #{$parent} { ... }
|
|
|
|
replace_properties(mxn_css) { |props| " \#{#{param}} { #{props.strip} }\n " }
|
|
|
|
# change nested& rules to nested#{$parent}
|
|
|
|
replace_rules(mxn_css, /.*[^\s ]&/) { |rule| replace_in_selector rule, /&/, "\#{#{param}}" }
|
2013-07-31 17:04:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-31 19:10:50 +02:00
|
|
|
# extracts rule immediately after it's parent and optionally changes selector to new_selector
|
|
|
|
def extract_nested_rule(css, selector, new_selector = selector)
|
2013-08-04 23:37:34 +02:00
|
|
|
log_transform selector, new_selector
|
2013-08-03 00:22:57 +02:00
|
|
|
matches = []
|
|
|
|
# first find the rules, and remove them
|
|
|
|
css = replace_rules(css, selector, comments: true) { |rule, pos|
|
|
|
|
matches << [rule, pos]
|
|
|
|
indent "// [converter] extracted #{get_selector(rule)} to #{new_selector}", indent_width(rule)
|
|
|
|
}
|
2013-07-31 19:10:50 +02:00
|
|
|
# replace rule selector with new_selector
|
2013-08-03 00:22:57 +02:00
|
|
|
matches.each do |m|
|
|
|
|
m[0].sub! /(#{COMMENT_RE}*)^(\s*).*?(\s*){/m, "\\1\\2#{new_selector}\\3{"
|
|
|
|
end
|
|
|
|
replace_substrings_at css,
|
|
|
|
matches.map { |_, pos| pos.begin + css[pos.begin..-1].index('}') + 1 },
|
|
|
|
matches.map { |rule, _| "\n\n" + unindent(rule) }
|
2013-07-31 19:10:50 +02:00
|
|
|
end
|
|
|
|
|
2013-07-31 18:06:22 +02:00
|
|
|
# .visible-sm { @include responsive-visibility() }
|
|
|
|
# to:
|
|
|
|
# @include responsive-visibility('.visible-sm')
|
|
|
|
def apply_mixin_parent_selector(file, rule_sel)
|
2013-08-04 23:37:34 +02:00
|
|
|
log_transform rule_sel
|
2013-07-31 18:06:22 +02:00
|
|
|
replace_rules file, "(\s*)#{rule_sel}" do |rule|
|
|
|
|
next rule unless rule =~ /@include/
|
|
|
|
rule =~ /\A\s+/ # keep indentation
|
2013-08-03 00:22:57 +02:00
|
|
|
$~.to_s + rule.sub(/(#{COMMENT_RE}*)(#{SELECTOR_RE})\{(.*)\}/m, '\3').sub(/(@include [\w-]+\()/, "#{$1}\\1'#{$2.strip}'").strip
|
2013-07-31 18:06:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-31 02:27:29 +02:00
|
|
|
# #gradient > { @mixin horizontal ... }
|
|
|
|
# to:
|
|
|
|
# @mixin gradient-horizontal
|
2013-07-31 18:45:17 +02:00
|
|
|
def flatten_mixins(file, container, prefix)
|
2013-08-04 23:37:34 +02:00
|
|
|
log_transform container, prefix
|
2013-07-31 16:02:19 +02:00
|
|
|
replace_rules file, Regexp.escape(container) do |mixins_css|
|
2013-08-03 00:22:57 +02:00
|
|
|
unindent unwrap_rule_block(mixins_css).gsub(/@mixin\s*([\w-]+)/, "@mixin #{prefix}-\\1")
|
2013-07-31 02:27:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-28 20:50:03 -07:00
|
|
|
# Replaces the following:
|
2013-08-01 01:29:45 +02:00
|
|
|
# .mixin() -> @include mixin()
|
|
|
|
# #scope > .mixin() -> @include scope-mixin()
|
2013-05-23 14:51:56 -07:00
|
|
|
def replace_mixins(less)
|
2013-07-28 18:01:43 +02:00
|
|
|
mixin_pattern = /(\s+)(([#|\.][\w-]+\s*>\s*)*)\.([\w-]+\(.*\))/
|
2013-05-28 20:50:03 -07:00
|
|
|
less.gsub(mixin_pattern) do |match|
|
|
|
|
matches = match.scan(mixin_pattern).flatten
|
2013-08-04 23:22:54 +02:00
|
|
|
scope = matches[1] || ''
|
2013-05-28 20:50:03 -07:00
|
|
|
if scope != ''
|
2013-07-16 23:29:41 -07:00
|
|
|
scope = scope.scan(/[\w-]+/).join('-') + '-'
|
2013-05-28 20:50:03 -07:00
|
|
|
end
|
2013-08-03 00:22:57 +02:00
|
|
|
mixin_name = match.scan(/\.([\w-]+)\(.*\)\s?\{?/).first
|
2013-07-28 18:14:12 +02:00
|
|
|
|
|
|
|
if mixin_name && @mixins.include?(mixin_name.first)
|
2013-08-03 00:22:57 +02:00
|
|
|
"#{matches.first}@include #{scope}#{matches.last}".gsub(/; \$/, ", $")
|
2013-07-28 18:14:12 +02:00
|
|
|
else
|
|
|
|
"#{matches.first}@extend .#{scope}#{matches.last.gsub(/\(\)/, '')}"
|
|
|
|
end
|
2013-05-28 20:50:03 -07:00
|
|
|
end
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
2013-08-04 23:37:34 +02:00
|
|
|
# change Microsoft filters to SASS calling convention
|
|
|
|
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);)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-01 01:58:26 +02:00
|
|
|
# unwraps topmost rule block
|
|
|
|
# #sel { a: b; }
|
|
|
|
# to:
|
|
|
|
# a: b;
|
2013-08-01 02:01:02 +02:00
|
|
|
def unwrap_rule_block(css)
|
2013-08-03 00:22:57 +02:00
|
|
|
replace_in_selector(css, /.*/, '').sub(/}\s*\z/m, '')
|
2013-08-01 01:58:26 +02:00
|
|
|
end
|
|
|
|
|
2013-05-23 14:51:56 -07:00
|
|
|
def replace_mixin_file(less)
|
2013-07-30 18:02:33 +02:00
|
|
|
less.gsub(/^(\s*)\.([\w-]+\(.*\))(\s*\{)/) { |match|
|
|
|
|
"#{$1}@mixin #{$2.tr(';', ',')}#{$3}"
|
|
|
|
}
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def replace_vars(less)
|
2013-07-30 17:07:44 +02:00
|
|
|
less = less.gsub(/(?!@media|@page|@keyframes|@font-face|@-\w)@/, '$')
|
|
|
|
# variables that would be ignored by gsub above: e.g. @page-header-border-color
|
2013-07-30 17:10:41 +02:00
|
|
|
less.gsub! /@(page[\w-]+)/, '$\1'
|
2013-07-30 17:07:44 +02:00
|
|
|
less
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
2013-08-01 01:29:45 +02:00
|
|
|
# #gradient > .horizontal()
|
|
|
|
# to:
|
|
|
|
# @include .horizontal-gradient()
|
2013-05-23 14:51:56 -07:00
|
|
|
def replace_less_extend(less)
|
|
|
|
less.gsub(/\#(\w+) \> \.([\w-]*)(\(.*\));?/, '@include \1-\2\3;')
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_spin(less)
|
2013-08-01 16:02:46 +02:00
|
|
|
less.gsub(/(?![\-$@.])spin(?!-)/, 'adjust-hue')
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def replace_image_urls(less)
|
|
|
|
less.gsub(/background-image: url\("?(.*?)"?\);/) {|s| "background-image: image-url(\"#{$1}\");" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_image_paths(less)
|
|
|
|
less.gsub('../img/', '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_escaping(less)
|
2013-06-13 12:09:18 -07:00
|
|
|
less = less.gsub(/\~"([^"]+)"/, '#{\1}') # Get rid of ~"" escape
|
2013-08-03 01:58:43 +02:00
|
|
|
less.gsub!(/\$\{([^}]+)\}/, '$\1') # Get rid of @{} escape
|
|
|
|
less.gsub!(/"([^"\n]*)(\$[\w\-]+)([^"\n]*)"/, '"\1#{\2}\3"') # interpolate variable in string, e.g. url("$file-1x") => url("#{$file-1x}")
|
2013-07-28 18:21:05 +02:00
|
|
|
less.gsub(/(\W)e\(%\("?([^"]*)"?\)\)/, '\1\2') # Get rid of e(%("")) escape
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def insert_default_vars(scss)
|
2013-06-13 12:29:03 -07:00
|
|
|
scss.gsub(/^(\$.+);/, '\1 !default;')
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
# Converts &-
|
|
|
|
def convert_less_ampersand(less)
|
|
|
|
regx = /^\.badge\s*\{[\s\/\w\(\)]+(&{1}-{1})\w.*?^}$/m
|
|
|
|
|
|
|
|
tmp = ''
|
|
|
|
less.scan(/^(\s*&)(-[\w\[\]]+\s*{.+})$/) do |ampersand, css|
|
|
|
|
tmp << ".badge#{css}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
less.gsub(regx, tmp)
|
|
|
|
end
|
2013-07-31 02:27:29 +02:00
|
|
|
|
|
|
|
# unindent by n spaces
|
|
|
|
def unindent(txt, n = 2)
|
2013-08-03 00:22:57 +02:00
|
|
|
txt.gsub /^[ ]{#{n}}/, ''
|
|
|
|
end
|
|
|
|
|
|
|
|
# indent by n spaces
|
|
|
|
def indent(txt, n = 2)
|
|
|
|
"#{' ' * n}#{txt}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# get indent length from the first line of txt
|
|
|
|
def indent_width(txt)
|
|
|
|
txt.match(/\A\s*/).to_s.length
|
|
|
|
end
|
|
|
|
|
|
|
|
# get full selector for rule_block
|
|
|
|
def get_selector(rule_block)
|
|
|
|
/^\s*(#{SELECTOR_RE}?)\s*\{/.match(rule_block) && $1 && $1.strip
|
2013-07-31 02:27:29 +02:00
|
|
|
end
|
|
|
|
|
2013-08-01 01:58:26 +02:00
|
|
|
# replace CSS rule blocks matching rule_prefix with yield(rule_block, rule_pos)
|
|
|
|
# will also include immediately preceding comments in rule_block
|
|
|
|
#
|
|
|
|
# option :comments -- include immediately preceding comments in rule_block
|
2013-07-31 02:27:29 +02:00
|
|
|
#
|
|
|
|
# replace_rules(".a{ \n .b{} }", '.b') { |rule| ">#{rule}<" } #=> ".a{ \n >.b{}< }"
|
2013-08-03 00:22:57 +02:00
|
|
|
def replace_rules(less, rule_prefix = SELECTOR_RE, options = {})
|
2013-08-01 01:58:26 +02:00
|
|
|
options = {comments: true}.merge(options || {})
|
2013-08-03 00:22:57 +02:00
|
|
|
less = less.dup
|
|
|
|
s = StringScanner.new(less)
|
|
|
|
rule_re = "#{rule_prefix}[^{]*#{RULE_OPEN_BRACE_RE}"
|
2013-08-01 01:58:26 +02:00
|
|
|
if options[:comments]
|
2013-08-03 00:22:57 +02:00
|
|
|
rule_start_re = /#{COMMENT_RE}*^#{rule_re}/
|
2013-08-01 01:58:26 +02:00
|
|
|
else
|
2013-08-03 00:22:57 +02:00
|
|
|
rule_start_re = /^#{rule_re}/
|
2013-08-01 01:58:26 +02:00
|
|
|
end
|
2013-08-03 00:22:57 +02:00
|
|
|
|
2013-07-31 02:27:29 +02:00
|
|
|
while (rule_start = scan_next(s, rule_start_re))
|
2013-08-03 00:22:57 +02:00
|
|
|
rule_pos = (s.pos - rule_start.length..next_brace_pos(less, s.pos - 1))
|
|
|
|
less[rule_pos] = yield(less[rule_pos], rule_pos)
|
2013-07-31 02:27:29 +02:00
|
|
|
end
|
|
|
|
less
|
|
|
|
end
|
|
|
|
|
2013-07-31 17:04:30 +02:00
|
|
|
# replace in the top-level selector
|
|
|
|
# replace_in_selector('a {a: {a: a} } a {}', /a/, 'b') => 'b {a: {a: a} } b {}'
|
|
|
|
def replace_in_selector(css, pattern, sub)
|
|
|
|
# scan for selector positions in css
|
2013-08-03 00:22:57 +02:00
|
|
|
s = StringScanner.new(css)
|
2013-07-31 17:04:30 +02:00
|
|
|
prev_pos = 0
|
2013-08-03 00:22:57 +02:00
|
|
|
sel_pos = []
|
|
|
|
while (brace = scan_next(s, /#{RULE_OPEN_BRACE_RE}/))
|
2013-07-31 17:04:30 +02:00
|
|
|
sel_pos << (prev_pos .. s.pos - 1)
|
2013-08-03 00:22:57 +02:00
|
|
|
s.pos = next_brace_pos(css, s.pos - 1) + 1
|
2013-07-31 17:04:30 +02:00
|
|
|
prev_pos = s.pos
|
|
|
|
end
|
2013-08-03 00:22:57 +02:00
|
|
|
replace_substrings_at(css, sel_pos) { |s| s.gsub(pattern, sub) }
|
2013-07-31 17:04:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-08-03 00:22:57 +02:00
|
|
|
sel_chars = '\[\]$\w\-{}#,.:&>@'
|
|
|
|
SELECTOR_RE = /[#{sel_chars}]+[#{sel_chars}\s]*/
|
|
|
|
COMMENT_RE = %r((?:^[ \t]*//[^\n]*\n))
|
|
|
|
RULE_OPEN_BRACE_RE = /(?<!#)\{/
|
|
|
|
RULE_CLOSE_BRACE_RE = /(?<!\w)\}/
|
|
|
|
BRACE_RE = /#{RULE_OPEN_BRACE_RE}|#{RULE_CLOSE_BRACE_RE}/m
|
2013-07-31 17:04:30 +02:00
|
|
|
|
|
|
|
# replace first level properties in the css with yields
|
|
|
|
# replace_properties("a { color: white }") { |props| props.gsub 'white', 'red' }
|
|
|
|
def replace_properties(css, &block)
|
|
|
|
s = StringScanner.new(css)
|
2013-08-03 00:22:57 +02:00
|
|
|
s.skip_until /#{RULE_OPEN_BRACE_RE}\n?/
|
2013-07-31 17:04:30 +02:00
|
|
|
prev_pos = s.pos
|
|
|
|
depth = 0
|
|
|
|
pos = []
|
2013-08-03 00:22:57 +02:00
|
|
|
while (b = scan_next(s, /#{SELECTOR_RE}#{RULE_OPEN_BRACE_RE}|#{RULE_CLOSE_BRACE_RE}/m))
|
|
|
|
depth += (b == '}' ? -1 : +1)
|
|
|
|
if depth == 1
|
2013-07-31 17:04:30 +02:00
|
|
|
if b == '}'
|
|
|
|
prev_pos = s.pos
|
|
|
|
else
|
2013-08-03 00:22:57 +02:00
|
|
|
pos << (prev_pos .. s.pos - b.length - 1)
|
2013-07-31 17:04:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-03 00:22:57 +02:00
|
|
|
replace_substrings_at css, pos, &block
|
2013-07-31 17:04:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-07-31 02:27:29 +02:00
|
|
|
# next matching brace for brace at brace_pos in css
|
|
|
|
def next_brace_pos(css, brace_pos)
|
|
|
|
depth = 0
|
2013-08-03 00:22:57 +02:00
|
|
|
s = StringScanner.new(css[brace_pos..-1])
|
2013-07-31 17:04:30 +02:00
|
|
|
while (b = scan_next(s, BRACE_RE))
|
2013-07-31 02:27:29 +02:00
|
|
|
depth += (b == '}' ? -1 : +1)
|
|
|
|
break if depth.zero?
|
|
|
|
end
|
|
|
|
raise "match not found for {" unless depth.zero?
|
|
|
|
brace_pos + s.pos - 1
|
|
|
|
end
|
|
|
|
|
2013-07-31 16:02:19 +02:00
|
|
|
# advance scanner to pos after the next match of pattern and return the match
|
2013-07-31 02:27:29 +02:00
|
|
|
def scan_next(scanner, pattern)
|
|
|
|
return unless scanner.skip_until(pattern)
|
|
|
|
scanner.pos -= scanner.matched_size
|
|
|
|
scanner.scan pattern
|
|
|
|
end
|
2013-07-31 17:04:30 +02:00
|
|
|
|
2013-08-03 00:22:57 +02:00
|
|
|
# insert substitutions into text at positions (Range or Fixnum)
|
|
|
|
# substitutions can be passed as array or as yields from the &block called with |substring, position, text|
|
|
|
|
# position is a range (begin..end)
|
|
|
|
def replace_substrings_at(text, positions, replacements = nil, &block)
|
2013-07-31 17:04:30 +02:00
|
|
|
offset = 0
|
2013-08-03 00:22:57 +02:00
|
|
|
positions.each_with_index do |p, i|
|
|
|
|
p = (p...p) if p.is_a?(Fixnum)
|
|
|
|
p = p.exclude_end? ? (p.begin + offset ... p.end + offset) : (p.begin + offset .. p.end + offset)
|
|
|
|
# block returns the substitution, e.g.: { |text, pos| text[pos].upcase }
|
|
|
|
r = replacements ? replacements[i] : block.call(text[p], p, text)
|
|
|
|
offset += r.size - (p.end - p.begin + 1)
|
|
|
|
text[p] = r
|
2013-07-31 17:04:30 +02:00
|
|
|
end
|
2013-08-03 00:22:57 +02:00
|
|
|
text
|
2013-07-31 17:04:30 +02:00
|
|
|
end
|
2013-08-03 09:31:57 +02:00
|
|
|
|
|
|
|
def get_json(url)
|
|
|
|
JSON.parse open(url).read
|
|
|
|
end
|
2013-08-04 23:22:54 +02:00
|
|
|
|
|
|
|
class Logger
|
|
|
|
include Term::ANSIColor
|
|
|
|
|
|
|
|
def initialize(env)
|
|
|
|
@env = env
|
|
|
|
puts bold "Convert Bootstrap LESS to SASS"
|
2013-08-05 01:46:33 +02:00
|
|
|
puts " repo : #{env[:repo]}"
|
|
|
|
puts " branch : #{env[:branch]} #{dark "#{env[:repo]}/tree/#{env[:branch_sha]}"}"
|
|
|
|
puts " save to: SCSS @ #{@env[:save_at][:scss]} JS @ #{@env[:save_at][:js]}"
|
2013-08-05 01:36:20 +02:00
|
|
|
puts dark "-" * 60
|
2013-08-04 23:22:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_status(status)
|
2013-08-05 01:36:20 +02:00
|
|
|
puts bold status
|
2013-08-04 23:22:54 +02:00
|
|
|
end
|
|
|
|
|
2013-08-04 23:37:34 +02:00
|
|
|
def log_transform(*args)
|
2013-08-05 00:21:17 +02:00
|
|
|
puts "#{cyan " #{caller[1][/`.*'/][1..-2]}"}#{cyan " with #{args * ', '}" unless args.empty?}"
|
2013-08-04 23:37:34 +02:00
|
|
|
end
|
|
|
|
|
2013-08-05 00:21:17 +02:00
|
|
|
def log_downloading(files)
|
2013-08-05 01:46:33 +02:00
|
|
|
puts " downloading #{files.length} files #{dark files * ' '}..."
|
2013-08-05 00:21:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_processing(name)
|
|
|
|
puts yellow " #{File.basename(name)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_processed(name)
|
2013-08-05 00:44:05 +02:00
|
|
|
puts green " #{name}"
|
2013-08-04 23:22:54 +02:00
|
|
|
end
|
|
|
|
end
|
2013-05-23 14:51:56 -07:00
|
|
|
end
|