org.asciidoctor.AttributesBuilder Java Examples

The following examples show how to use org.asciidoctor.AttributesBuilder. 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: AsciidocEngine.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
/**
 * Read a document's header.
 * @param source the document to read the header from
 * @return the header as {@code Map<String, Object>}, never {@code null}
 */
public Map<String, Object> readDocumentHeader(File source){
    checkValidFile(source, "source");
    final OptionsBuilder optionsBuilder = OptionsBuilder.options()
            .attributes(
                    AttributesBuilder
                            .attributes()
                            .attributes(attributes))
            .safe(SafeMode.UNSAFE)
            .headerFooter(false)
            .eruby("")
            .baseDir(source.getParentFile())
            .option("parse_header_only", true);
    if (backend != null) {
        optionsBuilder.backend(this.backend);
    }
    Document doc = asciidoctor.loadFile(source, optionsBuilder.asMap());
    Map<String, Object> headerMap = new HashMap<>();
    String h1 = parseSection0Title(source);
    if (h1 != null) {
        headerMap.put("h1", h1);
    }
    headerMap.putAll(doc.getAttributes());
    return headerMap;
}
 
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: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_return_default_configuration_when_site_xml_is_null() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(null, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
    assertThat(configuration.getRequires()).isEmpty();
}
 
Example #4
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_return_default_configuration_when_asciidoc_xml_is_null() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.siteNode()
            .build();
    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
    assertThat(configuration.getRequires()).isEmpty();
}
 
Example #5
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 #6
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_return_simple_single_requires() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("requires")
            .addChild("require", "gem")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
    assertThat(configuration.getRequires())
            .containsExactly("gem");
}
 
Example #7
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_return_multiple_requires() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("requires")
            .addChild("require", "gem_1", "gem_2", "gem_3")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
    assertThat(configuration.getRequires())
            .containsExactlyInAnyOrder("gem_1", "gem_2", "gem_3");
}
 
Example #8
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_return_multiple_requires_when_defined_in_single_element() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("requires")
            .addChild("require", "gem_1,gem_2, gem_3")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
    assertThat(configuration.getRequires())
            .containsExactlyInAnyOrder("gem_1", "gem_2", "gem_3");
}
 
Example #9
Source File: HighlightJsHighlighterTest.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
    public void should_invoke_formatting_syntax_highlighter() throws Exception {
        File sources_adoc =
            classpathResources.getResource("sources.adoc");


//tag::includeformatter[]

        asciidoctor.syntaxHighlighterRegistry()
            .register(HighlightJsWithLanguageHighlighter.class, "myhighlightjs");

        String result = asciidoctor.convertFile(sources_adoc,
            OptionsBuilder.options()
                .headerFooter(true)
                .toFile(false)
                .attributes(AttributesBuilder.attributes().sourceHighlighter("myhighlightjs")));

        assertThat(result,
            containsString("<script>hljs.initHighlighting()</script>"));
        assertThat(result,
            containsString("<code class='java'>public static class Test"));
//end::includeformatter[]
    }
 
Example #10
Source File: HighlightJsHighlighterTest.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
public void should_invoke_syntax_highlighter_with_3_params() throws Exception {
    File sources_adoc =
        classpathResources.getResource("sources.adoc");


    asciidoctor.syntaxHighlighterRegistry()
        .register(org.asciidoctor.integrationguide.syntaxhighlighter.threeparams.HighlightJsHighlighter.class, "myhighlightjs");

    String result = asciidoctor.convertFile(sources_adoc,
        OptionsBuilder.options()
            .headerFooter(true)
            .toFile(false)
            .attributes(AttributesBuilder.attributes().sourceHighlighter("myhighlightjs")));

    assertThat(result,
        containsString("<script>hljs.initHighlighting()</script>"));
}
 
