Skip to content

Commit

Permalink
Add a test with an AsciidoctorJ implementation of the FrontMatterPrep…
Browse files Browse the repository at this point in the history
…rocessor.

Add method Reader.getLines
  • Loading branch information
robertpanzer committed Jul 23, 2022
1 parent 1b81898 commit d761556
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions asciidoctorj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public List<String> lines() {
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);
Expand Down
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();
}
}
}
}
}
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");
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit d761556

Please sign in to comment.