Skip to content

Commit

Permalink
resolves asciidoctor#183 upgrade to AsciidoctorJ 1.5.3.2 and JRuby 1.…
Browse files Browse the repository at this point in the history
…7.23

- upgrade to AsciidoctorJ 1.5.3.2
- upgrade to JRuby 1.7.23
- fix tests
- use Asciidoctor#requireLibrary to require libraries
- add test for requires configuration option
  • Loading branch information
mojavelinux authored and abelsromero committed Jan 8, 2016
1 parent f04378b commit 3f74264
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 94 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
<project.execution.environment>JavaSE-1.6</project.execution.environment>
<spock.version>0.7-groovy-2.0</spock.version>
<groovy.version>2.1.3</groovy.version>
<asciidoctorj.version>1.5.2</asciidoctorj.version>
<asciidoctorj.version>1.5.3.2</asciidoctorj.version>
<maven.coveralls.plugin.version>3.0.1</maven.coveralls.plugin.version>
<maven.jacoco.plugin.version>0.7.2.201409121644</maven.jacoco.plugin.version>
<jruby.version>1.7.17</jruby.version>
<jruby.version>1.7.23</jruby.version>
<maven.plugin.annotations.version>3.2</maven.plugin.annotations.version>
<maven.plugin.api.version>2.0</maven.plugin.api.version>
<maven.project.version>2.2.1</maven.project.version>
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {

ensureOutputExists();

final Asciidoctor asciidoctorInstance = getAsciidoctorInstance(gemPath);
final Asciidoctor asciidoctor = getAsciidoctorInstance(gemPath);

if (requires.size() > 0) {
for (String require : requires) {
// FIXME AsciidoctorJ should provide a public API for requiring paths in the Ruby runtime
RubyUtils.requireLibrary(JRubyRuntimeContext.get(), require);
}
}
asciidoctor.requireLibraries(requires);

final OptionsBuilder optionsBuilder = OptionsBuilder.options()
.backend(backend)
Expand All @@ -170,7 +165,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

optionsBuilder.attributes(attributesBuilder);

ExtensionRegistry extensionRegistry = new AsciidoctorJExtensionRegistry(asciidoctorInstance);
ExtensionRegistry extensionRegistry = new AsciidoctorJExtensionRegistry(asciidoctor);
for (ExtensionConfiguration extension: extensions) {
try {
extensionRegistry.register(extension.getClassName(), extension.getBlockName());
Expand All @@ -182,12 +177,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (sourceDocumentName == null) {
for (final File f : scanSourceFiles()) {
setDestinationPaths(optionsBuilder, f);
renderFile(asciidoctorInstance, optionsBuilder.asMap(), f);
renderFile(asciidoctor, optionsBuilder.asMap(), f);
}
} else {
File sourceFile = new File(sourceDirectory, sourceDocumentName);
setDestinationPaths(optionsBuilder, sourceFile);
renderFile(asciidoctorInstance, optionsBuilder.asMap(), sourceFile);
renderFile(asciidoctor, optionsBuilder.asMap(), sourceFile);
}

// #67 -- get all files that aren't adoc/ad/asciidoc and create synchronizations for them
Expand Down Expand Up @@ -290,8 +285,8 @@ private void synchronize() {
}
}

protected void renderFile(Asciidoctor asciidoctorInstance, Map<String, Object> options, File f) {
asciidoctorInstance.renderFile(f, options);
protected void renderFile(Asciidoctor asciidoctor, Map<String, Object> options, File f) {
asciidoctor.renderFile(f, options);
logRenderedFile(f);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ else if ("requires".equals(optName)) {
}
else {
// supports variant:
// <requires>time,base64</requires>
// <requires>time, base64</requires>
for (String require : asciidocOpt.getValue().split(",")) {
requireLibrary(require);
}
Expand Down Expand Up @@ -182,8 +182,7 @@ protected File resolveTemplateDir(MavenProject project, String path) {
private void requireLibrary(String require) {
if (!(require = require.trim()).isEmpty()) {
try {
// TODO switch to Asciidoctor.requireLibrary after upgrading to AsciidoctorJ 1.5.3.2
RubyUtils.requireLibrary(JRubyRuntimeContext.get(), require);
asciidoctor.requireLibrary(require);
} catch (Exception ex) {
getLog().error(ex.getLocalizedMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor
import org.asciidoctor.maven.test.processors.MetaDocinfoProcessor
import org.asciidoctor.maven.test.processors.UriIncludeProcessor
import org.asciidoctor.maven.test.processors.YellBlockProcessor
import org.jruby.exceptions.RaiseException

import spock.lang.Specification
import spock.lang.Unroll
Expand Down Expand Up @@ -72,7 +71,7 @@ class AsciidoctorMojoExtensionsTest extends Specification {
mojo.execute()
then:
outputDir.list().size() == 0
thrown(RaiseException)
thrown(RuntimeException)
// e.message.contains(mojo.processors.get(0).className)
// e.message.contains('not found in classpath')
}
Expand Down Expand Up @@ -337,8 +336,7 @@ class AsciidoctorMojoExtensionsTest extends Specification {
text.contains("source 'https://rubygems.org'")
}

// This test is added to keep track of possible changes in the extension's SPI
def "fails renders html with Preprocessor, DocinfoProcessor"() {
def "renders html when using all types of extensions"() {
setup:
File srcDir = new File(SRC_DIR)
File outputDir = new File("${OUTPUT_DIR}/processors/${System.currentTimeMillis()}")
Expand All @@ -349,7 +347,9 @@ class AsciidoctorMojoExtensionsTest extends Specification {
mojo.sourceDocumentName = 'processors-sample.adoc'
mojo.outputDirectory = outputDir
mojo.headerFooter = true
mojo.attributes['toc'] = null
mojo.attributes['toc'] = ''
mojo.attributes['linkcss'] = ''
mojo.attributes['copycss!'] = ''
mojo.extensions = [
// Preprocessor
[className: 'org.asciidoctor.maven.test.processors.ChangeAttributeValuePreprocessor'] as ExtensionConfiguration,
Expand All @@ -366,9 +366,18 @@ class AsciidoctorMojoExtensionsTest extends Specification {
]
mojo.execute()
then:
def e = thrown(ClassCastException)
e.message =~ /org\.jruby\.gen.(.)* cannot be cast to org.jruby.RubyObject/
}
outputDir.list().toList().isEmpty() == false
outputDir.list().toList().contains('processors-sample.html')

File sampleOutput = new File(outputDir, 'processors-sample.html')
sampleOutput.length() > 0

String text = sampleOutput.text
text.contains('<meta name="author" content="asciidoctor">')
text.contains('<script src="https://gist.github.com/123456.js"></script>')
text.contains('<p>See <a href="gittutorial.html">gittutorial</a> to get started.</p>')
text.contains('<p>THE TIME IS NOW. GET A MOVE ON.</p>')
}

/**
* Manual test to validate automatic extension registration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ class AsciidoctorMojoTest extends Specification {
mojo.headerFooter == true
}

def "should require library"() {
setup:
File srcDir = new File('target/test-classes/src/asciidoctor')
File outputDir = new File('target/asciidoctor-output')

if (!outputDir.exists())
outputDir.mkdir()
when:
AsciidoctorMojo mojo = new AsciidoctorMojo()
mojo.requires = ['time'] as List
mojo.backend = 'html'
mojo.outputDirectory = outputDir
mojo.sourceDirectory = srcDir
mojo.sourceDocumentName = 'sample.asciidoc'
mojo.execute()
then:
outputDir.list().toList().isEmpty() == false
outputDir.list().toList().contains('sample.html')
assert "constant".equals(org.asciidoctor.internal.JRubyRuntimeContext.get().evalScriptlet('defined? ::DateTime').toString())
}

def "embedding resources"() {
setup:
File srcDir = new File('target/test-classes/src/asciidoctor')
Expand Down Expand Up @@ -489,9 +510,9 @@ class AsciidoctorMojoTest extends Specification {
}

/**
* Tests (currenty not working) Pygments source code highlighting options.
* Tests behavior when source code highlighting with Pygments is specified.
*
* Test checks that an exception is thrown.
* Test checks that an exception is not thrown.
*/
def 'code highlighting - pygments'() {
setup:
Expand All @@ -508,7 +529,10 @@ class AsciidoctorMojoTest extends Specification {
mojo.execute()

then:
thrown(org.jruby.exceptions.RaiseException)
File mainDocumentOutput = new File(outputDir, 'main-document.html')
String text = mainDocumentOutput.getText()
text.contains('Pygments is not available.')
text.contains('<pre class="pygments highlight">')
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
package org.asciidoctor.maven.test.processors;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.asciidoctor.ast.DocumentRuby;
import org.asciidoctor.extension.IncludeProcessor;
import org.asciidoctor.extension.PreprocessorReader;

public class UriIncludeProcessor extends IncludeProcessor {

public UriIncludeProcessor(Map<String, Object> config) {
super(config);
System.out.println(this.getClass().getSimpleName() + "("
+ this.getClass().getSuperclass().getSimpleName() + ") initialized");
}

@Override
public boolean handles(String target) {
return target.startsWith("http://") || target.startsWith("https://");
}

@Override
public void process(DocumentRuby document, PreprocessorReader reader, String target,
Map<String, Object> attributes) {

System.out.println("Processing "+ this.getClass().getSimpleName());

StringBuilder content = readContent(target);
reader.push_include(content.toString(), target, target, 1, attributes);

}

private StringBuilder readContent(String target) {

StringBuilder content = new StringBuilder();

try {

URL url = new URL(target);
InputStream openStream = url.openStream();

BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(openStream));

String line = null;
while ((line = bufferedReader.readLine()) != null) {
content.append(line);
}

bufferedReader.close();

} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return content;
}

}
package org.asciidoctor.maven.test.processors;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.asciidoctor.ast.DocumentRuby;
import org.asciidoctor.extension.IncludeProcessor;
import org.asciidoctor.extension.PreprocessorReader;

public class UriIncludeProcessor extends IncludeProcessor {

public UriIncludeProcessor(Map<String, Object> config) {
super(config);
System.out.println(this.getClass().getSimpleName() + "("
+ this.getClass().getSuperclass().getSimpleName() + ") initialized");
}

@Override
public boolean handles(String target) {
return target.startsWith("http://") || target.startsWith("https://");
}

@Override
public void process(DocumentRuby document, PreprocessorReader reader, String target,
Map<String, Object> attributes) {

System.out.println("Processing "+ this.getClass().getSimpleName());

StringBuilder content = readContent(target);
reader.push_include(content.toString(), target, target, 1, attributes);

}

private StringBuilder readContent(String target) {

StringBuilder content = new StringBuilder();

try {

URL url = new URL(target);
InputStream openStream = url.openStream();

BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(openStream));

String line = null;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}

bufferedReader.close();

} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return content;
}

}

0 comments on commit 3f74264

Please sign in to comment.