@Override public void register(Asciidoctor asciidoctor) { asciidoctor.javaExtensionRegistry() .preprocessor(new DefaultAttributesPreprocessor()); asciidoctor.rubyExtensionRegistry() .loadClass(RestDocsExtensionRegistry.class .getResourceAsStream("/extensions/operation_block_macro.rb")) .blockMacro("operation", "OperationBlockMacro"); }
@Override public void register(Asciidoctor asciidoctor) { JavaExtensionRegistry javaExtensionRegistry = asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.postprocessor(ArrowsAndBoxesIncludesPostProcessor.class); javaExtensionRegistry.block("arrowsAndBoxes", ArrowsAndBoxesBlock.class); }
@Test public void currentLineNumberShouldBeReturned() { JavaExtensionRegistry javaExtensionRegistry = asciidoctor .javaExtensionRegistry(); javaExtensionRegistry.preprocessor(NumberLinesPreprocessor.class); File inputFile = classpath.getResource("rendersample.asciidoc"); asciidoctor.convertFile(inputFile, new HashMap<String, Object>()); File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc"); assertThat(outpuFile.exists(), is(true)); }
@Test public void a_block_processor_as_string_should_be_executed_when_registered_block_is_found_in_document() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.block("yell", "org.asciidoctor.extension.YellStaticBlock"); String content = asciidoctor.convertFile( classpath.getResource("sample-with-yell-block.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(1)); assertThat(elements.get(0).text(), is("THE TIME IS NOW. GET A MOVE ON.")); }
@Test public void a_block_processor_should_be_executed_when_registered_block_is_found_in_document() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.block("yell", YellStaticBlock.class); String content = asciidoctor.convertFile( classpath.getResource("sample-with-yell-block.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(1)); assertThat(elements.get(0).text(), is("THE TIME IS NOW. GET A MOVE ON.")); }
@Test public void a_block_processor_should_be_executed_when_registered_listing_block_is_found_in_document() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.block("yell", YellStaticListingBlock.class); String content = asciidoctor.convertFile( classpath.getResource("sample-with-yell-listing-block.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(1)); assertThat(elements.get(0).text(), is("THE TIME IS NOW. GET A MOVE ON.")); }
@Test public void a_block_macro_extension_instance_should_be_executed_when_macro_is_detected() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.blockMacro(new GistMacro("gist", new HashMap<>())); String content = asciidoctor.convertFile( classpath.getResource("sample-with-gist-macro.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element script = doc.getElementsByTag("script").first(); assertThat(script.attr("src"), is("https://gist.github.com/123456.js")); }
@Test public void a_postprocessor_as_string_should_be_executed_after_document_is_rendered() throws IOException { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.postprocessor("org.asciidoctor.extension.CustomFooterPostProcessor"); Options options = options().inPlace(false).toFile(new File(testFolder.getRoot(), "rendersample.html")) .safe(SafeMode.UNSAFE).get(); asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); File renderedFile = new File(testFolder.getRoot(), "rendersample.html"); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); Element footer = doc.getElementById("footer-text"); assertThat(footer.text(), containsString("Copyright Acme, Inc.")); }
@Test public void an_inline_macro_extension_should_be_executed_when_an_inline_macro_is_detected() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.inlineMacro("man", ManpageMacro.class); String content = asciidoctor.convertFile( classpath.getResource("sample-with-man-link.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element link = doc.getElementsByTag("a").first(); assertThat(link.attr("href"), is("gittutorial.html")); }
@Test public void a_block_macro_extension_should_be_executed_when_macro_is_detected() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.blockMacro("gist", GistMacro.class); String content = asciidoctor.convertFile( classpath.getResource("sample-with-gist-macro.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element script = doc.getElementsByTag("script").first(); assertThat(script.attr("src"), is("https://gist.github.com/123456.js")); }
@Test public void an_inline_macro_as_string_extension_should_be_executed_when_an_inline_macro_is_detected() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.inlineMacro("man", "org.asciidoctor.extension.ManpageMacro"); String content = asciidoctor.convertFile( classpath.getResource("sample-with-man-link.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element link = doc.getElementsByTag("a").first(); assertThat(link.attr("href"), is("gittutorial.html")); }
@Test public void a_docinfoprocessor_should_be_executed_and_add_meta_in_header_by_default() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.docinfoProcessor(MetaRobotsDocinfoProcessor.class.getCanonicalName()); String content = asciidoctor.convertFile( classpath.getResource("simple.adoc"), options().headerFooter(true).safe(SafeMode.SERVER).toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element metaRobots = doc.getElementsByAttributeValueContaining("name", "robots").first(); assertThat(metaRobots, is(notNullValue())); }
@Test public void should_create_context_menu() { asciidoctor.javaExtensionRegistry().inlineMacro(ContextMenuInlineMacroProcessor.class); String result = asciidoctor.convert("rightclick:New|Class[]", OptionsBuilder.options().toFile(false).safe(SafeMode.UNSAFE)); assertThat( result, containsString( "<span class=\"menuseq\"><b class=\"menu\">Right click</b> <b class=\"caret\">›</b> <b class=\"submenu\">New</b> <b class=\"caret\">›</b> <b class=\"menuitem\">Class</b></span>")); } }
@Test public void testRegisterClassAsClassWithExplicitName() { final String explicitblockname = "anotherexplicitname"; asciidoctor.javaExtensionRegistry().inlineMacro(explicitblockname, AbstractTestProcessor.class); final String result = asciidoctor.convert(document(explicitblockname, "ExplicitClass"), OptionsBuilder.options()); check("Hello EXPLICIT CLASS", result); }
@Test public void testRegisterClassAsClassWithExplicitName() { final String explicitblockname = "anotherexplicitname"; asciidoctor.javaExtensionRegistry().blockMacro(explicitblockname, AbstractTestProcessor.class); final String result = asciidoctor.convert(document(explicitblockname, "Hello Explicit Class"), OptionsBuilder.options()); check("HELLO EXPLICIT CLASS", result); }
@Test(expected = AsciidoctorCoreException.class) public void testRegisterClassAsInstance() { asciidoctor.javaExtensionRegistry().block(new AbstractTestProcessor()); asciidoctor.convert(document("foo", "Hello Explicit Instance"), OptionsBuilder.options()); }
@Test public void a_include_processor_should_only_handle_its_handles() { JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); javaExtensionRegistry.includeProcessor(UriIncludeProcessor.class); String content = asciidoctor.convertFile(classpath.getResource("sample-with-include.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element contentElement = doc.getElementsByAttributeValue("class", "bare").first(); assertThat(contentElement.text(), startsWith("sample-book.adoc")); }
@Test public void testRegisterClassAsInstanceWithExplicitName() { final String explicitblockname = "aname"; asciidoctor.javaExtensionRegistry().block(explicitblockname, new AbstractTestProcessor()); final String result = asciidoctor.convert(document(explicitblockname, "Hello Explicit Instance"), OptionsBuilder.options()); check("H e l l o E x p l i c i t I n s t a n c e", result); }
@Test public void testRegisterClassWithNameAsInstanceWithExplicitName() { final String blockName = "somename"; asciidoctor.javaExtensionRegistry().block(blockName, new TestProcessorWithName()); final String result = asciidoctor.convert(document(blockName, "Yet Another Test"), OptionsBuilder.options()); check("Y e t A n o t h e r T e s t", result); }
@Test public void testRegisterClassWithNameAsInstanceWithExplicitName() { final String blockName = "somename"; asciidoctor.javaExtensionRegistry().inlineMacro(blockName, new TestProcessorWithName()); final String result = asciidoctor.convert(document(blockName, "YetAnotherTest"), OptionsBuilder.options()); check("Hello YET ANOTHER TEST", result); }