001package org.jszip.sass;
002
003import org.codehaus.plexus.util.FileUtils;
004import org.jruby.embed.EmbedEvalUnit;
005import org.jruby.embed.ParseFailedException;
006import org.jruby.embed.ScriptingContainer;
007import org.jruby.exceptions.RaiseException;
008import org.jruby.javasupport.JavaEmbedUtils;
009import org.jszip.css.CssCompilationError;
010import org.jszip.css.CssEngine;
011import org.jszip.pseudo.io.PseudoFileSystem;
012
013import java.io.IOException;
014
015/**
016 * @author stephenc
017 * @since 31/01/2013 15:30
018 */
019public class SassEngine implements CssEngine {
020
021    private final PseudoFileSystem fs;
022    private final RubyProxy proxy;
023    private final PseudoFileSystemImporter fileSystemImporter;
024    private final ScriptingContainer container;
025
026    public SassEngine(PseudoFileSystem fs, String encoding) throws IOException {
027        this.fs = fs;
028        this.container = new ScriptingContainer();
029        fileSystemImporter = new PseudoFileSystemImporter(fs, encoding);
030        try {
031            Object reciever = this.container.runScriptlet(getClass().getResourceAsStream("sass-engine.rb"), "sass-engine.rb");
032            proxy = this.container.getInstance(reciever, RubyProxy.class);
033        } catch (ParseFailedException e) {
034            final IOException ioe = new IOException(e.getMessage());
035            ioe.initCause(e);
036            throw ioe;
037        }
038    }
039
040    public String mapName(String sourceFileName) {
041        return sourceFileName.replaceFirst("\\.[sS][aAcC][sS][sS]$", ".css");
042    }
043
044    public String toCSS(String name) {
045        return proxy.toCSS(fileSystemImporter, name);
046    }
047
048    public static interface RubyProxy {
049        String toCSS(PseudoFileSystemImporter importer, String name);
050    }
051}