org.asciidoctor.Asciidoctor Java Examples

The following examples show how to use org.asciidoctor.Asciidoctor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CukedoctorMojo.java    From cukedoctor with Apache License 2.0 6 votes vote down vote up
private void generateDocumentation(DocumentAttributes documentAttributes, File adocFile, Asciidoctor asciidoctor) {
    
    OptionsBuilder ob = OptionsBuilder.options()
            .safe(SafeMode.UNSAFE)
            .backend(documentAttributes.getBackend())
            .attributes(documentAttributes.toMap());
    getLog().info("Document attributes:\n"+documentAttributes.toMap());
    ExtensionGroup cukedoctorExtensionGroup = asciidoctor.createGroup(CUKEDOCTOR_EXTENSION_GROUP_NAME);
    if ("pdf".equals(documentAttributes.getBackend())) {
        cukedoctorExtensionGroup.unregister();
        //remove auxiliary files
        FileUtil.removeFile(adocFile.getParent() + "/" + outputFileName + "-theme.yml");
    }
    asciidoctor.convertFile(adocFile, ob);

    getLog().info("Generated documentation at: " + adocFile.getParent());
}
 
Example #2
Source File: Swagger2ToDocTest.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void convertV1Doc() {
	Asciidoctor asciidoctor = create();
	Options options = new Options();
	options.setInPlace(true);
	options.setBackend("html5");
	options.setSafe(SafeMode.SAFE);
	options.setToDir(baseOutputDir + "/html5/v1");
	options.setMkDirs(true);
	options.setDocType("docbook");

	Map<String, Object> attributes = new HashMap<String, Object>();
	attributes.put("toc", "left");
	attributes.put("toclevels", "3");
	attributes.put("generated", "./generated/v1");
	options.setAttributes(attributes);

	asciidoctor.convertFile(new File(baseOutputDir + "/index.adoc"), options);
}
 
Example #3
Source File: Swagger2ToDocTest.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void convertV2Doc() {
	Asciidoctor asciidoctor = create();
	Options options = new Options();
	options.setInPlace(true);
	options.setBackend("html5");
	options.setSafe(SafeMode.SAFE);
	options.setToDir(baseOutputDir + "/html5/v2");
	options.setMkDirs(true);
	options.setDocType("docbook");

	Map<String, Object> attributes = new HashMap<String, Object>();
	attributes.put("toc", "left");
	attributes.put("toclevels", "3");
	attributes.put("generated", "./generated/v2");
	options.setAttributes(attributes);

	asciidoctor.convertFile(new File(baseOutputDir + "/index.adoc"), options);
}
 
Example #4
Source File: TextConverterTest.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
    public void should_use_text_converter_for_conversion(@ArquillianResource Asciidoctor asciidoctor) {

//tag::include[]
        File test_adoc = //...
//end::include[]
            classpathResources.getResource("textconvertertest.adoc");

//tag::include[]

        asciidoctor.javaConverterRegistry().register(TextConverter.class); // <1>

        String result = asciidoctor.convertFile(
                test_adoc,
                OptionsBuilder.options()
                        .backend("text")                                   // <2>
                        .toFile(false));

//end::include[]

        verifyResult(result);
    }
 
Example #5
Source File: AsciidocEngine.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance of {@link AsciidocEngine}.
 * @param backend the name of the backend
 * @param libraries the asciidoctor libraries to use
 * @param attributes the asciidoctor attributes to use
 * @param imagesdir the images to use
 */
public AsciidocEngine(String backend,
                      List<String> libraries,
                      Map<String, Object> attributes,
                      String imagesdir){
    checkNonNullNonEmpty(backend, BACKEND_PROP);
    installSLF4JBridge();
    this.backend = backend;
    this.attributes = attributes == null ? Collections.emptyMap() : attributes;
    this.libraries = libraries == null ? Collections.emptyList() : libraries;
    this.imagesdir = imagesdir == null ? DEFAULT_IMAGESDIR : imagesdir;
    if (asciidoctorInstance == null) {
        asciidoctorInstance = Asciidoctor.Factory.create();
    }
    this.asciidoctor = asciidoctorInstance;
    new AsciidocExtensionRegistry(backend).register(asciidoctor);
}
 
