Skip to content

Instantly share code, notes, and snippets.

@meistermeier
Created January 28, 2022 10:55
Show Gist options
  • Save meistermeier/9d30ed375ddfc9c1d4689c7ca565587f to your computer and use it in GitHub Desktop.
Save meistermeier/9d30ed375ddfc9c1d4689c7ca565587f to your computer and use it in GitHub Desktop.
Cypher execution from Asciidoctor document with AsciidoctorJ
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Options;
import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.StructuralNode;
import org.neo4j.driver.Driver;
import java.net.URISyntaxException;
import java.nio.file.Path;
public class AsciidoctorCypher {
public static void executeDocument(Driver driver, Path documentPath) throws URISyntaxException {
var asciidoctor = Asciidoctor.Factory.create();
var document = asciidoctor.loadFile(documentPath.toFile(), Options.builder().build());
handle(driver, document);
}
private static void handle(Driver driver, StructuralNode document) {
for (StructuralNode block : document.getBlocks()) {
handleDocumentNode(driver, block);
handle(driver, block);
}
}
private static void handleDocumentNode(Driver driver, StructuralNode documentNode) {
var language = documentNode.getAttribute("language");
if (documentNode.isBlock() && documentNode.getContext().contains("listing") && language != null && language.equals("cypher")) {
executeBlock(driver, (Block) documentNode);
}
}
private static void executeBlock(Driver driver, Block blockBlock) {
try (var session = driver.session()) {
var source = blockBlock.getSource();
session.run(source).consume();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment