I have a method that takes a scss file f and compiles it:
def compile f
options = ...
Sass::Engine.new(File.read(f), options).render
end
where options is a hash that specifies several parameters. All the scss files that I want to compile with this command #import-s a fixed file, say "_foo.scss". So at the moment, I have the line
#import "foo";
at the top of every scss file. I want to let the method above implicitly/automatically do the equivalent to #import "foo"; so that I do not have to write that at the top of every scss file. How can I do that? Is there something I can do with options, or some hack with sass internals?
Related
Is there a way to make sass ignore multiline comments when generating the css file:
// these comments are ignored
These are not (only ignored in compressed mode):
/*
* multiline comments
*
*/
I found this ticket on Github where the author says:
If you really want, you can monkeypatch Sass to silence /* */ comments as well.
But I don't know what he means by monkeypatch sass, so how can I do this ?
Yay! I've learned monkey patching SASS while answering this question:
Sass mixin recursion; #include loop
And now i can help you too!
1) Install Compass
For this solution to work, you'll need Compass. Install it with:
gem install compass
2) Configure Compass
Create a compass.rb file in your project's root and define directories where you keep your SASS and CSS code, e. g.:
css_dir = "stylesheets"
sass_dir = "sass"
3) Create a monkey patch
Create a file called remove-all-comments-monkey-patch.rb in your project's root:
class Sass::Tree::Visitors::Perform < Sass::Tree::Visitors::Base
# Removes all comments completely
def visit_comment(node)
return []
end
end
4) Require the monkey patch from the config.rb
In the config.rb, add:
# Removing all comments by applying a monkey patch to SASS compiler
require "./remove-all-comments-monkey-patch"
5) Compile your project with Compass
Use compass compile to compile SASS into CSS. You can also use compass watch to make the Compass command line tool constantly monitor your code for changes and recompile parts that you modify.
Considerations
This will not remove comments with line numbers generated by SASS. To disable them comment out the line_comments = true line in config.rb or set it to false.
To re-enable multiline comments, just comment out the line that requires the monkey patch and do compass clean.
Don't use it! Use single-line comments with Ctrl+/.
Though this solution is portable and will work for everybody without hacking SASS code manually, you should really consider using an IDE that allows commenting out whole paragraphs with single-line comments using a single keystroke. For me it's Ctrl+/.
Here, i've filmed a short video for you to show that using line comments is actually quicker and more effective than using multiline comments: http://www.youtube.com/watch?feature=player_detailpage&v=DTyMAPZrwyc
Line comments also let you comment out comments without breaking the code.
Consider you have the following code:
foo
/* Bla bla */
bar
baz
And you need to comment it all out. If you wrap it all with /* */...
/*foo
/* Bla bla */
bar
baz*/
...then you broke the code! Now you have a comment that starts with /*foo and ends with bla */, and also a syntax error at baz*/.
Instead, just select the whole code and hit Ctrl+/ (provided that use some IDE or programmer's notepad), it will all be commented out immediately:
//foo
//
///* Bla bla */
//bar
//
//baz
And of course it can later be uncommented with the same hotkey.
You could wrap the comment in an unused #mixin - not an ideal fix but it works.
#mixin ignore {
/*
COMMENT
*/
}
I am using 2 set of scripts for soap communication, inside they are identical classes and variables therefore when using the main script I need to put condition.
if A
require A.rb
if B
require B.rb
The problem is that the ruby2exe does not include these script when compiling the main script.
If have put the 2 require on the top of the file it does not work but then the main script confuse between the classes in the two files.
Idea how to solve it ?
Thanks
Eran
Try to include both at the beginning so the compiler picks them up, later at line +520 you do the include again, i presume the last included file will be what is used.
Also try the ocra compiler which is more recent then ruby2exe.
many websites join the same propery of css rule in their css file, I want to use compass sass to complete this job, I found that extend method can do it, but it must extend base an selector, any way not to output the base css selector?,for example.
.a{color:red;font-size:10px;}
.b{color:red:font-weight:bold;}
they has same "color:red".if I want to ouput
.a,.b{color:red;}
I must extend .c
.c{color:red;}
but actually I don't want .c display at my css file. how to do it?
In Sass 3.2, they added a feature that does just that: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#placeholders
Basically, use a % instead of a . to make a silent class that's only for extending.
%c {
color: green;
}
.a, .b {
#extend %c;
}
Sass::Exec::SassConvert.new(["{path_to_scss_file}.scss", "{path_to_css_file_to_generate}.css"]).process_result
When I run following command from inside a ruby script, the generated file contains "sass"-syntax instead of valid css
When I try to add a set_opts command to specify "-F scss -T css", just to be sure the convertor knows what to do, it throws an error:
NoMethodError: undefined method `banner=' for ["-F scss -T css"]:Array
The goal is to compile scss files to css files from inside an ant build script.
Everything is working except for the wrong syntax issue.
Is there anything I'm missing here?
I rewrote the script to use Sass::Engine instead of SassConvert.
After looking through the code of the SassConvert class, the following fragment pointed me to the solution:
unless [:scss, :sass].include?(#options[:to])
raise "Unknown format for sass-convert --to: #{name}"
end
It appears the SassConvert class only converts CSS files to .scss or .sass, not the other way around.
Also, the correct way to add option flags when calling Sass::Exec::SassConvert, is by doing the following:
sassConvert = Sass::Exec::SassConvert.new(["-F scss", "-T sass", "{path_to_from_file}", "{path_to_file_to_generate}"]
sassConvert.parse!
sassConvert.process_result
I've got a sass file that only contains import statements
#import "this";
#import "that";
if I run sass from the command line everything's good
bundle exec sass foo.scss:foo.css
If, however I run it from within a script (also via bundle exec) it gets upset about those semicolons. This...
template = File.read("foundation.scss")
sass_engine = Sass::Engine.new(template)
sass_output = sass_engine.render
...produces the following on the sass_engine.render call:
(sass):1: Invalid #import: expected end of line, was ";". (Sass::SyntaxError)
if i get rid of the semicolons then the situation is reversed. It complains on the command line and not in the script.
What's going on, and how do I get it to accept the semicolons when run from a script?
The difference is that the Sass command line program notices the "scss" extension and parses the file as SCSS instead of traditional Sass. When doing it programatically, you are starting a Sass engine and not telling it that it is SCSS instead.
So, the error is that its reading it as Sass instead of SCSS.
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options
This should fix your problem right up!
template = File.read("foundation.scss")
sass_engine = Sass::Engine.new(template, :syntax => :scss)
sass_output = sass_engine.render
Viola!