Gautam Gautam - 1 year ago 101
Java Question

Parsing YAML Front matter in Java

I have to parse

, So Iooked into the source code, and found this but I can't make much sense of it(I don't know much ruby).

So My Question is, How do I parse
YAML Front Matter
in java ?

I have snakeyaml in my classpath and I would be parsing
YAML Front Matter
from a markdown file, for which I use pegdown

Answer Source
void parse(Reader r) throws IOException {
    BufferedReader br = new BufferedReader(r);

    // detect YAML front matter
    String line = br.readLine();
    while (line.isEmpty()) line = br.readLine();
    if (!line.matches("[-]{3,}")) { // use at least three dashes
        throw new IllegalArgumentException("No YAML Front Matter");
    final String delimiter = line;

    // scan YAML front matter
    StringBuilder sb = new StringBuilder();
    line = br.readLine();
    while (!line.equals(delimiter)) {
        line = br.readLine();

    // parse data

To get a obtain Reader, you will probably need a FileReader or an InputStreamReader.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download