org.commonmark.testutil.example.ExampleReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commonmark-test-util Show documentation
Show all versions of commonmark-test-util Show documentation
commonmark-java classes for tests
The newest version!
package org.commonmark.testutil.example;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Reader for files containing examples of CommonMark source and the expected HTML rendering (e.g. spec.txt).
*/
public class ExampleReader {
private static final Pattern SECTION_PATTERN = Pattern.compile("#{1,6} *(.*)");
private static final String EXAMPLE_START_MARKER = "```````````````````````````````` example";
private final InputStream inputStream;
private final String filename;
private State state = State.BEFORE;
private String section;
// The gfm spec has additional text after the example marker for their additions, e.g. "table"
private String info = "";
private StringBuilder source;
private StringBuilder html;
private int exampleNumber = 0;
private List examples = new ArrayList<>();
private ExampleReader(InputStream stream, String filename) {
this.inputStream = stream;
this.filename = filename;
}
public static List readExamples(URL url) {
try (InputStream stream = url.openStream()) {
return new ExampleReader(stream, new File(url.getPath()).getName()).read();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static List