Example #11
Source File: HighlightJsHighlighterTest.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
    public void should_invoke_syntax_highlighter() throws Exception {
//tag::include[]
        File sources_adoc = //...
//end::include[]
            classpathResources.getResource("sources.adoc");

        //tag::include[]

        asciidoctor.syntaxHighlighterRegistry()
            .register(HighlightJsHighlighter.class, "myhighlightjs"); // <1>

        String result = asciidoctor.convertFile(sources_adoc,
            OptionsBuilder.options()
                .headerFooter(true) // <2>
                .toFile(false)
                .attributes(AttributesBuilder.attributes().sourceHighlighter("myhighlightjs"))); // <3>

        assertThat(result,
            containsString("<script>hljs.initHighlighting()</script>"));
//end::include[]
    }
 
Example #12
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_remove_empty_and_blank_requires() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("requires")
            .addChild("require", "gem_1,,gem_2", "", ",,", "gem_3")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
    assertThat(configuration.getRequires())
            .containsExactlyInAnyOrder("gem_1", "gem_2", "gem_3");
}
 
Example #13
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_not_return_empty_template_dirs() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("template_dirs")
            .addChild("dir", "")
            .parent()
            .addChild("dir", null)
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES);
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
}
 
Example #14
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void should_return_baseDir_dirs_when_defined_as_template_dirs_dir() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("baseDir", "path")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();

    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES, BASEDIR);
    assertThat(optionsMap.get(BASEDIR))
            .isEqualTo(new File("path").getAbsolutePath());
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
}
 
Example #15
Source File: AbstractAsciiDocMojo.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> asciiDoctorOptions(
        Map<String, Object> attributes,
        Path inputRelativePath,
        File outputDirectory,
        Path baseDirPath,
        boolean runPreprocessing) {
    final OptionsBuilder optionsBuilder = OptionsBuilder.options()
            .attributes(
                    AttributesBuilder
                            .attributes()
                            .attributes(attributes))
            .safe(SafeMode.UNSAFE)
            .headerFooter(false)
            .baseDir(baseDirPath.toFile())
            .eruby("");
    if (outputDirectory != null) {
        optionsBuilder.option("preincludeOutputPath",
                    outputDirectory.toPath().resolve(inputRelativePath));
    }
    if (runPreprocessing) {
        optionsBuilder.option("preprocessOutputType", outputType());
    }

    return optionsBuilder.asMap();
}
 
Example #16
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_return_template_dirs_when_defined_as_templateDirs_dir() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("templateDirs")
            .addChild("dir", "path")
            .parent()
            .addChild("dir", "path2")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES, TEMPLATE_DIRS);
    assertThat(optionsMap.get(TEMPLATE_DIRS))
            .isEqualTo(Arrays.asList(
                    new File("path").getAbsolutePath(),
                    new File("path2").getAbsolutePath()
            ));
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
}
 
Example #17
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_return_template_dirs_when_defined_as_template_dirs_dir() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("template_dirs")
            .addChild("dir", "path")
            .parent()
            .addChild("dir", "path2")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap).containsOnlyKeys(ATTRIBUTES, TEMPLATE_DIRS);
    assertThat(optionsMap.get(TEMPLATE_DIRS))
            .isEqualTo(Arrays.asList(
                    new File("path").getAbsolutePath(),
                    new File("path2").getAbsolutePath()
            ));
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
}
 
