Search in sources :

Example 1 with Importer

use of io.bit3.jsass.importer.Importer 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)

Example 2 with Importer

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

the class MCRSassResource method getCSS.

@GET
@Path("{fileName:.+}")
@Produces("text/css")
public Response getCSS(@PathParam("fileName") String name, @Context Request request) {
    try {
        MCRServletContextResourceImporter importer = new MCRServletContextResourceImporter(context);
        Optional<String> cssFile = MCRSassCompilerManager.getInstance().getCSSFile(name, Stream.of(importer).collect(Collectors.toList()));
        if (cssFile.isPresent()) {
            CacheControl cc = new CacheControl();
            cc.setMaxAge(SECONDS_OF_ONE_DAY);
            String etagString = MCRSassCompilerManager.getInstance().getLastMD5(name).get();
            EntityTag etag = new EntityTag(etagString);
            Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
            if (builder != null) {
                return builder.cacheControl(cc).tag(etag).build();
            }
            return Response.ok().status(Response.Status.OK).cacheControl(cc).tag(etag).entity(cssFile.get()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (IOException | CompilationException e) {
        StreamingOutput so = (OutputStream os) -> e.printStackTrace(new PrintStream(os, true, "UTF-8"));
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(so).build();
    }
}
Also used : CompilationException(io.bit3.jsass.CompilationException) PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) MCRServletContextResourceImporter(org.mycore.sass.MCRServletContextResourceImporter) Response(javax.ws.rs.core.Response) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

CompilationException (io.bit3.jsass.CompilationException)2 IOException (java.io.IOException)2 CssCompressor (com.yahoo.platform.yui.compressor.CssCompressor)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 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)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