public Options parse() { OptionsBuilder optionsBuilder = OptionsBuilder.options(); AttributesBuilder attributesBuilder = AttributesBuilder.attributes(); optionsBuilder.backend(this.backend).safe(safeMode).docType(doctype).eruby(eruby); optionsBuilder.toFile(new File(outFile)); optionsBuilder.safe(SafeMode.SAFE); optionsBuilder.headerFooter(false); optionsBuilder.compact(this.compact); optionsBuilder.baseDir(new File(this.baseDir)); optionsBuilder.templateEngine(this.templateEngine); optionsBuilder.templateDir(new File(templateDir)); optionsBuilder.toDir(new File(this.destinationDir)); optionsBuilder.inPlace(true); optionsBuilder.option(MONITOR_OPTION_NAME, new HashMap<Object, Object>()); optionsBuilder.attributes(attributesBuilder.get()); return optionsBuilder.get();
@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); }
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 file_document_should_be_rendered_into_foreign_directory_using_options_class() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException { Options options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(testFolder.getRoot()).get(); String renderContent = asciidoctor.convertFile(classpath.getResource("rendersample.asciidoc"), options); File expectedFile = new File(testFolder.getRoot(), "rendersample.html"); assertThat(expectedFile.exists(), is(true)); assertThat(renderContent, is(nullValue())); }
@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); } });
@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", ""))); }
.options() .baseDir(source.getParentFile()) .toFile(output) .backend(backend) .safe(SafeMode.UNSAFE) .docType("book") // todo: config .mkDirs(true); configureDefaultsAttributes(workDir, log, backend, attrs, attributes); optionsBuilder.attributes(attrs); log.debug("Options: " + optionsBuilder.asMap()); asciidoctor.render(wrap(title, version, source), optionsBuilder);
private static Options options(Path templatesFolder, Path baseFolder, Path generatedAssetsTargetFolder) { if (!exists(templatesFolder)) { throw new RuntimeException("templateDir folder does not exist"); } if (!isDirectory(templatesFolder)) { throw new RuntimeException("templateDir folder is not a folder"); } Map<String, Object> attributes = new HashMap<>(); attributes.put("imagesoutdir", generatedAssetsTargetFolder.toString()); attributes.put("outdir", generatedAssetsTargetFolder.toString()); return OptionsBuilder.options() .backend("xhtml5") .safe(UNSAFE) .baseDir(baseFolder.toFile()) .templateDirs(templatesFolder.toFile()) .attributes(attributes) .get(); }
@Test public void string_content_with_icons_enabled_should_be_rendered() throws IOException, SAXException, ParserConfigurationException { InputStream content = new FileInputStream( classpath.getResource("documentwithnote.asciidoc")); Map<String, Object> attributes = attributes().icons( Attributes.IMAGE_ICONS).asMap(); Map<String, Object> options = options().attributes(attributes).asMap(); String result = asciidoctor.convert(toString(content), options); result = result.replaceAll("<img(.*?)>", "<img$1/>"); assertRenderedAdmonitionIcon(result); }
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); }
@Test public void all_files_from_a_collection_should_be_rendered_into_files_and_not_in_array() { Map<String, Object> options = options().inPlace(false).safe(SafeMode.UNSAFE).toDir(testFolder.getRoot()) .asMap(); String[] allRenderedFiles = asciidoctor.convertFiles( Arrays.asList(classpath.getResource("rendersample.asciidoc")), options); assertThat(allRenderedFiles, is(arrayWithSize(0))); }
@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); }
@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())); }
private Map<String, Object> parseAttributes(Reader in, Map<String, Object> existingAttrs) { OptionsBuilder options = OptionsBuilder.options() .safe(SafeMode.SAFE) .attributes(existingAttrs); if (docletOptions.baseDir().isPresent()) { options.baseDir(docletOptions.baseDir().get()); } Map<String, Object> parsed = asciidoctor.readDocumentStructure(in, options.get().map()).getHeader().getAttributes(); // workaround for https://github.com/asciidoctor/asciidoctorj/pull/169 return new HashMap<String, Object>(parsed); }
/** * Updates and OptionsBuilder instance with the options defined in the configuration. * * @param optionsBuilder AsciidoctorJ options to be updated. */ protected void setOptionsOnBuilder(OptionsBuilder optionsBuilder) { optionsBuilder .backend(backend) .safe(SafeMode.UNSAFE) .headerFooter(headerFooter) .eruby(eruby) .mkDirs(true); // Following options are only set when the value is different than the default if (sourcemap) optionsBuilder.option("sourcemap", true); if (catalogAssets) optionsBuilder.option("catalog_assets", true); if (!templateCache) optionsBuilder.option("template_cache", false); if (doctype != null) optionsBuilder.docType(doctype); if (templateEngine != null) optionsBuilder.templateEngine(templateEngine); if (templateDir != null) optionsBuilder.templateDir(templateDir); }
@Test public void shouldWriteFileWithSuffixFromConverterWithAnnotation() throws Exception { asciidoctor.javaConverterRegistry().register(TextConverter.class); File todir = tmp.newFolder(); asciidoctor.convertFile(classpath.getResource("simple.adoc"), OptionsBuilder.options().backend(TextConverter.DEFAULT_FORMAT).toDir(todir).safe(SafeMode.UNSAFE)); assertThat(new File(todir, "simple.html").exists(), is(false)); assertThat(new File(todir, "simple.txt").exists(), is(true)); }