SomMeri / less4j

Less language is an extension of css and less4j compiles it into regular css. Less adds several dynamic features into css: variables, expressions, nested rules, and so on. Less was designed to be compatible with css and any correct css file is also correct less file.
146 stars 47 forks source link

The --include-path option is missing #229

Closed gdelhumeau closed 10 years ago

gdelhumeau commented 10 years ago

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.

SomMeri commented 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:

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.

SomMeri commented 10 years ago

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.

gdelhumeau commented 10 years ago

Thanks for the nice answer. I'll check that.

gdelhumeau commented 10 years ago

It would be nice if you could release your modifications, indeed.

Thanks :)

SomMeri commented 10 years ago

@gdelhumeau Done, less4j-1.8.3 should be available in maven central.

gdelhumeau commented 10 years ago

Thanks @SomMeri !

brianhks commented 9 years ago

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.

SomMeri commented 9 years ago

@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.

gdelhumeau commented 9 years ago

I've made a pull request for this: https://github.com/SomMeri/less4j/pull/289

SomMeri commented 9 years ago

@gdelhumeau Thank you.