@Test public void should_read_empty_file() { String content = fileContent(""); ContentWithVariables parsed = yamlFrontMatter.parse(content); assertThat(parsed.getVariables()).isEmpty(); assertThat(parsed.getContent()).isEmpty(); }
@Test public void should_read_file_without_headers() { String content = fileContent("CONTENT"); ContentWithVariables parsed = yamlFrontMatter.parse(content); assertThat(parsed.getVariables()).isEmpty(); assertThat(parsed.getContent()).isEqualTo("CONTENT"); }
@Test public void should_read_header_variables() { String content = fileContent("---", "layout: standard", "title: CodeStory - Devoxx Fight", "---", "CONTENT"); ContentWithVariables parsed = yamlFrontMatter.parse(content); assertThat(parsed.getVariables()) .includes(entry("layout", "standard"), entry("title", "CodeStory - Devoxx Fight")); assertThat(parsed.getContent()).isEqualTo("CONTENT"); }
@Test public void should_ignore_commented_variable() { String content = fileContent( "---", "#layout: standard", "title: CodeStory - Devoxx Fight", "---", "CONTENT"); ContentWithVariables parsed = yamlFrontMatter.parse(content); assertThat(parsed.getVariables()) .excludes(entry("layout", "standard")) .excludes(entry("#layout", "standard")) .includes(entry("title", "CodeStory - Devoxx Fight")); }
CacheEntry render(Map<String, Object> keyValues) { try { YamlFrontMatter yamlFrontMatter = YamlFrontMatter.parse(path); String content = yamlFrontMatter.getContent(); Map<String, Object> variables = yamlFrontMatter.getVariables(); Map<String, Object> allKeyValues = merge(variables, keyValues); String body = new HandlebarsCompiler().compile(content, allKeyValues); String layout = (String) variables.get("layout"); if (layout != null) { body = new Template("_layouts", layout) .render(allKeyValues) .content() .replace("[[body]]", body); } return Compilers.INSTANCE.compile(path, body); } catch (IOException e) { throw new IllegalStateException("Unable to render template", e); } }