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; }
/** * Adds an attribute into a {@link AttributesBuilder} taking care of Maven's XML parsing special cases like * toggles toggles, nulls, etc. * * @param attribute Asciidoctor attribute name * @param value Asciidoctor attribute value * @param attributesBuilder AsciidoctorJ AttributesBuilder */ public static void addAttribute(String attribute, Object value, AttributesBuilder attributesBuilder) { // NOTE Maven interprets an empty value as null, so we need to explicitly convert it to empty string (see #36) // NOTE In Asciidoctor, an empty string represents a true value if (value == null || "true".equals(value)) { attributesBuilder.attribute(attribute, ""); } // NOTE a value of false is effectively the same as a null value, so recommend the use of the string "false" else if ("false".equals(value)) { attributesBuilder.attribute(attribute, null); } // NOTE Maven can't assign a Boolean value from the XML-based configuration, but a client may else if (value instanceof Boolean) { attributesBuilder.attribute(attribute, Attributes.toAsciidoctorFlag((Boolean) value)); } else { // Can't do anything about dates and times because all that logic is private in Attributes attributesBuilder.attribute(attribute, value); } }
case "html5": if (!attributes.containsKey("stylesheet")) { attrs.attribute("stylesheet", "talend.css"); attrs.attribute("stylesdir", new File(extractedResources, "resources/html").getAbsolutePath()); attrs.attribute("data-uri"); attrs.attribute("pdf-style", "talend.yml"); attrs.attribute("pdf-stylesdir", new File(extractedResources, "resources/pdf").getAbsolutePath()); attrs.attribute("icons", "font"); attrs.attribute("source-highlighter", "coderay"); attrs.attribute("toc", "left");
private static AttributesBuilder defaultAttributes() { return AttributesBuilder.attributes() .attribute("at", "@") .attribute("slash", "/") .attribute("icons", null) .attribute("idprefix", "") .attribute("idseparator", "-") .attribute("javadoc", "") .attribute("showtitle", true) .attribute("source-highlighter", "coderay") .attribute("coderay-css", "class") .attribute("env-asciidoclet") .attribute("env", "asciidoclet"); }
attributes.attribute(((String) entry.getKey()).replaceAll("\\.", "-"), entry.getValue());
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); }
@Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { final String fileName = file.getFileName().toString(); if (fileName.endsWith(".adoc")) { pool.submit(() -> { final String path = sourceBase.relativize(file).toString(); final File target = new File(targetBase, path.substring(0, path.length() - "adoc".length()) + "pdf"); final File asFile = file.toFile(); final Map<String, Object> attributes = asciidoctor.readDocumentHeader(asFile).getAttributes(); // if we generate the PDF link we need to create the PDF excepted if it is expected to be manual if (attributes.containsKey("jbake-meecrowavepdf") && !attributes.containsKey("jbake-meecrowavepdf-manual")) { target.getParentFile().mkdirs(); asciidoctor.convertFile( asFile, options() //.baseDir(asFile.getParentFile()) .safe(UNSAFE) .backend("pdf") .attributes(AttributesBuilder.attributes() .attribute("source-highlighter", "coderay") .attribute("context_rootpath", "http://openwebbeans.apache.org/meecrowave")) .toFile(target).get()); System.out.println("Generated " + target); } }); } return super.visitFile(file, attrs); } });
@Override protected void doParse(RuleSource source, RuleSetBuilder ruleSetBuilder) throws RuleException { String content; try (InputStream stream = source.getInputStream()) { content = IOUtils.toString(stream); } catch (IOException e) { throw new RuleException("Cannot parse AsciiDoc document from " + source.getId(), e); } OptionsBuilder optionsBuilder = options().mkDirs(true).safe(SafeMode.UNSAFE).baseDir(tempDir) .attributes(attributes().attribute(AsciidoctorFactory.ATTRIBUTE_IMAGES_OUT_DIR, tempDir.getAbsolutePath()).experimental(true)); getAsciidoctor().load(content, optionsBuilder.asMap()); extractRules(source, singletonList(treeprocessor.getDocument()), ruleSetBuilder); }
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 no_compat_mode_should_change_how_document_is_rendered_to_new_system() { Attributes attributes = attributes().attribute("version", "1.0.0").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("1.0.0")); }
@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 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!")); }