Example #6
Source File: CukedoctorPublisher.java    From cucumber-living-documentation-plugin with MIT License 6 votes vote down vote up
private Runnable run(final List<Feature> features, final DocumentAttributes attrs, final CukedoctorConfig cukedoctorConfig, final String outputPath) {
    return new Runnable() {

        @Override
        public void run() {
            Asciidoctor asciidoctor = null;
            try {
                asciidoctor = Asciidoctor.Factory.create(CukedoctorPublisher.class.getClassLoader());
                generateDocumentation(features, attrs, cukedoctorConfig, outputPath, asciidoctor);
            } catch (Exception e) {
                e.printStackTrace();
                final String errorMessage = String.format("Unexpected error on documentation generation, message %s, cause %s", e.getMessage(), e.getCause());
                throw new RuntimeException(errorMessage);
            } finally {
                if (asciidoctor != null) {
                    asciidoctor.shutdown();
                }
            }
        }
    };

}
 
Example #7
Source File: WhenRubyExtensionGroupIsRegistered.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
public void ruby_postprocessor_should_be_registered() {

    final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
    final Asciidoctor asciidoctor = JRubyAsciidoctor.create(singletonList(rubyExtPath));
    asciidoctor.createGroup()
        .requireRubyLibrary("xml-entity-postprocessor.rb")
        .rubyPostprocessor("XmlEntityPostprocessor")
        .register();

    String content = asciidoctor.convert(
        "Read &sect;2 and it&apos;ll all be clear.",
        options().toFile(false).get());

    System.out.println(content);
    assertThat(content, containsString("Read &#167;2 and it&#39;ll all be clear."));
}
 
Example #8
Source File: WhenRubyExtensionGroupIsRegistered.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void ruby_docinfoprocessor_should_be_registered() {

    final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
    final Asciidoctor asciidoctor = JRubyAsciidoctor.create(singletonList(rubyExtPath));
    asciidoctor.createGroup()
        .requireRubyLibrary("view-result-docinfoprocessor.rb")
        .rubyDocinfoProcessor("ViewResultDocinfoProcessor")
        .register();

    String content = asciidoctor.convert(
        "= View Result Sample             \n" +
            "                                 \n" +
            ".This will have a link next to it\n" +
            "----                             \n" +
            "* always displayed               \n" +
            "* always displayed 2             \n" +
            "----                             \n" +
            "                                 \n" +
            "[.result]                        \n" +
            "====                             \n" +
            "* hidden till clicked            \n" +
            "* hidden till clicked 2          \n" +
            "====                             ",
        options().toFile(false).safe(SafeMode.SAFE).headerFooter(true).get());

    final Document document = Jsoup.parse(content);
    final Iterator<Element> elems = document.getElementsByTag("style").iterator();
    boolean found = false;
    while (elems.hasNext()) {
        final Element styleElem = elems.next();
        if (styleElem.toString().contains(".listingblock a.view-result")) {
            found = true;
        }
    }
    assertTrue("Could not find style element that should have been added by docinfo processor:\n" + document, found);
}
 
Example #9
Source File: AsciidoctorInvoker.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
private String renderInput(Asciidoctor asciidoctor, Options options, List<File> inputFiles) {

        if(inputFiles.size() == 1 && "-".equals(inputFiles.get(0).getName())) {
            return asciidoctor.convert(readInputFromStdIn(), options);
        }

        StringBuilder output = new StringBuilder();

        for (File inputFile : inputFiles) {

            if (inputFile.canRead()) {

                String renderedFile = asciidoctor
                        .convertFile(inputFile, options);
                if (renderedFile != null) {
                    output.append(renderedFile).append(
                            System.getProperty("line.separator"));
                }
            } else {
                System.err.println("asciidoctor: FAILED: input file(s) '"
                        + inputFile.getAbsolutePath()
                        + "' missing or cannot be read");
                throw new IllegalArgumentException(
                        "asciidoctor: FAILED: input file(s) '"
                                + inputFile.getAbsolutePath()
                                + "' missing or cannot be read");
            }
        }

        return output.toString();
    }
 
Example #10
Source File: ArrowsAndBoxesExtension.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Asciidoctor asciidoctor) {
    
    JavaExtensionRegistry javaExtensionRegistry = asciidoctor.javaExtensionRegistry();
    javaExtensionRegistry.postprocessor(ArrowsAndBoxesIncludesPostProcessor.class);
    javaExtensionRegistry.block("arrowsAndBoxes", ArrowsAndBoxesBlock.class);
    
}
 
