Closed gdelhumeau closed 10 years ago
Hi, sorry for later answer, I'm glad you are interested in using less4j in your app. Import dir like functionality can be achieved using custom less source as described here.
What is LessSource
Less4j uses LessSource
to fetch imported files. Less4j does not care what is behind the LessSource
interface as long as its implementation returns data or throws appropriate exceptions.
Three important methods:
relativeSource
- get referenced source,String getContent
- importing of css and less,byte[] getBytes()
- used from data-uri function - it encodes file content into base64.Then you just have to use CompilationResult compile(LessSource inputFile)
method of LessCompiler
interface to compile your sources.
How to achieve what you want You can new LessSource type (maybe from FileSource) which would search for files in both current directory and in custom non-relative paths.
Using custom less source:
//create demo less file and compiler
File inputLessFile = createFile("sampleInput.less", "* { margin: 1 1 1 1; }");
LessCompiler compiler = new ThreadUnsafeLessCompiler();
//use custom less source
CompilationResult compilationResult = compiler.compile(new CustomFileSource(inputLessFile));
//print compiled css
System.out.println(compilationResult.getCss());
Either constructor or relativeSource
method of your custom less source will contain something like this:
this.inputFile = new File(parent.inputFile.getParentFile(), filename);
if (!inputFile.exists()) {
this.inputFile = new File(CUSTOM_PATH, filename);
}
Note: import-once feature uses LessSource.equals
to check for multiple imports of the same file. If you are using it, your custom LessSource should implement it equals
and hashCode
.
I updated wiki, because the same request was asked by multiple people.
I added new public FileSource(FileSource parent, File inputFile, String charsetName)
constructor to the FileSource
class as I was writing that wiki. Therefore, wiki solution is not compatible with latest released version. If it is problem for you, let me know and I will do minor release.
Thanks for the nice answer. I'll check that.
It would be nice if you could release your modifications, indeed.
Thanks :)
@gdelhumeau Done, less4j-1.8.3 should be available in maven central.
Thanks @SomMeri !
You should just add the CustomFileSource to the code base as MultiFileSource or some such. I'm using less4j from a script environment and adding custom classes is a pain in the butt.
@brianhks Good idea, this came up already multiple times. I opened new issue for it #287 . If you have such general classes already written, I would accept pull request.
I've made a pull request for this: https://github.com/SomMeri/less4j/pull/289
@gdelhumeau Thank you.
Hello.
I would be very interested to use Less4j for the integration of LESS inside our application (XWiki: http://extensions.xwiki.org/xwiki/bin/view/Extension/LESS+Module).
But I cannot do it if there is no support for the "
--include-path
" option, which allows us to specify a directory where LESS can find the files that we would like to import using "@import
".Is there a plan to add this feature in Less4J?
Thanks,
Guillaume from XWiki.