org.asciidoctor.jruby.AsciidoctorJRuby Java Examples

The following examples show how to use org.asciidoctor.jruby.AsciidoctorJRuby. 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: WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
public void should_have_gempath_in_ruby_env_when_created_with_gempath() {
    // Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
    final String gemPath = "/another/path";
    assertThat(System.getenv("GEM_PATH"), notNullValue());
    assertThat(System.getenv("GEM_HOME"), notNullValue());

    // When: A new Asciidoctor instance is created passing in a null GEM_PATH
    Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create(gemPath);

    // Then: The org.jruby.JRuby instance does not see this variable
    Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor);
    RubyString rubyGemPath = rubyRuntime.newString(gemPath);
    assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is((Object) rubyGemPath));
    assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is((Object) rubyGemPath));
}
 
Example #2
Source File: WhenRubyExtensionIsRegistered.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
public void ruby_treeprocessor_should_be_registered() {

    final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
    final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath));
    asciidoctor.rubyExtensionRegistry()
        .requireLibrary("shell-session-tree-processor.rb")
        .treeprocessor("ShellSessionTreeProcessor");

    String content = asciidoctor.convert(
        " $ echo \"Hello, World!\"\n" +
            " > Hello, World!\n" +
            "\n" +
            " $ gem install asciidoctor",
            options().toFile(false).get());

    final Document document = Jsoup.parse(content);
    final TextNode commandElement = document.getElementsByClass("command").get(0).textNodes().get(0);
    assertThat(commandElement.getWholeText(), is("echo \"Hello, World!\""));
    final TextNode commandElement2 = document.getElementsByClass("command").get(1).textNodes().get(0);
    assertThat(commandElement2.getWholeText(), is("gem install asciidoctor"));
}
 
Example #3
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 #4
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 #5
Source File: WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_have_gempath_in_ruby_env_when_created_with_default_create() {
    // Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
    assertThat(System.getenv("GEM_PATH"), notNullValue());
    assertThat(System.getenv("GEM_HOME"), notNullValue());

    // When: A new Asciidoctor instance is created passing in no GEM_PATH
    Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create();

    // Then: The org.jruby.JRuby instance sees this variable
    Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor);
    assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is(rubyRuntime.getNil()));
    assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is(rubyRuntime.getNil()));
}
 
Example #6
Source File: WhenAnAsciidoctorClassIsInstantiatedInAnEnvironmentWithGemPath.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_not_have_gempath_in_ruby_env_when_created_with_null_gempath() {
    // Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly)
    assertThat(System.getenv("GEM_PATH"), notNullValue());
    assertThat(System.getenv("GEM_HOME"), notNullValue());

    // When: A new Asciidoctor instance is created passing in a null GEM_PATH
    Asciidoctor asciidoctor = AsciidoctorJRuby.Factory.create((String) null);

    // Then: The org.jruby.JRuby instance does not see this variable
    Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor);
    assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is(rubyRuntime.getNil()));
    assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is(rubyRuntime.getNil()));
}
 
Example #7
Source File: WhenAnAsciidoctorJInstanceIsRequired.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUnwrapAsciidoctorInstanceAndRegisterRubyExtension() throws Exception {
  AsciidoctorJRuby asciidoctorj = Asciidoctor.Factory.create().unwrap(AsciidoctorJRuby.class);
  asciidoctorj.rubyExtensionRegistry().loadClass(getClass().getResourceAsStream("/ruby-extensions/YellRubyBlock.rb")).block("yell", "YellRubyBlock");

  String html = asciidoctorj.convert(DOC, OptionsBuilder.options().headerFooter(false));

  assertThat(html, containsString("HELLO WORLD"));
}
 
Example #8
Source File: WhenRubyExtensionIsRegistered.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 AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath));
    asciidoctor.rubyExtensionRegistry()
        .requireLibrary("view-result-docinfoprocessor.rb")
        .docinfoProcessor("ViewResultDocinfoProcessor");

    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: WhenRubyExtensionIsRegistered.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void ruby_preprocessor_should_be_registered() {

    final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
    final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath));
    asciidoctor.rubyExtensionRegistry()
        .requireLibrary("front-matter-preprocessor.rb")
        .preprocessor("FrontMatterPreprocessor");

    String content = asciidoctor.convert(
        "---\n" +
            "tags: [announcement, website]\n" +
            "---\n" +
            "= Document Title\n" +
            "\n" +
            "content\n" +
            "\n" +
            "[subs=\"attributes,specialcharacters\"]\n" +
            ".Captured front matter\n" +
            "....\n" +
            "---\n" +
            "{front-matter}\n" +
            "---\n" +
            "....",
            options().toFile(false).get());

    final Document document = Jsoup.parse(content);
    final Element contentElement = document.getElementsByClass("content").get(0);
    final Element literalElement = contentElement.getElementsByTag("pre").get(0);
    assertThat(literalElement.toString().replace("\r", ""),
        containsString("---\n" +
            "tags: [announcement, website]\n" +
            "---"));
}
 
Example #10
Source File: WhenRubyExtensionIsRegistered.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void ruby_postprocessor_should_be_registered() {

    final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
    final AsciidoctorJRuby asciidoctor = AsciidoctorJRuby.Factory.create(singletonList(rubyExtPath));
    asciidoctor.rubyExtensionRegistry()
        .requireLibrary("xml-entity-postprocessor.rb")
        .postprocessor("XmlEntityPostprocessor");

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

    assertThat(content, containsString("Read &#167;2 and it&#39;ll all be clear."));
}
 
Example #11
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 #12
Source File: SyntaxHighlighterRegistryExecutor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public SyntaxHighlighterRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
Example #13
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private static void registerLogHandlers(AsciidoctorJRuby asciidoctor) {
    new LogHandlerRegistryExecutor(asciidoctor).registerAllLogHandlers();
}
 
Example #14
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private static void registerSyntaxHighlighters(AsciidoctorJRuby asciidoctor) {
    new SyntaxHighlighterRegistryExecutor(asciidoctor).registerAllSyntaxHighlighter();
}
 
Example #15
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private static void registerExtensions(AsciidoctorJRuby asciidoctor) {
    new ExtensionRegistryExecutor(asciidoctor).registerAllExtensions();
}
 
Example #16
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
private static void registerConverters(AsciidoctorJRuby asciidoctor) {
    new ConverterRegistryExecutor(asciidoctor).registerAllConverters();
}
 
Example #17
Source File: LogHandlerRegistryExecutor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public LogHandlerRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
Example #18
Source File: ExtensionRegistryExecutor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public ExtensionRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
Example #19
Source File: ConverterRegistryExecutor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public ConverterRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
Example #20
Source File: AsciidoctorResourceProvider.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canProvide(Class<?> type) {
    return Asciidoctor.class == type || AsciidoctorJRuby.class == type;
}