I have to parse YAML Front Matter
in java
like jekyll
, 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
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)) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
// parse data
parseYamlFrontMatter(sb.toString());
parseMarkdownOrWhatever(br);
}
To get a obtain Reader
, you will probably need a FileReader
or an InputStreamReader
.