Java源码示例:org.asciidoctor.jruby.AsciidoctorJRuby

示例1
@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"));
}
 
示例2
@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));
}
 
示例3
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());
    }
}
 
示例4
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());
        }

    }
 
示例5
@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 §2 and it'll all be clear.",
            options().toFile(false).get());

    assertThat(content, containsString("Read §2 and it'll all be clear."));
}
 
示例6
@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" +
            "---"));
}
 
示例7
@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);
}
 
示例8
@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"));
}
 
示例9
@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()));
}
 
示例10
@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()));
}
 
示例11
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;
}
 
示例12
@Override
public boolean canProvide(Class<?> type) {
    return Asciidoctor.class == type || AsciidoctorJRuby.class == type;
}
 
示例13
public ConverterRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
示例14
public ExtensionRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
示例15
public LogHandlerRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}
 
示例16
private static void registerConverters(AsciidoctorJRuby asciidoctor) {
    new ConverterRegistryExecutor(asciidoctor).registerAllConverters();
}
 
示例17
private static void registerExtensions(AsciidoctorJRuby asciidoctor) {
    new ExtensionRegistryExecutor(asciidoctor).registerAllExtensions();
}
 
示例18
private static void registerSyntaxHighlighters(AsciidoctorJRuby asciidoctor) {
    new SyntaxHighlighterRegistryExecutor(asciidoctor).registerAllSyntaxHighlighter();
}
 
示例19
private static void registerLogHandlers(AsciidoctorJRuby asciidoctor) {
    new LogHandlerRegistryExecutor(asciidoctor).registerAllLogHandlers();
}
 
示例20
public SyntaxHighlighterRegistryExecutor(AsciidoctorJRuby asciidoctor) {
    this.asciidoctor = asciidoctor;
}