Search in sources :

Example 1 with Output

use of io.bit3.jsass.Output in project mycore by MyCoRe-Org.

the class MCRSassCompilerManager method compile.

/**
 * Just compiles a file and fills all maps.
 *
 * @param name the name of the file (with .min.css or .css ending)
 * @return the compiled css
 * @throws CompilationException
 */
private String compile(String name, List<Importer> importer) throws CompilationException, IOException {
    Options options = new Options();
    Collection<Importer> importerList = options.getImporters();
    importerList.addAll(importer);
    String realFileName = name.replace(".min.css", ".scss").replace(".css", ".scss");
    Optional<Import> importedStartStylesheet = importer.stream().map(i -> i.apply(realFileName, null)).filter(Objects::nonNull).map(i -> i.stream().findFirst()).filter(Optional::isPresent).map(Optional::get).findFirst();
    if (!importedStartStylesheet.isPresent()) {
        return null;
    }
    Import firstImport = importedStartStylesheet.get();
    StringContext context = new StringContext(firstImport.getContents(), firstImport.getAbsoluteUri(), firstImport.getAbsoluteUri(), options);
    Compiler compiler = new Compiler();
    Output output = compiler.compile(context);
    String css = output.getCss();
    boolean compress = name.endsWith(".min.css");
    if (compress) {
        try {
            CssCompressor cssCompressor = new CssCompressor(new StringReader(css));
            int lineBreaks = MCR_CONFIGURATION.getInt(LINE_BREAK_CONFIG_KEY, DEFAULT_LINE_BREAK_CONFIG_VALUE);
            StringWriter out = new StringWriter(css.length());
            cssCompressor.compress(out, lineBreaks);
            css = out.toString();
        } catch (IOException e) {
            throw new MCRException("Read from StringReader produces IOException! (this should not happen)", e);
        }
    }
    this.fileCompiledContentMap.put(name, css);
    this.fileLastCompileDateMap.put(name, new Date());
    try {
        this.fileMD5Map.put(name, MCRUtils.asMD5String(1, null, css));
    } catch (NoSuchAlgorithmException e) {
        throw new MCRException("Error while generating md5 of result css!", e);
    }
    return css;
}
Also used : StringContext(io.bit3.jsass.context.StringContext) FileContext(io.bit3.jsass.context.FileContext) Date(java.util.Date) Output(io.bit3.jsass.Output) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRException(org.mycore.common.MCRException) Options(io.bit3.jsass.Options) Importer(io.bit3.jsass.importer.Importer) Map(java.util.Map) Compiler(io.bit3.jsass.Compiler) CompilationException(io.bit3.jsass.CompilationException) StringWriter(java.io.StringWriter) Collection(java.util.Collection) Import(io.bit3.jsass.importer.Import) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Objects(java.util.Objects) MCRUtils(org.mycore.common.MCRUtils) CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) List(java.util.List) Logger(org.apache.logging.log4j.Logger) StringReader(java.io.StringReader) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) Options(io.bit3.jsass.Options) Compiler(io.bit3.jsass.Compiler) MCRException(org.mycore.common.MCRException) Import(io.bit3.jsass.importer.Import) Optional(java.util.Optional) CssCompressor(com.yahoo.platform.yui.compressor.CssCompressor) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Date(java.util.Date) StringWriter(java.io.StringWriter) Output(io.bit3.jsass.Output) Objects(java.util.Objects) StringReader(java.io.StringReader) Importer(io.bit3.jsass.importer.Importer) StringContext(io.bit3.jsass.context.StringContext)

Aggregations

CssCompressor (com.yahoo.platform.yui.compressor.CssCompressor)1 CompilationException (io.bit3.jsass.CompilationException)1 Compiler (io.bit3.jsass.Compiler)1 Options (io.bit3.jsass.Options)1 Output (io.bit3.jsass.Output)1 FileContext (io.bit3.jsass.context.FileContext)1 StringContext (io.bit3.jsass.context.StringContext)1 Import (io.bit3.jsass.importer.Import)1 Importer (io.bit3.jsass.importer.Importer)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1