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; }
protected void setAttributesOnBuilder(AttributesBuilder attributesBuilder) throws MojoExecutionException { if (sourceHighlighter != null) { attributesBuilder.sourceHighlighter(sourceHighlighter); } if (embedAssets) { attributesBuilder.linkCss(false); attributesBuilder.dataUri(true); } if (imagesDir != null) { attributesBuilder.imagesDir(imagesDir); } if ("skip".equals(attributeMissing) || "drop".equals(attributeMissing) || "drop-line".equals(attributeMissing)) { attributesBuilder.attributeMissing(attributeMissing); } else { throw new MojoExecutionException(attributeMissing + " is not valid. Must be one of 'skip', 'drop' or 'drop-line'"); } if ("drop".equals(attributeUndefined) || "drop-line".equals(attributeUndefined)) { attributesBuilder.attributeUndefined(attributeUndefined); } else { throw new MojoExecutionException(attributeUndefined + " is not valid. Must be one of 'drop' or 'drop-line'"); } AsciidoctorHelper.addAttributes(attributes, attributesBuilder); if (!attributesChain.isEmpty()) { getLog().info("Attributes: " + attributesChain); attributesBuilder.arguments(attributesChain); } }
@Override public String convertAsciiDocToXHtml(final String asciiDoc) { LOG.info("Converting AsciiDoc to XHTML5..."); final Asciidoctor asciidoctor = create(); final Map<String, Object> attributes = AttributesBuilder.attributes() .unsetStyleSheet() .tableOfContents(Placement.TOP) .asMap(); final Map<String, Object> options = OptionsBuilder.options() .attributes(attributes) .backend("xhtml5") .asMap(); LOG.info("XHTML5 Conversion Complete!"); return asciidoctor.render(asciiDoc, options); }
/** * Creates attributes builder. * * @return atributes builder. */ public static AttributesBuilder attributes(String[] arguments) { AttributesBuilder attributesBuilder = new AttributesBuilder(); attributesBuilder.arguments(arguments); return attributesBuilder; }
protected AttributesBuilder initAttributes(MavenProject project, File siteDirectory) { return AttributesBuilder.attributes() .attribute("idprefix", "@") .attribute("showtitle", "@"); }
@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 copycss_with_in_place_should_copy_css_to_rendered_directory() { Attributes attributes = attributes().linkCss(true).copyCss(true).get(); Options options = options().inPlace(true).safe(SafeMode.UNSAFE) .attributes(attributes).get(); File inputFile = classpath.getResource("rendersample.asciidoc"); asciidoctor.convertFile(inputFile, options); File cssFile = new File(inputFile.getParent(),"asciidoctor.css"); assertThat(cssFile.exists(), is(true)); cssFile.delete(); }
static void runAsciiDoctor(Asciidoctor asciidoctor, Path srcPath, String courseName, Path destPath, PrintWriter err) throws IOException { Path courseDestDir = destPath.resolve(courseName); Attributes attributes = attributes() .tableOfContents(true) .tableOfContents(Placement.LEFT) .attribute("toc-title", courseName) .attribute("numbered", true) .attribute("verbose", true) .attribute("linkcss", "true") .attribute("stylesheet", "../css/style.css") .get(); Options options = options() .safe(SafeMode.UNSAFE) .attributes(attributes) .docType("book") .destinationDir(new File(courseDestDir.toString())) .baseDir(new File(courseDestDir.toString())) .toFile(courseIndexFile(courseDestDir)) .get(); asciidoctor.convertFile(new File(courseADocFile(courseName, courseDestDir).toString()), options); }
public Options parse() { OptionsBuilder optionsBuilder = OptionsBuilder.options(); AttributesBuilder attributesBuilder = AttributesBuilder.attributes(); attributesBuilder.sectionNumbers(this.sectionNumbers); attributesBuilder.attributes(getAttributes()); optionsBuilder.attributes(attributesBuilder.get()); return optionsBuilder.get();
private Attributes buildAttributes(DocletOptions docletOptions, DocErrorReporter errorReporter) { return defaultAttributes() .attributes(new AttributesLoader(asciidoctor, docletOptions, errorReporter).load()) .get(); }
@Test public void unsetting_toc_attribute_table_of_contents_should_not_be_generated() { Attributes attributes = attributes().tableOfContents(false).get(); Options options = options().toFile(false).attributes(attributes).get(); String renderContent = asciidoctor.convertFile( classpath.getResource("tocsample.asciidoc"), options); Document doc = Jsoup.parse(renderContent, "UTF-8"); Elements tocElement = doc.select("div.toc"); assertThat(tocElement.hasClass("toc"), is(false)); }
@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 api_parameters_should_be_transformed_to_cli_command() { AttributesBuilder attributesBuilder = AttributesBuilder.attributes() .attribute("myAtribute", "myValue").sectionNumbers(true) .copyCss(false); OptionsBuilder optionsBuilder = OptionsBuilder.options() .backend("docbook").templateDirs(new File("a"), new File("b")) .safe(SafeMode.UNSAFE).attributes(attributesBuilder.get()); String command = AsciidoctorUtils.toAsciidoctorCommand( optionsBuilder.asMap(), "file.adoc"); String currentDirectory = new File( "" ).getAbsolutePath() + File.separator; String parametersString = command.substring(command.indexOf(" "), command.length()); String[] parameters = parametersString.split(" "); AsciidoctorCliOptions asciidoctorCliOptions = new AsciidoctorCliOptions(); new JCommander(asciidoctorCliOptions, parameters); assertThat(asciidoctorCliOptions.getTemplateDir(), containsInAnyOrder(currentDirectory+"a", currentDirectory+"b")); assertThat(asciidoctorCliOptions.getSafeMode(), is(SafeMode.UNSAFE)); assertThat(asciidoctorCliOptions.getBackend(), is("docbook")); assertThat(asciidoctorCliOptions.getParameters(), containsInAnyOrder("file.adoc")); assertThat(asciidoctorCliOptions.getAttributes(), hasEntry("myAtribute", (Object)"myValue")); assertThat(asciidoctorCliOptions.getAttributes(), hasKey("numbered")); assertThat(asciidoctorCliOptions.getAttributes(), hasKey("copycss!")); }
@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 use_font_awesome_icons() throws Exception { //tag::attributeFontIcons[] String result = asciidoctor.convert( "NOTE: Asciidoctor supports font-based admonition icons!\n" + "\n" + "{foo}", OptionsBuilder.options() .toFile(false) .headerFooter(false) .attributes( AttributesBuilder.attributes() // <1> .icons(Attributes.FONT_ICONS) // <2> .attribute("foo", "bar") // <3> .get()) .get()); assertThat(result, containsString("<i class=\"fa icon-note\" title=\"Note\"></i>")); assertThat(result, containsString("<p>bar</p>")); //end::attributeFontIcons[] }
@Test public void styles_dir_is_set_css_routes_should_use_it() throws IOException { Attributes attributes = attributes().stylesDir("./styles") .linkCss(true).styleSheetName("mycustom.css").get(); Options options = options().inPlace(false).safe(SafeMode.UNSAFE) .toDir(testFolder.getRoot()) .mkDirs(true) .attributes(attributes).get(); asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); Document doc = Jsoup.parse(new File(testFolder.getRoot(), "rendersample.html"), "UTF-8"); Elements link = doc.select("link[href]"); String attr = link.attr("href"); assertThat(attr, is("./styles/mycustom.css")); }
@Test public void shouldRedirectToJUL() throws Exception { final MemoryLogHandler memoryLogHandler = registerMemoryLogHandler(); File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) .safe(SafeMode.SERVER) .attributes( AttributesBuilder.attributes().allowUriRead(true)) .asMap()); File expectedFile = new File(inputFile.getParent(), "documentwithnotexistingfile.html"); expectedFile.delete(); assertEquals(4, memoryLogHandler.getLogRecords().size()); assertThat(memoryLogHandler.getLogRecords().get(0).getMessage(), both(containsString("include file not found")) .and(containsString("documentwithnotexistingfile.adoc: line 3"))); }
AttributesBuilder.attributes().attributeMissing("skip").attributeUndefined("drop"); if (attributes == null) { configureDefaultsAttributes(workDir, log, backend, attrs, emptyMap()); attributes.forEach((k, v) -> { if (v == null) { attrs.attribute(k); } else { attrs.attribute(k, v);
@Test public void linkcss_should_not_embed_css_file() throws IOException { Attributes attributes = attributes().linkCss(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"); Elements link = doc.select("link[href]"); String attr = link.get(1).attr("href"); assertThat(attr, is("./asciidoctor.css")); }
@Test public void should_be_able_to_read_asset() throws FileNotFoundException { Map<String, Object> options = OptionsBuilder.options().safe(SafeMode.SAFE) .attributes(AttributesBuilder.attributes().dataUri(false)) .compact(true).asMap(); Document document = asciidoctor.load(DOCUMENT, options); File inputFile = classpath.getResource("rendersample.asciidoc"); String content = document.readAsset(inputFile.getAbsolutePath(), new HashMap<Object, Object>()); assertThat(content.replace("\r", ""), is(IOUtils.readFull(new FileReader(inputFile)).replace("\r", ""))); }