-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test with an AsciidoctorJ implementation of the FrontMatterPrep…
…rocessor. Add method Reader.getLines
- Loading branch information
1 parent
1b81898
commit d761556
Showing
6 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
asciidoctorj-core/src/test/java/org/asciidoctor/extension/FrontMatterPreprocessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.asciidoctor.extension; | ||
|
||
import org.asciidoctor.ast.Document; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FrontMatterPreprocessor extends Preprocessor { | ||
|
||
@Override | ||
public void process(Document document, PreprocessorReader reader) { | ||
List<String> lines = reader.getLines(); | ||
if (lines.isEmpty()) { | ||
return; | ||
} | ||
List<String> frontMatter = new ArrayList<>(); | ||
if ("---".equals(lines.get(0).trim())) { | ||
lines.remove(0); | ||
while (!lines.isEmpty() && !"---".equals(lines.get(0).trim())) { | ||
frontMatter.add(lines.remove(0)); | ||
} | ||
|
||
if (!lines.isEmpty() && "---".equals(lines.get(0).trim())) { | ||
lines.remove(0); | ||
document.setAttribute("front-matter", frontMatter, true); | ||
for (int i = 0; i < frontMatter.size() + 2; i++) { | ||
reader.advance(); | ||
} | ||
} | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
asciidoctorj-core/src/test/java/org/asciidoctor/extension/FrontMatterPreprocessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.asciidoctor.extension; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.Options; | ||
import org.asciidoctor.ast.Document; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Element; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(Arquillian.class) | ||
public class FrontMatterPreprocessorTest { | ||
|
||
@ArquillianResource | ||
private Asciidoctor asciidoctor; | ||
|
||
@Test | ||
public void shouldRemoveFrontMatter() { | ||
// Given: the FrontMatterPreprocessor | ||
asciidoctor.javaExtensionRegistry().preprocessor(FrontMatterPreprocessor.class); | ||
|
||
// And a document with a front matter | ||
String document = "---\n" + | ||
"layout: info\n" + | ||
"permalink: /sample/\n" + | ||
"---\n" + | ||
"= Sample Page\n" + | ||
":url-asciidoctor: http://asciidoctor.org\n" + | ||
"\n" + | ||
"This is a sample page composed in AsciiDoc.\n" + | ||
"Jekyll converts it to HTML using {url-asciidoctor}[Asciidoctor].\n" + | ||
"\n" + | ||
"[source,ruby]\n" + | ||
"puts \"Hello, World!\""; | ||
|
||
// If I parse the document | ||
Document ast = asciidoctor.load(document, Options.builder() | ||
.headerFooter(true) | ||
.toFile(false) | ||
.build()); | ||
|
||
// Then: the preprocessor has set the attribute | ||
assertThat(ast.getAttribute("front-matter")).isEqualTo(Arrays.asList( | ||
"layout: info", | ||
"permalink: /sample/" | ||
)); | ||
|
||
// And: the processor has removed the front-matter from the converted document | ||
String html = ast.convert(); | ||
assertThat(html).doesNotContain("permalink"); | ||
assertThat(html).doesNotContain("layout"); | ||
org.jsoup.nodes.Document htmlTree = Jsoup.parse(html); | ||
Element body = htmlTree.body(); | ||
assertThat(body.getElementsByTag("h1").first().text()) | ||
.isEqualTo("Sample Page"); | ||
|
||
assertThat(body.getElementsByTag("p").first().text()) | ||
.startsWith("This is a sample page"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters