Skip to content

Instantly share code, notes, and snippets.

@2no
Last active December 17, 2015 16:59
Show Gist options
  • Save 2no/5643176 to your computer and use it in GitHub Desktop.
Save 2no/5643176 to your computer and use it in GitHub Desktop.
Sass に文字列置換の関数を追加し、更に引数で関数を指定できる様にしてみる
$tmp: "";
.sample:after {
$tmp: str-replace("/_./", "|x|x.upcase.sub!('_', '')", "test_hoge");
content: "#{$tmp}";
}
// 結果:content: "testHoge";
$tmp: "";
.sample2:after{
$tmp: str-replace("_", "", "test_hoge");
content: "#{$tmp}";
}
// 結果:content: "testhoge";
# 参考:http://www.skyward-design.net/blog/archives/000128.html
require "sass"
module WakuworksFunctions
def str_replace(search_cond, replace_str, str)
assert_type search_cond, :String
assert_type replace_str, :String
assert_type str, :String
begin
replace = eval "lambda { " + replace_str.value + " }"
rescue
replace = replace_str.value
end
is_proc = replace.class == Proc
begin
search = eval search_cond.value
rescue
search = search_cond.value
end
if (search_cond.class == Regexp) then
val = is_proc ?
str.value.gsub(search, &replace) :
str.value.gsub(search, replace)
else
val = is_proc ?
str.value.sub(search, &replace) :
str.value.sub(search, replace)
end
Sass::Script::String.new(val)
end
end
module Sass::Script::Functions
include WakuworksFunctions
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment