private Options getAsciiDocOptionsAndAttributes(ParserContext context) { JBakeConfiguration config = context.getConfig(); List<String> asciidoctorAttributes = config.getAsciidoctorAttributes(); final AttributesBuilder attributes = attributes(asciidoctorAttributes.toArray(new String[asciidoctorAttributes.size()])); if (config.getExportAsciidoctorAttributes()) { final String prefix = config.getAttributesExportPrefixForAsciidoctor(); for (final Iterator<String> it = config.getKeys(); it.hasNext(); ) { final String key = it.next(); if (!key.startsWith("asciidoctor")) { attributes.attribute(prefix + key.replace(".", "_"), config.get(key)); } } } final List<String> optionsSubset = config.getAsciidoctorOptionKeys(); final Options options = options().attributes(attributes.get()).get(); for (final String optionKey : optionsSubset) { Object optionValue = config.getAsciidoctorOption(optionKey); if (optionKey.equals(Options.TEMPLATE_DIRS)) { List<String> dirs = getAsList(optionValue); if (!dirs.isEmpty()) { options.setTemplateDirs(String.valueOf(dirs)); } } else { options.setOption(optionKey, optionValue); } } options.setBaseDir(context.getFile().getParentFile().getAbsolutePath()); options.setSafe(UNSAFE); return options; }
@Test public void should_preload_open_cache_uri_gem() throws IOException { Attributes attributes = attributes().cacheUri(true).get(); String content = asciidoctor.convert("read my lips", OptionsBuilder.options().attributes(attributes)); assertThat(content, is(notNullValue())); }
@Test public void experimental_flag_should_enable_experimental_features_like_keyboard_shortcuts() { Attributes attributes = attributes().experimental(true).get(); Options options = options().attributes(attributes).get(); String content = asciidoctor.convert("kbd:[F11]", options); Document doc = Jsoup.parse(content); Elements image = doc.select("kbd"); assertThat(image.text(), is("F11")); }
@Test public void qualified_http_url_inline_with_hide_uri_scheme_set() throws IOException { Attributes attributes = attributes().hiddenUriScheme(true).get(); String content = asciidoctor.convert("The AsciiDoc project is located at http://asciidoc.org.", OptionsBuilder.options().attributes(attributes)); Document doc = Jsoup.parse(content, "UTF-8"); Element link = doc.getElementsByTag("a").first(); assertThat(link.text(), is("asciidoc.org")); }
@Test public void attribute_missing_should_drop_line_should_drop_line_with_reference_to_missing_attribute_if_attribute_missing_attribute_is_drop_line() { Attributes attributes = attributes().attributeMissing("drop-line").get(); Options options = options().attributes(attributes).get(); String renderContent = asciidoctor.convert("This is\n" + "blah blah {foobarbaz}\n" + "all there is.", options); assertThat(renderContent, not(containsString("{foobarbaz}"))); }
@Test public void compat_mode_should_change_how_document_is_rendered_to_legacy_system() { Attributes attributes = attributes().attribute("version", "1.0.0").compatMode(CompatMode.LEGACY).get(); String content = asciidoctor.convert("The `AsciiDoc {version}` project.", OptionsBuilder.options().attributes(attributes)); Document doc = Jsoup.parse(content, "UTF-8"); Element code = doc.getElementsByTag("code").first(); assertThat(code.text(), containsString("{version}")); }
@Test public void attribute_undefined_should_not_drop_line_with_attribute_unassignment_if_attribute_undefined_is_drop() { Attributes attributes = attributes().attributeUndefined("drop").get(); Options options = options().attributes(attributes).get(); String renderContent = asciidoctor.convert(":foo:\n\n{set:foo!}\n{foo}yes", options); Document doc = Jsoup.parse(renderContent, "UTF-8"); Element paragraph = doc.getElementsByTag("p").first(); assertThat(paragraph.text(), is("{foo}yes")); }
@Test public void set_anchors_attribute_should_add_anchor_to_sections() { Attributes attributes = attributes().setAnchors(true).get(); Options options = options().inPlace(false).toFile(false).attributes(attributes).get(); String content = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); Document doc = Jsoup.parse(content, "UTF-8"); Element anchorElement = doc.select("a[class=anchor]").first(); assertThat(anchorElement.attr("href"), is("#_section_a")); }
@Test public void copycss_with_render_to_file_should_copy_css_to_to_file_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); Options options = options().inPlace(false) .toFile(new File(testFolder.getRoot(), "output.html")) .safe(SafeMode.UNSAFE).attributes(attributes).get(); asciidoctor.convert("This is Asciidoctor", options); File cssFile = new File(testFolder.getRoot(), "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); }
@Test public void show_title_true_attribute_should_show_title_on_standalone_document() { final Options options = options() .attributes(attributes().showTitle(true).get()) .toFile(false) .headerFooter(true) .get(); final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); assertEquals(1, doc.getElementsByTag("h1").size()); assertEquals("Document Title", doc.getElementsByTag("h1").get(0).text()); }
@Test public void should_skip_front_matter_if_specified_by_skip_front_matter_attribute() throws IOException { Attributes attributes = attributes().skipFrontMatter(true).get(); Options options = options().toFile(false).inPlace(false).attributes(attributes).get(); String content = asciidoctor.convertFile(classpath.getResource("renderwithfrontmatter.adoc"), options); Document doc = Jsoup.parse(content, "UTF-8"); Elements hrElements = doc.getElementsByTag("hr"); assertThat(hrElements.size(), is(0)); }
@Test public void should_add_AsciiMath_delimiters_around_math_block_content_if_math_attribute_not_latexmath() throws IOException { Attributes attributes = attributes().math("asciimath").get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) .toDir(testFolder.getRoot()).attributes(attributes).get(); asciidoctor.convertFile(classpath.getResource("math.asciidoc"), options); Document doc = Jsoup.parse(new File(testFolder.getRoot(), "math.html"), "UTF-8"); assertThat(doc.getElementsByAttributeValue("type", "text/x-mathjax-config").size(), is(1)); }
@Test public void no_footer_attribute_should_not_show_footer_info() throws IOException { Attributes attributes = attributes().noFooter(true).get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) .toDir(testFolder.getRoot()).attributes(attributes).get(); asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); Document doc = Jsoup.parse(new File(testFolder.getRoot(), "rendersample.html"), "UTF-8"); assertThat(doc.getElementById("footer"), is(nullValue())); }
@Test public void show_title_false_then_true_attribute_should_show_title_on_embedded_document() { final Options options = options() .attributes(attributes().showTitle(false).showTitle(true).get()) .toFile(false) .headerFooter(false) .get(); final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); assertEquals(1, doc.getElementsByTag("h1").size()); assertEquals("Document Title", doc.getElementsByTag("h1").get(0).text()); }
@Test public void sect_num_levels_attribute_should_only_number_levels_up_to_value_defined_by_sectnumlevels_attribute() throws IOException { Attributes attributes = attributes().sectionNumbers(true).sectNumLevels(2).get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) .toDir(testFolder.getRoot()).attributes(attributes).get(); asciidoctor.convertFile(classpath.getResource("multiple_levels.asciidoc"), options); Document doc = Jsoup.parse(new File(testFolder.getRoot(), "multiple_levels.html"), "UTF-8"); assertThat(doc.getElementById("_level_1").text(), startsWith("1.")); assertThat(doc.getElementById("_level_2").text(), startsWith("1.1.")); assertThat(doc.getElementById("_level_3").text(), not(startsWith("1.1.1."))); assertThat(doc.getElementById("_level_4").text(), not(startsWith("1.1.1.1."))); }
@Test public void copycss_with_render_to_dir_should_copy_css_to_to_dir_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); Options options = options().inPlace(false).toDir(testFolder.getRoot()) .safe(SafeMode.UNSAFE).attributes(attributes).get(); asciidoctor.convert("This is Asciidoctor", options); File cssFile = new File(testFolder.getRoot(), "asciidoctor.css"); assertThat(cssFile.exists(), is(true)); }
@Test public void show_title_false_attribute_should_hide_title_on_embedded_document() { final Options options = options() .attributes(attributes().showTitle(false).get()) .toFile(false) .headerFooter(false) .get(); final Document doc = Jsoup.parse(asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options)); assertEquals(0, doc.getElementsByTag("h1").size()); }
@Test public void coderay_gem_should_be_preloaded() { Map<String, Object> options = OptionsBuilder.options() .attributes(AttributesBuilder.attributes().sourceHighlighter("coderay").get()).asMap(); ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'coderay'"); assertThat(evalScriptlet.isFalse(), is(true)); }
@Test public void should_use_custom_appendix_caption_if_specified() throws IOException { Attributes attributes = attributes().appendixCaption("App").get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) .toDir(testFolder.getRoot()).attributes(attributes).get(); asciidoctor.convertFile(classpath.getResource("appendix.asciidoc"), options); Document doc = Jsoup.parse(new File(testFolder.getRoot(), "appendix.html"), "UTF-8"); Element attributeElement = doc.getElementById("_attribute_options"); assertThat(attributeElement.text(), startsWith("App")); }
@Test public void data_uri_gem_should_be_preloaded() { Map<String, Object> options = OptionsBuilder.options() .attributes(AttributesBuilder.attributes().dataUri(true).get()).asMap(); ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'base64'"); assertThat(evalScriptlet.isFalse(), is(true)); }