Add a test with an AsciidoctorJ implementation of the FrontMatterPreprocessor.
Add method Reader.getLines
MODIFY
asciidoctorj-api/src/main/java/org/asciidoctor/extension/Reader.java
asciidoctorj-api/src/main/java/org/asciidoctor/extension/Reader.java
@@ -76,8 +76,18 @@ public interface Reader {
*/
String readLine();
+ /**
+ * @return A copy of the String List of lines remaining in this Reader
+ * @deprecated Use {@link #getLines()}
+ */
+ @Deprecated
List<String> lines();
+ /**
+ * @return A copy of the String List of lines remaining in this Reader
+ */
+ List<String> getLines();
+
/**
* Push the String line onto the beginning of the Array of source data.
*
@@ -107,7 +117,8 @@ public interface Reader {
* already marked as processed, but does not consume them.
*
* @param lineCount The Integer number of lines to peek.
- * @return
+ * @return A String List of the next multiple lines of source data, or an empty list
+ * if there are no more lines in this Reader.
*/
List<String> peekLines(int lineCount);

MODIFY
asciidoctorj-core/build.gradle
asciidoctorj-core/build.gradle
@@ -33,6 +33,7 @@ dependencies {
testImplementation "com.google.guava:guava:$guavaVersion"
testImplementation "org.jsoup:jsoup:$jsoupVersion"
testImplementation "io.netty:netty-all:$nettyVersion"
+ testImplementation "org.assertj:assertj-core:$assertjVersion"
testImplementation project(path: ':asciidoctorj-arquillian-extension')
compileOnly "org.osgi:osgi.annotation:$osgiVersion"
}

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/ReaderImpl.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/ReaderImpl.java
@@ -76,6 +76,11 @@ public class ReaderImpl extends RubyObjectWrapper implements Reader {
return getList("lines", String.class);
}
+ @Override
+ public List<String> getLines() {
+ return getList("lines", String.class);
+ }
+
@Override
public void restoreLine(String line) {
getRubyProperty("unshift_line", line);

ADD
asciidoctorj-core/src/test/java/org/asciidoctor/extension/FrontMatterPreprocessor.java
+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();
+ }
+ }
+ }
+ }
+}

ADD
asciidoctorj-core/src/test/java/org/asciidoctor/extension/FrontMatterPreprocessorTest.java
+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");
+ }
+}

MODIFY
build.gradle
build.gradle
@@ -72,7 +72,7 @@ ext {
jrubyVersion = '9.3.4.0'
jsoupVersion = '1.14.3'
junitVersion = '4.13.2'
- assertjVersion = '3.19.0'
+ assertjVersion = '3.22.0'
nettyVersion = '4.1.58.Final'
saxonVersion = '9.9.0-2'
xmlMatchersVersion = '1.0-RC1'

Add a test with an AsciidoctorJ implementation of the FrontMatterPreprocessor.
Add method Reader.getLines