private InputStream processSingleFile(File source, Asciidoctor asciidoctor, ExtensionGroup group, Preprocessor preprocessor, Options options) throws IOException { options.setBaseDir(source.getParentFile().getAbsolutePath()); try(FileInputStream fis= new FileInputStream(source)) { List<String> content = IOUtils.readLines(fis, getConfiguredCharset()); StringBuilder contentBuilder = new StringBuilder(); for (String contentline : content) { if (contentline.contains(INCLUDE) && shouldIncludeStatementRemain(contentline)) contentline = contentline.replace(INCLUDE, "include##"); contentBuilder.append(contentline); contentBuilder.append("\r\n"); } asciidoctor.convert(contentBuilder.toString(), options); group.unregister(); List<String> lines = preprocessor.getContentLines(); StringBuilder newContentBuilder = new StringBuilder(); for (String line : lines) { line = line.replace("include##", INCLUDE); newContentBuilder.append(line); newContentBuilder.append(System.lineSeparator()); } return IOUtils.toInputStream(newContentBuilder.toString(), getConfiguredCharset()); } }
private List<InputStream> convertToInputStreams(List<File> source) throws IOException { List<InputStream> inputStreams = new ArrayList<>(); Asciidoctor asciidoctor = Factory.create(); ExtensionGroup group = asciidoctor.createGroup(); Preprocessor preprocessor = new Preprocessor(); group.preprocessor(preprocessor); Options options = new Options(); options.setToFile(false); options.setSafe(SafeMode.UNSAFE); group.register(); for (File file : source) { inputStreams.add(processSingleFile(file, asciidoctor, group, preprocessor, options)); } return inputStreams; }
@Override public void register(Asciidoctor asciidoctor) { //null means extension is enabled (by default) if(System.getProperty(DISABLE_ALL_EXT_KEY) != null) { return; } ExtensionGroup cukedoctorExtensionGroup = asciidoctor.createGroup(CUKEDOCTOR_EXTENSION_GROUP_NAME); cukedoctorExtensionGroup.postprocessor(CukedoctorScriptExtension.class); if(System.getProperty(FILTER_DISABLE_EXT_KEY) == null){ cukedoctorExtensionGroup.postprocessor(CukedoctorFilterExtension.class); } if(System.getProperty(MINMAX_DISABLE_EXT_KEY) == null){ cukedoctorExtensionGroup.blockMacro("minmax", CukedoctorMinMaxExtension.class); } if(System.getProperty(THEME_DISABLE_EXT_KEY) == null){ cukedoctorExtensionGroup.postprocessor(CukedoctorThemeExtension.class); } if(System.getProperty(FOOTER_DISABLE_EXT_KEY) == null){ cukedoctorExtensionGroup.postprocessor(CukedoctorFooterExtension.class); } if(System.getProperty(STYLE_DISABLE_EXT_KEY) == null){ cukedoctorExtensionGroup.postprocessor(CukedoctorStyleExtension.class); } cukedoctorExtensionGroup.register(); }
@Test public void ruby_block_macro_processor_should_be_registered_with_block_name() { this.asciidoctor.createGroup() .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/gist-block-macro.rb")).rubyBlockMacro("mygist", "GistBlockMacro") .register(); String content = asciidoctor.convert( ".My Gist\n" + "mygist::123456[]", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByTag("script"); assertThat(elements.size(), is(1)); assertThat(elements.get(0).attr("src"), is("https://gist.github.com/123456.js")); }
.postprocessor(CustomFooterPostProcessor.class); extensionGroup.register(); Options options = options().inPlace(false).toFile(new File(testFolder.getRoot(), "rendersample.html")) .safe(SafeMode.UNSAFE).get(); extensionGroup.unregister();;
.loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")) .rubyBlock("rubyyell", "YellRubyBlock"); extensionGroup.register(); String content = asciidoctor.convertFile( classpath.getResource("sample-with-ruby-yell-block.ad"), extensionGroup.unregister();
YellBlock yellBlock = new YellBlock("yell", config); ExtensionGroup extensionGroup = this.asciidoctor.createGroup().block(yellBlock); extensionGroup.register(); String content = asciidoctor.convertFile( classpath.getResource("sample-with-yell-block.ad"), extensionGroup.unregister(); String contentWithoutBlock = asciidoctor.convertFile( classpath.getResource("sample-with-yell-block.ad"),
@Test public void an_inner_class_should_be_registered() { TestHttpServer.start(Collections.singletonMap("http://example.com/asciidoctorclass.rb", classpath.getResource("org/asciidoctor/internal/asciidoctorclass.rb"))); this.asciidoctor.createGroup() .includeProcessor(new RubyIncludeSource(new HashMap<String, Object>())) .register(); String content = asciidoctor.convertFile( classpath.getResource("sample-with-uri-include.ad"), options().toFile(false).get()); org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8"); Element contentElement = doc.getElementsByAttributeValue("class", "language-ruby").first(); assertThat(contentElement.text(), startsWith(ASCIIDOCTORCLASS_PREFIX)); }
@Test public void a_block_macro_extension_instance_should_be_executed_when_macro_is_detected() { this.asciidoctor.createGroup() .blockMacro(new GistMacro("gist", new HashMap<String, Object>())) .register(); 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 { this.asciidoctor.createGroup() .postprocessor("org.asciidoctor.extension.CustomFooterPostProcessor") .register(); 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 a_block_processor_should_be_executed_when_registered_listing_block_is_found_in_document() throws IOException { this.asciidoctor.createGroup() .block("yell", YellStaticListingBlock.class) .register(); 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 an_inline_macro_as_string_extension_should_be_executed_when_an_inline_macro_is_detected() { this.asciidoctor.createGroup() .inlineMacro("man", "org.asciidoctor.extension.ManpageMacro") .register(); 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_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 @Ignore public void extensions_should_be_correctly_added_using_extension_registry() throws IOException { // To avoid registering the same extension over and over for all tests, // service is instantiated manually. this.asciidoctor.createGroup() .postprocessor(ArrowsAndBoxesIncludesPostProcessor.class) .block("arrowsAndBoxes", ArrowsAndBoxesBlock.class) .register(); Options options = options().inPlace(false).toFile(new File(testFolder.getRoot(), "rendersample.html")) .safe(SafeMode.UNSAFE).get(); asciidoctor.convertFile(classpath.getResource("arrows-and-boxes-example.ad"), options); File renderedFile = new File(testFolder.getRoot(), "rendersample.html"); org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8"); Element arrowsJs = doc.select("script[src=http://www.headjump.de/javascripts/arrowsandboxes.js").first(); assertThat(arrowsJs, is(notNullValue())); Element arrowsCss = doc.select("link[href=http://www.headjump.de/stylesheets/arrowsandboxes.css").first(); assertThat(arrowsCss, is(notNullValue())); Element arrowsAndBoxes = doc.select("pre[class=arrows-and-boxes").first(); assertThat(arrowsAndBoxes, is(notNullValue())); }
@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 ruby_inline_macro_processor_should_be_registered_with_macro_name() { this.asciidoctor.createGroup() .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")).rubyInlineMacro("myman", "ManInlineMacro") .register(); String content = asciidoctor.convert( "= Man Inline Macro Extension\n" + "\n" + "See myman:gittutorial[7] to get started.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByTag("a"); assertThat(elements.size(), is(1)); assertThat(elements.get(0).attr("href"), is("gittutorial.html")); }
@Test public void ruby_extension_should_be_registered_with_implicit_block_name() { this.asciidoctor.createGroup() .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).rubyBlock("YellRubyBlock") .register(); String content = asciidoctor.convert( "= Block Yell Example\n" + "\n" + "content\n" + "\n" + "[yell]\n" + "The time is now. Get a move on.", options().toFile(false).get()); Document doc = Jsoup.parse(content, "UTF-8"); Elements elements = doc.getElementsByClass("paragraph"); assertThat(elements.size(), is(2)); assertThat(elements.get(1).text(), is("THE TIME IS NOW! GET A MOVE ON!")); }
@Test public void ruby_postprocessor_should_be_registered() { final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath(); final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath)); asciidoctor.createGroup() .requireRubyLibrary("xml-entity-postprocessor.rb") .rubyPostprocessor("XmlEntityPostprocessor") .register(); String content = asciidoctor.convert( "Read §2 and it'll all be clear.", options().toFile(false).get()); System.out.println(content); assertThat(content, containsString("Read §2 and it'll all be clear.")); }
@Test public void ruby_includeprocessor_should_be_registered() { asciidoctor.createGroup() .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb")) .rubyIncludeProcessor("ResponseIncludeProcessor") .register(); String content = asciidoctor.convert( "The response to everything is\n" + "\n" + "include::response[]" + "", options().toFile(false).safe(SafeMode.SAFE).get()); final Document document = Jsoup.parse(content); assertThat( document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(), is("<p>42</p>")); }
@Test public void ruby_treeprocessor_should_be_registered() { this.asciidoctor.createGroup() .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/shell-session-tree-processor.rb")) .rubyTreeprocessor("ShellSessionTreeProcessor") .register(); String content = this.asciidoctor.convert( " $ echo \"Hello, World!\"\n" + " > Hello, World!\n" + "\n" + " $ gem install asciidoctor", options().toFile(false).get()); final Document document = Jsoup.parse(content); final TextNode commandElement = document.getElementsByClass("command").get(0).textNodes().get(0); assertThat(commandElement.getWholeText(), is("echo \"Hello, World!\"")); final TextNode commandElement2 = document.getElementsByClass("command").get(1).textNodes().get(0); assertThat(commandElement2.getWholeText(), is("gem install asciidoctor")); }