Example #11
Source File: CukedoctorPublisher.java    From cucumber-living-documentation-plugin with MIT License 5 votes vote down vote up
protected synchronized void generateDocumentation(List<Feature> features, DocumentAttributes attrs, CukedoctorConfig cukedoctorConfig, String outputPath, Asciidoctor asciidoctor) {
    asciidoctor.unregisterAllExtensions();
    if (!attrs.getBackend().equalsIgnoreCase("pdf")) {
        new CukedoctorExtensionRegistry().register(asciidoctor);
    }
    CukedoctorConverter converter = Cukedoctor.instance(features, attrs, cukedoctorConfig);
    String doc = converter.renderDocumentation();
    File adocFile = FileUtil.saveFile(outputPath + "/documentation.adoc", doc);
    asciidoctor.convertFile(adocFile, OptionsBuilder.options().backend(attrs.getBackend()).safe(SafeMode.SAFE).asMap());
}
 
Example #12
Source File: WhenClassloaderIsRequired.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void run() {
    try {
        if (classloader == null) {
            Asciidoctor.Factory.create();
        } else {
            JRubyAsciidoctor.create(classloader);
        }
        loadingsucceeded = true;
    } catch (org.jruby.exceptions.RaiseException exp) {
        loadingsucceeded = false;
    }
}
 
Example #13
Source File: MyResource.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@GET
@Produces(MediaType.TEXT_HTML)
public String get() {
    Asciidoctor asciidoctor = Asciidoctor.Factory.create();
    try {
        return asciidoctor.render("Hello World\n-----------", Collections.emptyMap());
    } finally {
        asciidoctor.shutdown();
    }
}
 
Example #14
Source File: AsciidoctorInterface.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
    public void autocloseAsciidoctorInstance() {
//tag::autoclose[]
        try (Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
            asciidoctor.convert("Hello World", OptionsBuilder.options());
        }
//end::autoclose[]
    }
 
Example #15
Source File: AsciidocExtensionRegistry.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Asciidoctor asciidoctor) {
    JavaConverterRegistry javaConverterRegistry =
            asciidoctor.javaConverterRegistry();
    javaConverterRegistry.register(AsciidocConverter.class, backendName);

    JavaExtensionRegistry javaExtensionRegistry = asciidoctor
            .javaExtensionRegistry();
    javaExtensionRegistry.block(new CardBlockProcessor());
    javaExtensionRegistry.block(new PillarsBlockProcessor());
    javaExtensionRegistry.preprocessor(new IncludePreprocessor());
}
 
Example #16
Source File: GenerateDocumentation.java    From inception with Apache License 2.0 5 votes vote down vote up
private static void buildDoc(String type, Path outputDir)
{
    Attributes attributes = AttributesBuilder.attributes()
            .attribute("source-dir", getInceptionDir() + "/")
            .attribute("include-dir", outputDir.resolve("asciidoc").resolve(type)
                    .toString() + "/")
            .attribute("imagesdir", outputDir.resolve("asciidoc").resolve(type)
                    .resolve("images").toString() + "/")
            .attribute("doctype", "book")
            .attribute("toclevels", "8")
            .attribute("sectanchors", "true")
            .attribute("docinfo1", "true")
            .attribute("project-version", "DEVELOPER BUILD")
            .attribute("revnumber", "DEVELOPER BUILD")
            .attribute("product-name", "INCEpTION")
            .attribute("product-website-url", "https://inception-project.github.io")
            .attribute("icons", "font")
            .attribute("toc", "left")
            .get();
    OptionsBuilder options = OptionsBuilder.options()
            .toDir(outputDir.toFile())
            .safe(SafeMode.UNSAFE)
            .attributes(attributes);
    Asciidoctor asciidoctor = Asciidoctor.Factory.create();
    asciidoctor.requireLibrary("asciidoctor-diagram");
    File f = new File(outputDir.resolve("asciidoc").resolve(type).toString() + ".adoc");
    asciidoctor.convertFile(f , options);
}
 
Example #17
Source File: WhenTheInlineMacroProcessorRunsTwice.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void inlineMacroAttributes() {
    Asciidoctor asciidoctor = Asciidoctor.Factory.create();
    asciidoctor.javaExtensionRegistry().inlineMacro(new InlineMacroProcessor("example") {

        @Override
        public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
            return createPhraseNode(parent, "quoted", attributes.toString(), attributes, new HashMap<>());
        }

    });
    assertThat(convert(asciidoctor), containsString("{format=test}"));
    assertThat(convert(asciidoctor), containsString("{format=test}"));
}
 