Example #18
Source File: SiteConversionConfigurationParserTest.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void should_return_any_configuration_inside_asciidoc_node_as_option() {
    // given
    final MavenProject project = fakeProject();
    OptionsBuilder emptyOptions = OptionsBuilder.options();
    AttributesBuilder emptyAttributes = AttributesBuilder.attributes();
    Xpp3Dom siteConfig = Xpp3DoomBuilder.asciidocNode()
            .addChild("option-1", "value-1")
            .parent().addChild("option_2", "value-2")
            .parent().addChild("_option-3", "value-3")
            .parent().addChild("option-4_", "value-4")
            .parent().addChild("option.5", "value-5")
            .build();

    // when
    SiteConversionConfiguration configuration = new SiteConversionConfigurationParser(project)
            .processAsciiDocConfig(siteConfig, emptyOptions, emptyAttributes);

    // then
    final Map<String, Object> optionsMap = configuration.getOptions().map();
    assertThat(optionsMap)
            .containsOnlyKeys(ATTRIBUTES, "option-1", "option_2", "_option-3", "option-4_", "option.5");
    assertThat(optionsMap.get("option-1")).isEqualTo("value-1");
    assertThat(optionsMap.get("option_2")).isEqualTo("value-2");
    assertThat(optionsMap.get("_option-3")).isEqualTo("value-3");
    assertThat(optionsMap.get("option-4_")).isEqualTo("value-4");
    assertThat(optionsMap.get("option.5")).isEqualTo("value-5");
    assertThat((Map) optionsMap.get(ATTRIBUTES)).isEmpty();
}
 
Example #19
Source File: AsciidoctorEngine.java    From jbake with MIT License 5 votes vote down vote up
private Options getAsciiDocOptionsAndAttributes(ParserContext context) {
    JBakeConfiguration config = context.getConfig();
    List<String> asciidoctorAttributes = config.getAsciidoctorAttributes();
    final AttributesBuilder attributes = attributes(asciidoctorAttributes.toArray(new String[0]));
    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;
}
 
Example #20
Source File: WhenAsciidoctorAPICallIsCalled.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@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());

    List<String> command = AsciidoctorUtils.toAsciidoctorCommand(
            optionsBuilder.asMap(), "file.adoc");

    String currentDirectory = new File( "" ).getAbsolutePath() + File.separator;

    String[] parameters = command.subList(1, command.size()).toArray(new String[0]);
    
    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!"));
    
}
 
Example #21
Source File: WhenDocumentIsRenderedWithPreloading.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void data_uri_gem_should_be_preloaded() {

    Map<String, Object> options = OptionsBuilder.options()
            .attributes(AttributesBuilder.attributes().dataUri(true).get()).asMap();

    ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options);
    RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'base64'");
    assertThat(evalScriptlet.isFalse(), is(true));

}
 
Example #22
Source File: WhenDocumentIsRenderedWithPreloading.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void not_coderay_gem_should_not_be_preloaded() {

    Map<String, Object> options = OptionsBuilder.options()
            .attributes(AttributesBuilder.attributes().sourceHighlighter("pygments").get()).asMap();

    ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options);
    RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'coderay'");
    assertThat(evalScriptlet.isTrue(), is(true));

}
 
Example #23
Source File: WhenDocumentIsRenderedWithPreloading.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@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));

}
 
Example #24
Source File: OrderTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_invoke_syntax_highlighter() throws Exception {
    File sources_adoc = //...
        classpathResources.getResource("syntax-highlighting-order.adoc");

    File toDir = // ...
        tempDir.newFolder();

    asciidoctor.syntaxHighlighterRegistry()
        .register(OrderDocumentingHighlighter.class, "order");

    asciidoctor.convertFile(sources_adoc,
        OptionsBuilder.options()
            .headerFooter(true)
            .toDir(toDir)
            .safe(SafeMode.UNSAFE)
            .attributes(AttributesBuilder.attributes()
                .sourceHighlighter("order")
                .copyCss(true)
                .linkCss(true)));

    try (PrintWriter out = new PrintWriter(new FileWriter("build/resources/test/order.txt"))) {
        for (String msg: OrderDocumentingHighlighter.messages) {
            out.println(". " + msg);
        }
    }
}
 
