org.asciidoctor.Attributes Java Examples

The following examples show how to use org.asciidoctor.Attributes. 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: OptionsTest.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@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[]
    }
 
Example #2
Source File: AsciidoctorHelper.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #3
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 #4
Source File: InspectorImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private String swaggerToHtml(String swaggerContent) {
  if (asciidoctor == null) {
    synchronized (this) {
      if (asciidoctor == null) {
        // very slow, need a few seconds
        LOGGER.info("create AsciiDoctor start.");
        asciidoctor = Factory.create();
        asciidoctor.javaExtensionRegistry().docinfoProcessor(AppendStyleProcessor.class);
        LOGGER.info("create AsciiDoctor end.");
      }
    }
  }

  // swagger to markup
  Builder markupBuilder = Swagger2MarkupConverter.from(SwaggerUtils.parseSwagger(swaggerContent));
  // default not support cookie parameter
  // so must customize config
  Swagger2MarkupConfig markupConfig = new Swagger2MarkupConfigBuilder()
      .withParameterOrdering(Ordering
          .explicit("path", "query", "header", "cookie", "formData", "body")
          .onResultOf(Parameter::getIn))
      .build();
  String markup = markupBuilder.withConfig(markupConfig).build().toString();

  // markup to html
  OptionsBuilder builder = OptionsBuilder.options();
  builder.docType("book")
      .backend("html5")
      .headerFooter(true)
      .safe(SafeMode.UNSAFE)
      .attributes(AttributesBuilder.attributes()
          .attribute("toclevels", 3)
          .attribute(Attributes.TOC_2, true)
          .attribute(Attributes.TOC_POSITION, "left")
          .attribute(Attributes.LINK_CSS, true)
          .attribute(Attributes.STYLESHEET_NAME, inspectorConfig.getAsciidoctorCss())
          .attribute(Attributes.SECTION_NUMBERS, true)
          .attribute(Attributes.SECT_NUM_LEVELS, 4));
  return asciidoctor.convert(markup, builder.asMap());
}
 
Example #5
Source File: GenerateDocumentation.java    From webanno 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", getWebannoDir() + "/")
            .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", "WebAnno")
            .attribute("product-website-url", "https://webanno.github.io/webanno/")
            .attribute("icons", "font")
            .attribute("toc", "preamble")
            .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 #6
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 #7
Source File: RubyGemsPreloader.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void preloadRequiredLibraries(Map<String, Object> options) {

        if (options.containsKey(Options.ATTRIBUTES)) {
            Map<String, Object> attributes = (Map<String, Object>) options.get(Options.ATTRIBUTES);

            if (isOptionSet(attributes, Attributes.SOURCE_HIGHLIGHTER)
                    && isOptionWithValue(attributes, Attributes.SOURCE_HIGHLIGHTER, CODERAY)) {
                preloadLibrary(Attributes.SOURCE_HIGHLIGHTER);
            }

            if (isOptionSet(attributes, Attributes.CACHE_URI)) {
                preloadLibrary(Attributes.CACHE_URI);
            }

            if (isOptionSet(attributes, Attributes.DATA_URI)) {
                preloadLibrary(Attributes.DATA_URI);
            }
        }

        if (isOptionSet(options, Options.ERUBY) && isOptionWithValue(options, Options.ERUBY, ERUBIS)) {
            preloadLibrary(Options.ERUBY);
        }

        if (isOptionSet(options, Options.TEMPLATE_DIRS)) {
            preloadLibrary(Options.TEMPLATE_DIRS);
        }
        
        if(isOptionSet(options, Options.BACKEND) && "epub3".equalsIgnoreCase((String) options.get(Options.BACKEND))) {
            preloadLibrary(EPUB3);
        }

        if(isOptionSet(options, Options.BACKEND) && "pdf".equalsIgnoreCase((String) options.get(Options.BACKEND))) {
            preloadLibrary(PDF);
        }

        if(isOptionSet(options, Options.BACKEND) && "revealjs".equalsIgnoreCase((String) options.get(Options.BACKEND))) {
            preloadLibrary(REVEALJS);
        }
    }
 
Example #8
Source File: DefaultCssResolver.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private boolean isStylesheetWithValidValue(Map<String, Object> attributes) {
    return isAttributeNotConfirmed(attributes, Attributes.STYLESHEET_NAME)
            || "".equals(attributes.get(Attributes.STYLESHEET_NAME))
            || "DEFAULT".equals(attributes.get(Attributes.STYLESHEET_NAME));
}
 
Example #9
Source File: DefaultCssResolver.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private boolean isLinkCssWithValidValue(Map<String, Object> attributes) {
    return isAttributeNotConfirmed(attributes, Attributes.LINK_CSS)
            || "".equals(attributes.get(Attributes.LINK_CSS));
}
 
Example #10
Source File: DefaultCssResolver.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private boolean isCopyCssPresent(Map<String, Object> attributes) {
    return "".equals(attributes.get(Attributes.COPY_CSS));
}
 
Example #11
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public <T> T convert(String content, Map<String, Object> options, Class<T> expectedResult) {

        this.rubyGemsPreloader.preloadRequiredLibraries(options);

        logger.fine(String.join(" ", AsciidoctorUtils.toAsciidoctorCommand(options, "-")));

        if (AsciidoctorUtils.isOptionWithAttribute(options, Attributes.SOURCE_HIGHLIGHTER, "pygments")) {
            logger.fine("In order to use Pygments with Asciidoctor, you need to install Pygments (and Python, if you don't have it yet). Read http://asciidoctor.org/news/#syntax-highlighting-with-pygments.");
        }

        String currentDirectory = rubyRuntime.getCurrentDirectory();

        if (options.containsKey(Options.BASEDIR)) {
            rubyRuntime.setCurrentDirectory((String) options.get(Options.BASEDIR));
        }

        final Object toFileOption = options.get(Options.TO_FILE);
        if (toFileOption instanceof OutputStream) {
            options.put(Options.TO_FILE, RubyOutputStreamWrapper.wrap(getRubyRuntime(), (OutputStream) toFileOption));
        }

        RubyHash rubyHash = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);

        try {

            IRubyObject object = getAsciidoctorModule().callMethod("convert",
                    rubyRuntime.newString(content), rubyHash);
            if (NodeConverter.NodeType.DOCUMENT_CLASS.isInstance(object)) {
                // If a document is rendered to a file Asciidoctor returns the document, we return null
                return null;
            }
            return RubyUtils.rubyToJava(rubyRuntime, object, expectedResult);
        } catch (RaiseException e) {
            logger.severe(e.getException().getClass().getCanonicalName());
            throw new AsciidoctorCoreException(e);
        } finally {
            // we restore current directory to its original value.
            rubyRuntime.setCurrentDirectory(currentDirectory);
        }

    }