Example #18
Source File: CukedoctorPublisher.java    From cucumber-living-documentation-plugin with MIT License 5 votes vote down vote up
/**
 * generates html and PDF documentation 'inlined' otherwise if we execute them in separated threads
 * only the last thread content is rendered (cause they work on the same adoc file)
 *
 * @return
 */
private Runnable runAll(final List<Feature> features, final DocumentAttributes attrs, final CukedoctorConfig cukedoctorConfig, final String outputPath) {
    return new Runnable() {

        @Override
        public void run() {

            Asciidoctor asciidoctor = null;
            try {
                asciidoctor = Asciidoctor.Factory.create(CukedoctorPublisher.class.getClassLoader());
                attrs.backend("html5");
                generateDocumentation(features, attrs, cukedoctorConfig, outputPath, asciidoctor);
                attrs.backend("pdf");
                generateDocumentation(features, attrs, cukedoctorConfig, outputPath, asciidoctor);

            } catch (Exception e) {
                e.printStackTrace();
                final String errorMessage = String.format("Unexpected error on documentation generation, message %s, cause %s", e.getMessage(), e.getCause());
                throw new RuntimeException(errorMessage);
            } finally {
                if (asciidoctor != null) {
                    asciidoctor.shutdown();
                }
            }
        }
    };

}
 