Example #25
Source File: PrismJsHighlighterTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
    public void should_invoke_syntax_highlighter() throws Exception {
//tag::include[]
        File sources_adoc = //...
//end::include[]
            classpathResources.getResource("sources.adoc");

//tag::include[]
        File toDir = // ...
//end::include[]
            tempDir.newFolder();
//tag::include[]

        //tag::include[]
        asciidoctor.syntaxHighlighterRegistry()
            .register(PrismJsHighlighter.class, "prismjs"); // <1>

        asciidoctor.convertFile(sources_adoc,
            OptionsBuilder.options()
                .headerFooter(true)
                .toDir(toDir)
                .safe(SafeMode.UNSAFE)
                .attributes(AttributesBuilder.attributes()
                    .sourceHighlighter("prismjs")           // <1>
                    .copyCss(true)
                    .linkCss(true)));

        File docFile = new File(toDir, "sources.html");

        Document document = Jsoup.parse(new File(toDir, "sources.html"), "UTF-8");
        Elements keywords = document.select("div.content pre.highlight code span.token.keyword"); // <2>
        assertThat(keywords, not(empty()));
        assertThat(keywords.first().text(), is("public"));
//end::include[]
    }
 
Example #26
Source File: HighlightJsHighlighterTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
    public void should_invoke_stylesheet_writing_syntax_highlighter() throws Exception {
        File sources_adoc =
            classpathResources.getResource("sources.adoc");

//tag::includestylesheetwriter[]
        File toDir = // ...
//end::includestylesheetwriter[]
            tempDir.newFolder();
//tag::includestylesheetwriter[]

        asciidoctor.syntaxHighlighterRegistry()
            .register(HighlightJsWithOfflineStylesHighlighter.class, "myhighlightjs");

        asciidoctor.convertFile(sources_adoc,
            OptionsBuilder.options()
                .headerFooter(true)
                .toDir(toDir)              // <1>
                .safe(SafeMode.UNSAFE)
                .attributes(AttributesBuilder.attributes()
                    .sourceHighlighter("myhighlightjs")
                    .copyCss(true)         // <1>
                    .linkCss(true)));

        File docFile = new File(toDir, "sources.html");
        assertTrue(docFile.exists());

        File cssFile = new File(toDir, "github.min.css");
        assertTrue(cssFile.exists());

        File jsFile = new File(toDir, "highlight.min.js");
        assertTrue(jsFile.exists());

        try (FileReader docReader = new FileReader(new File(toDir, "sources.html"))) {
            String html = IOUtils.toString(docReader);
            assertThat(html, containsString("<link rel=\"stylesheet\" href=\"github.min.css\">"));
            assertThat(html, containsString("<script src=\"highlight.min.js\"></script>"));
        }
//end::includestylesheetwriter[]
    }
 
Example #27
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 #28
Source File: AsciidoctorExecutor.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private void configureDefaultsAttributes(final File workDir, final Log log, final String backend,
        final AttributesBuilder attrs, final Map<String, String> attributes) {
    if (extractedResources == null) {
        extractResources(workDir, log);
    }
    switch (backend.toLowerCase(ENGLISH)) {
    case "html":
    case "html5":
        if (!attributes.containsKey("stylesheet")) {
            attrs.attribute("stylesheet", "talend.css");
        }
        if (!attributes.containsKey("stylesdir")) {
            attrs.attribute("stylesdir", new File(extractedResources, "resources/html").getAbsolutePath());
        }
        if (!attributes.containsKey("data-uri")) {
            attrs.attribute("data-uri");
        }
        break;
    case "pdf":
        if (!attributes.containsKey("pdf-style")) {
            attrs.attribute("pdf-style", "talend.yml");
        }
        if (!attributes.containsKey("pdf-stylesdir")) {
            attrs.attribute("pdf-stylesdir", new File(extractedResources, "resources/pdf").getAbsolutePath());
        }
        break;
    default:
    }

    if (!attributes.containsKey("icons")) {
        attrs.attribute("icons", "font");
    }
    if (!attributes.containsKey("source-highlighter")) {
        attrs.attribute("source-highlighter", "coderay");
    }
    if (!attributes.containsKey("toc")) {
        attrs.attribute("toc", "left");
    }
}
 
Example #29
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 #30
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);
}