Herb Java Bindings
Herb provides official Java bindings through JNI (Java Native Interface) to the C library, allowing you to parse HTML+ERB in Java projects with native performance.
Installation
The Java bindings are built from source. Check that Java is installed, then build them:
shell
java -version
git clone https://github.com/marcoroth/herb
cd herb/java
make templates
make jni
make javaThis creates the native library, which is libherb_jni.dylib on macOS and libherb_jni.so on Linux. Add the compiled classes to your classpath and make sure the native library is on your java.library.path.
Getting Started
java
import org.herb.Herb;
import org.herb.ParseResult;
public class Example {
public static void main(String[] args) {
String source = "<h1><%= user.name %></h1>";
ParseResult result = Herb.parse(source);
System.out.println(result.value.inspect());
}
}The API
The API pages document each call once, with a Java tab alongside the other bindings.
| Page | Java methods |
|---|---|
| Parsing | Herb.parse(String), Herb.parse(String, ParserOptions) |
| Lexing | Herb.lex(String) |
| Extracting Ruby and HTML | Herb.extractRuby, Herb.extractHTML |
| Working with the tree | Visitor<T>, node.accept(visitor) |
| Versions | Herb.version(), Herb.herbVersion(), Herb.prismVersion() |
Two things the other bindings have are missing here. Reading a file is up to you, so pass the contents to Herb.parse, and there is no locate, so walk with a visitor and compare locations instead.