Example #19
Source File: AsciiDoctor.java    From sqlg with MIT License 5 votes vote down vote up
private void createDocs() {
    String version = "2.0.0-SNAPSHOT";
    Asciidoctor asciidoctor = create();
    try {
        File file = new File("sqlg-doc/docs/" + version + "/sqlg.adoc");
        File html = new File("sqlg-doc/docs/" + version + "/index.html");
        Attributes attributes = new Attributes();
        attributes.setBackend("html5");
        attributes.setStyleSheetName("asciidoctor-default.css");
        attributes.setDocType("book");
        attributes.setSourceHighlighter("highlightjs");

        Map<String, Object> options =  OptionsBuilder.options()
                .attributes(attributes)
                .toFile(new File(html.getPath()))
                .headerFooter(true)
                .safe(SafeMode.SERVER)
                .asMap();
        options.put("location", ":footer");
        Docinfo docinfo = new Docinfo(options);
        asciidoctor.javaExtensionRegistry().docinfoProcessor(docinfo);
        asciidoctor.convertFile(
                file,
                options
        );
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #20
Source File: JRubyRuntimeContext.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static Ruby get(Asciidoctor asciidoctor) {
    if (!(asciidoctor instanceof JRubyAsciidoctor)) {
        throw new IllegalArgumentException(
                "Expected a " + JRubyAsciidoctor.class.getName() + "instead of " + asciidoctor);
    }
    return ((JRubyAsciidoctor) asciidoctor).getRubyRuntime();
}
 
Example #21
Source File: AsciidoctorRefreshMojo.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void createAsciidoctor() {
    final ExecutorService es = Executors.newSingleThreadExecutor();
    asciidoctor = es.submit(new Callable<Asciidoctor>() {
        @Override
        public Asciidoctor call() throws Exception {
            return Asciidoctor.Factory.create();
        }
    });
    es.shutdown();
}
 
Example #22
Source File: AsciidoctorEngine.java    From jbake with MIT License 5 votes vote down vote up
private Asciidoctor getEngine(Options options) {
    try {
        lock.readLock().lock();
        if (engine == null) {
            lock.readLock().unlock();
            try {
                lock.writeLock().lock();
                if (engine == null) {
                    LOGGER.info("Initializing Asciidoctor engine...");
                    if (options.map().containsKey(OPT_GEM_PATH)) {
                        engine = AsciidoctorJRuby.Factory.create(String.valueOf(options.map().get(OPT_GEM_PATH)));
                    } else {
                        engine = Asciidoctor.Factory.create();
                    }

                    if (options.map().containsKey(OPT_REQUIRES)) {
                        String[] requires = String.valueOf(options.map().get(OPT_REQUIRES)).split(",");
                        if (requires.length != 0) {
                            for (String require : requires) {
                                engine.requireLibrary(require);
                            }
                        }
                    }

                    LOGGER.info("Asciidoctor engine initialized.");
                }
            } finally {
                lock.readLock().lock();
                lock.writeLock().unlock();
            }
        }
    } finally {
        lock.readLock().unlock();
    }
    return engine;
}
 
Example #23
Source File: AsciidoctorInterface.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
    public void destroyAsciidoctorInstance() {
//tag::shutdown[]
        Asciidoctor asciidoctor = Asciidoctor.Factory.create();
        asciidoctor.shutdown();
//end::shutdown[]
    }
 
Example #24
Source File: AsciidoctorTestObserver.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void beforeTestClassCreateSharedAsciidoctorInstance(@Observes(precedence = -100) BeforeClass beforeClass) {
    if (isSharedInstanceRequired(beforeClass.getTestClass().getJavaClass(), AsciidoctorJRuby.class)) {
        scopedAsciidoctor.get().setSharedAsciidoctor(
                AsciidoctorJRuby.Factory.create());
    } else if (isSharedInstanceRequired(beforeClass.getTestClass().getJavaClass(), Asciidoctor.class)) {
        scopedAsciidoctor.get().setSharedAsciidoctor(
                Asciidoctor.Factory.create());
    }
}
 
Example #25
Source File: AsciidoctorTestObserver.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void beforeTestCreateUnsharedAsciidoctorInstance(@Observes(precedence = 5) Before before) {

        if (isUnsharedInstanceRequired(before.getTestClass().getJavaClass(), AsciidoctorJRuby.class)
                || isUnsharedInstanceRequired(before.getTestMethod(), Asciidoctor.class)) {
            scopedAsciidoctor.get().setUnsharedAsciidoctor(
                    AsciidoctorJRuby.Factory.create());
        } else if (isUnsharedInstanceRequired(before.getTestClass().getJavaClass(), Asciidoctor.class)
                || isUnsharedInstanceRequired(before.getTestMethod(), Asciidoctor.class)) {
            scopedAsciidoctor.get().setUnsharedAsciidoctor(
                    Asciidoctor.Factory.create());
        }

    }
 
Example #26
Source File: AsciidoctorTestObserver.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void afterTestShutdownUnsharedAsciidoctorInstance(@Observes After after) {
    Asciidoctor asciidoctor = scopedAsciidoctor.get().getUnsharedAsciidoctor();
    if (asciidoctor != null) {
        asciidoctor.shutdown();
        scopedAsciidoctor.get().setUnsharedAsciidoctor(null);
    }
}
 
Example #27
Source File: AsciidoctorTestObserver.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void afterTestClassShutdownSharedAsciidoctorInstance(@Observes AfterClass afterClass) {
    Asciidoctor asciidoctor = scopedAsciidoctor.get().getSharedAsciidoctor();
    if (asciidoctor != null) {
        asciidoctor.shutdown();
        scopedAsciidoctor.get().setSharedAsciidoctor(null);
    }

}
 
Example #28
Source File: AsciidoctorInterface.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
    public void createAsciidoctorInstance() {
//tag::create[]
        Asciidoctor asciidoctor = Asciidoctor.Factory.create();
//end::create[]
        assertThat(asciidoctor, notNullValue());
    }
 
Example #29
Source File: SimpleAsciidoctorRendering.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Asciidoctor asciidoctor = Asciidoctor.Factory.create(); // <1>
    asciidoctor.convertFile(                                // <2>
            new File(args[0]),
            OptionsBuilder.options()                        // <3>
                    .toFile(true)
                    .safe(SafeMode.UNSAFE));
}
 
Example #30
Source File: TextConverterTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
    public void should_use_text_converter_for_conversion_registered_as_service_impl() throws Exception {


        ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader();

        try {
            URL serviceDir = classpathResources.getResource("converterregistry").toURI().toURL();
            URLClassLoader tccl = new URLClassLoader(new URL[] { serviceDir });
            Thread.currentThread().setContextClassLoader(tccl);

            Asciidoctor asciidoctor = Asciidoctor.Factory.create();
//tag::include[]
            File test_adoc = //...
//end::include[]
                    classpathResources.getResource("textconvertertest.adoc");

//tag::include[]

            String result = asciidoctor.convertFile(
                    test_adoc,
                    OptionsBuilder.options()
                            .backend("text")                                   // <1>
                            .toFile(false));

//end::include[]
            verifyResult(result);
        } finally {
            Thread.currentThread().setContextClassLoader(oldTCCL);
        }

    }