@Override public void register(Asciidoctor asciidoctor) { JavaExtensionRegistry javaExtensionRegistry = asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.treeprocessor(RemotePlantUMLTreeprocessor.class); }
/** * Returns an {@link Asciidoctor} instance which is created lazliy to reduce * startup time. * * @return The {@link Asciidoctor} instance. */ private Asciidoctor getAsciidoctor() { if (asciidoctor == null) { asciidoctor = AsciidoctorFactory.getAsciidoctor(); treeprocessor = new Treeprocessor(); IgnoreIncludeProcessor includeProcessor = new IgnoreIncludeProcessor(); JavaExtensionRegistry extensionRegistry = asciidoctor.javaExtensionRegistry(); extensionRegistry.treeprocessor(treeprocessor); extensionRegistry.includeProcessor(includeProcessor); } return asciidoctor; }
@Test public void createAstStructure() throws Exception { asciidoctor.javaExtensionRegistry().treeprocessor(ASTExtractorTreeprocessor.class); Document document = asciidoctor.loadFile(classpathResources.getResource("ast-demo.adoc"), OptionsBuilder.options().asMap()); assertThat( ASTExtractorTreeprocessor.result.toString(), is(IOUtils.readFull(new FileReader(classpathResources.getResource("ast-demo-result.txt"))).replaceAll("\\r", ""))); }
@Test public void a_treeprocessor_instance_should_be_executed_in_document() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.treeprocessor(new TerminalCommandTreeprocessor(new HashMap<>())); String content = asciidoctor.convertFile( classpath.getResource("sample-with-terminal-command.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element contentElement = doc.getElementsByAttributeValue("class", "command").first(); assertThat(contentElement.text(), is("echo \"Hello, World!\"")); contentElement = doc.getElementsByAttributeValue("class", "command").last(); assertThat(contentElement.text(), is("gem install asciidoctor")); }
@Test public void a_treeprocessor_should_be_executed_in_document() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.treeprocessor(TerminalCommandTreeprocessor.class); String content = asciidoctor.convertFile( classpath.getResource("sample-with-terminal-command.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element contentElement = doc.getElementsByAttributeValue("class", "command").first(); assertThat(contentElement.text(), is("echo \"Hello, World!\"")); contentElement = doc.getElementsByAttributeValue("class", "command").last(); assertThat(contentElement.text(), is("gem install asciidoctor")); }
@Test public void a_treeprocessor_as_string_should_be_executed_in_document() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.treeprocessor("org.asciidoctor.extension.TerminalCommandTreeprocessor"); String content = asciidoctor.convertFile( classpath.getResource("sample-with-terminal-command.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element contentElement = doc.getElementsByAttributeValue("class", "command").first(); assertThat(contentElement.text(), is("echo \"Hello, World!\"")); contentElement = doc.getElementsByAttributeValue("class", "command").last(); assertThat(contentElement.text(), is("gem install asciidoctor")); }
@Test public void a_treeprocessor_and_blockmacroprocessor_should_be_executed_in_document() { this.asciidoctor.javaExtensionRegistry() .treeprocessor(TerminalCommandTreeprocessor.class) .blockMacro("gist", GistMacro.class); String content = asciidoctor.convertFile( classpath.getResource("sample-with-terminal-command-and-gist-macro.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element contentElement = doc.getElementsByAttributeValue("class", "command").first(); assertThat(contentElement.text(), is("echo \"Hello, World!\"")); contentElement = doc.getElementsByAttributeValue("class", "command").last(); assertThat(contentElement.text(), is("gem install asciidoctor")); Element script = doc.getElementsByTag("script").first(); assertThat(script.attr("src"), is("https://gist.github.com/42.js")); }
@Test public void should_create_toc_with_treeprocessor() { asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() { @Override public org.asciidoctor.ast.Document process(org.asciidoctor.ast.Document document) { List<StructuralNode> blocks = document.getBlocks(); for (StructuralNode block : blocks) { for (StructuralNode block2 : block.getBlocks()) { if (block2 instanceof Section) System.out.println(((Section) block2).getId()); } } return document; } }); String content = asciidoctor.convertFile( classpath.getResource("documentwithtoc.adoc"), options().headerFooter(true).toFile(false).safe(SafeMode.UNSAFE).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element toc = doc.getElementById("toc"); assertThat(toc, notNullValue()); Elements elements = toc.getElementsByAttributeValue("href", "#TestId"); assertThat(elements.size(), is(1)); }
@Test public void should_process_terminal_listings() { File referenceDocument = classpathResources.getResource("treeprocessorresult.adoc"); String referenceResult = asciidoctor.convertFile( referenceDocument, OptionsBuilder.options() .headerFooter(false) .toFile(false)); //tag::include[] File src = //... //end::include[] classpathResources.getResource("treeprocessorcontent.adoc"); //tag::include[] asciidoctor.javaExtensionRegistry() .treeprocessor(TerminalCommandTreeprocessor.class); // <1> String result = asciidoctor.convertFile( src, OptionsBuilder.options() .headerFooter(false) .toFile(false)); //end::include[] assertThat(result, is(referenceResult)); }