简体   繁体   中英

Compile a string as a Compass file

Is there a simple way to compile a string of text as a compass (sass) stylesheet?

Example input: "section\\n background: darken(white, 10%)"

sass has:

-s, --stdin   Read input from standard input instead of a n input file

and

--compass  Make Compass imports available and load project configuration.

You could use popen with something like this:

output = IO.popen("sass -s --compass", "w+") do |pipe|
  pipe.puts "section\n background: darken(white, 10%)"
  pipe.close_write
  pipe.read
end

and output: section {\\n background: #e6e6e6; }\\n section {\\n background: #e6e6e6; }\\n

You can use the class method Sass.compile . To use .sass (indented) syntax you need to pass the :syntax => :sass option:

require 'sass'

Sass.compile "section\n background: darken(white, 10%)", syntax: :sass
#=> "section {\n  background: #e6e6e6; }\n"

Note: Compass itself doesn't provide an equivalent function so if you want all of Compass' goodies you'll need to @import 'compass' in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM