@Test public void a_treeprocessor_instance_should_be_executed_in_document() { this.asciidoctor.createGroup() .treeprocessor(new TerminalCommandTreeprocessor(new HashMap<String, Object>())) .register(); 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() { this.asciidoctor.createGroup() .treeprocessor("org.asciidoctor.extension.TerminalCommandTreeprocessor") .register(); 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() { this.asciidoctor.createGroup() .treeprocessor(TerminalCommandTreeprocessor.class) .register(); 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.createGroup() .treeprocessor(TerminalCommandTreeprocessor.class) .blockMacro("gist", GistMacro.class) .register(); 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"); System.out.println(content); Element script = doc.getElementsByTag("script").first(); assertThat(script.attr("src"), is("https://gist.github.com/42.js")); 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 should_create_toc_with_treeprocessor() throws Exception { this.asciidoctor.createGroup() .treeprocessor(new Treeprocessor() { @Override public Document process(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; } }) .register(); 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)); }