use of io.bit3.jsass.importer.Import in project Orchid by JavaEden.
the class SassImporter method apply.
@Override
public Collection<Import> apply(String url, Import previous) {
EdenPair<String, String> thisItem = splitPath(url);
String[] availableFiles = new String[] { thisItem.first + "/" + thisItem.second + ".scss", thisItem.first + "/" + thisItem.second + ".sass", thisItem.first + "/" + "_" + thisItem.second + ".scss", thisItem.first + "/" + "_" + thisItem.second + ".sass" };
for (String availableFile : availableFiles) {
String absoluteUri = OrchidUtils.normalizePath(splitPath(previous.getAbsoluteUri().getPath()).first + "/" + availableFile);
if (absoluteUri.contains("//")) {
absoluteUri = absoluteUri.replaceAll("//", "/");
}
if (absoluteUri.startsWith("/")) {
absoluteUri = absoluteUri.substring(1);
}
OrchidResource importedResource = resources.get().getResourceEntry("assets/css/" + absoluteUri);
if (importedResource != null) {
String content = importedResource.getContent();
if (importedResource.shouldPrecompile()) {
content = context.getTheme().precompile(content, importedResource.getEmbeddedData());
}
try {
String newURI = "" + OrchidUtils.normalizePath(absoluteUri);
Import newImport = new Import(newURI, newURI, content);
return Collections.singletonList(newImport);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
use of io.bit3.jsass.importer.Import in project mycore by MyCoRe-Org.
the class MCRServletContextResourceImporter method apply.
@Override
public Collection<Import> apply(String url, Import previous) {
try {
String absolute = url;
if (previous != null) {
absolute = previous.getAbsoluteUri().resolve(absolute).toString();
}
List<String> possibleNameForms = getPossibleNameForms(absolute);
Optional<URL> firstPossibleName = possibleNameForms.stream().map(form -> {
try {
return context.getResource(normalize(form));
} catch (MalformedURLException e) {
// ignore exception because it seems to be a not valid name form
return null;
}
}).filter(Objects::nonNull).findFirst();
if (!firstPossibleName.isPresent()) {
return null;
}
URL resource = firstPossibleName.get();
String contents = getStringContent(resource);
URI absoluteUri = resource.toURI();
LOGGER.debug("Resolved {} to {}", url, absoluteUri);
return Stream.of(new Import(absolute, absolute, contents)).collect(Collectors.toList());
} catch (IOException | URISyntaxException e) {
LOGGER.error("Error while resolving {}", url, e);
return null;
}
}
use of io.bit3.jsass.importer.Import 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;
}
Aggregations