Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Port of less.js into java.
Java

This branch is 8 commits ahead, 553 commits behind SomMeri:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
.gitignore
.travis.yml
README.md
pom.xml

README.md

Less language is an extension of css and this project compiles it into regular css. It adds several dynamic features into css: variables, expressions, nested rules, and so on.

The original compiler was written in JavaScript and is called less.js. The less language is mostly defined in less.js documentation/issues and by what less.js actually do. Links to less.js:

Less4j is a port and its behavior should be as close to the original implementation as reasonable. Unless explicitly stated otherwise, any difference between less.js and less4j outputs is considered a bug. As close as reasonable means that style sheets generated by less4j must be functionally the same as the outputs of less.js. However, they do not have to be exactly the same:

  • Behavior of less.js and less4j may differ on invalid input files.
  • Output files may differ by whitespaces or comments locations.
  • Less4j may do more than less.js in some situations. The input rejected by less.js may be accepted and translated by less4j.

All known differences are documented on wiki page. In the future, Less4j will produce warning any time it produces functionally different CSS. That feature was not implemented yet.

Continuous Integration

Continuous integration is set up on Travis-CI, its current status is: Build Status.

Twitter

Our twitter account: Less4j

Documentation:

The documentation is kept on Github wiki:

For those interested about project internals, architecture and comments handling are described in a blog post. The blog post captures our ideas at the time of its writing, so current implementation may be a bit different.

Command Line

Less4j can run from command line. Latest versions are shared via less4j dropbox account. Shared folder always contains at least two latest versions, but we may remove older ones.

If you need an old version for some reason, checkout appropriate tag from git and use mvn package -P standalone command. The command compiles less4j and all its dependencies into target/less4j-<version>-shaded.jar file.

Maven

Less4j is available in Maven central repository.

Pom.xml dependency:

<dependency>
  <groupId>com.github.sommeri</groupId>
  <artifactId>less4j</artifactId>
  <version>0.0.11</version>
</dependency>

Integration With Wro4j

The easiest way to integrate less4j into Java project is to use wro4j library. More about wro4j can be found either in a blog post or on wro4j google code page.

API:

Warning: Project is still in alpha and current API is very temporary. It will change in the future.

Access the compiler either through the com.github.less4j.LessCompiler interface. Its thread safe implementation is com.github.less4j.core.DefaultLessCompiler. The interface exposes two methods:

  • CompilationResult compile(File inputFile) - compiles a file,
  • CompilationResult compile(String lessContent) - compiles a string.

The first and second method differ in one important point: the second method is unable to handle "@import" statements located in compiled string. Files referenced by the import statement are relative to current file. Compiler invoked through string based compile method is unable to find imported less files and therefore leaves the import statement as it is. Use it with caution.

Return object CompilationResult has two methods:

  • getCss - returns compiled css,
  • getWarnings - returns list of compilation warnings or an empty list.

Each warning is described by an error message and knows both line and character number of the place that caused it.

// create input file
File inputLessFile = createFile("sampleInput.less", "* { margin: 1 1 1 1; }");

// compile it
LessCompiler compiler = new ThreadUnsafeLessCompiler();
CompilationResult compilationResult = compiler.compile(inputLessFile);

// print results to console
System.out.println(compilationResult.getCss());
for (Problem warning : compilationResult.getWarnings()) {
  System.err.println(format(warning));
}

private static String format(Problem warning) {
  return "WARNING " + warning.getLine() +":" + warning.getCharacter()+ " " + warning.getMessage();
}

The method may throw Less4jException. The exception is checked and can return list of all found compilation errors. In addition, compilation of some syntactically incorrect inputs may still lead to some output or produce a list of warnings. If this is the case, produced css is most likely invalid and the list of warnings incomplete. Even if they are invalid, they still can occasionally help to find errors in the input and the exception provides access to them.

  • List<Problem> getErrors - list of all found compilation errors.
  • CompilationResult getPartialResult() - css and list of warnings produced despite compilation errors. There is no guarantee on what exactly will be returned. Use with caution.

Links:

Something went wrong with that request. Please try again.