Skip to content

Commit ff18f80

Browse files
committed
Add AsciidoctorJ 1.6 and 2.0 compatibility
AsciidoctorJ 1.6 is not backwards compatible with AsciidoctorJ 1.5 and AsciidoctorJ 2.0 is not backwards compatible with AsciidoctorJ 1.6 or 1.5. The backwards incompatibilities are such that they cannot be worked around using reflection so code needs to be compiled against each of the three versions. To this end, this commit splits spring-restdocs-asciidoctor into several separate modules: 1. spring-restdocs-asciidoctor-support 2. spring-restdocs-asciidoctor-1.5 3. spring-restdocs-asciidoctor-1.6 4. spring-restdocs-asciidoctor-2.0 spring-restdocs-asciidoctor-support contains support code that is not tied to a specific version of AsciidoctorJ and can be used with 1.5, 1.6 and 2.0. The other three modules contain code that is specific to a particular version of AsciidoctorJ. Each version-specific module uses class names that are unique across all three modules and is written in such a way that they will back off when used in an environment with a different version of AsciidoctorJ. The existing spring-restdocs-asciidoctor module has been modified to merge the version specific jars and the support jar together into a single jar that supports AsciidoctorJ 1.5, 1.6, and 2.0. The above-described changes should allow users to depend upon spring-restdocs-asciidoctor as before and to now be able to use AsciidoctorJ 1.5, 1.6, or 2.0. Closes gh-581
1 parent 00da083 commit ff18f80

File tree

23 files changed

+427
-69
lines changed

23 files changed

+427
-69
lines changed

build.gradle

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ subprojects {
8383
dependency 'io.rest-assured:rest-assured:3.0.7'
8484
dependency 'org.apache.pdfbox:pdfbox:2.0.7'
8585
dependency 'org.assertj:assertj-core:2.9.1'
86-
dependency 'org.asciidoctor:asciidoctorj:1.5.8.1'
8786
dependency 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
8887
dependency 'org.hamcrest:hamcrest-core:1.3'
8988
dependency 'org.hamcrest:hamcrest-library:1.3'
@@ -123,27 +122,12 @@ subprojects {
123122

124123
}
125124

126-
configure(subprojects - project(":docs")) { subproject ->
125+
def codeProjects = subprojects - project(":docs")
126+
configure(codeProjects) {
127127
apply plugin: 'io.spring.javaformat'
128128
apply plugin: 'checkstyle'
129129
apply from: "${rootProject.projectDir}/gradle/publish-maven.gradle"
130130

131-
if (project.hasProperty('platformVersion') && subproject.path != ':spring-restdocs-asciidoctor') {
132-
apply plugin: 'spring-io'
133-
134-
repositories {
135-
maven { url "https://repo.spring.io/libs-snapshot" }
136-
}
137-
138-
dependencyManagement {
139-
springIoTestRuntime {
140-
imports {
141-
mavenBom "io.spring.platform:platform-bom:${platformVersion}"
142-
}
143-
}
144-
}
145-
}
146-
147131
checkstyle {
148132
configFile = rootProject.file('config/checkstyle/checkstyle.xml')
149133
configProperties = [ 'checkstyle.config.dir' : rootProject.file('config/checkstyle') ]
@@ -158,7 +142,10 @@ configure(subprojects - project(":docs")) { subproject ->
158142
checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:$javaFormatVersion"
159143
jacoco 'org.jacoco:org.jacoco.agent::runtime'
160144
}
145+
}
161146

147+
def publishedCodeProjects = codeProjects.findAll { codeProject -> !codeProject.name.contains('spring-restdocs-asciidoctor')}
148+
configure(publishedCodeProjects) { subproject ->
162149
javadoc {
163150
description = "Generates project-level javadoc for use in -javadoc jar"
164151
options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
@@ -243,8 +230,7 @@ task api (type: Javadoc) {
243230
options.links = javadocLinks
244231
options.addStringOption '-quiet'
245232

246-
source subprojects.findAll { project -> project.path != ":spring-restdocs-asciidoctor" }
247-
.collect { project -> project.sourceSets.main.allJava }
233+
source publishedCodeProjects.collect { project -> project.sourceSets.main.allJava }
248234

249235
destinationDir = new File(buildDir, "api")
250236

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
version=2.0.4.BUILD-SNAPSHOT
22
javaFormatVersion=0.0.7-SNAPSHOT
33
org.gradle.daemon=false
4+
asciidoctorj15Version=1.5.8.1
5+
asciidoctorj16Version=1.6.2
6+
asciidoctorj20Version=2.0.0-RC.3

settings.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ rootProject.name = 'spring-restdocs'
22

33
include 'docs'
44
include 'spring-restdocs-asciidoctor'
5+
include 'spring-restdocs-asciidoctor-1.5'
6+
include 'spring-restdocs-asciidoctor-1.6'
7+
include 'spring-restdocs-asciidoctor-2.0'
8+
include 'spring-restdocs-asciidoctor-support'
59
include 'spring-restdocs-core'
610
include 'spring-restdocs-mockmvc'
711
include 'spring-restdocs-restassured'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
description = 'AsciidoctorJ 1.5 extensions for Spring REST Docs'
2+
3+
dependencies {
4+
compileOnly "org.asciidoctor:asciidoctorj:$asciidoctorj15Version"
5+
implementation project(':spring-restdocs-asciidoctor-support')
6+
testCompile 'junit:junit'
7+
testCompile "org.asciidoctor:asciidoctorj:$asciidoctorj15Version"
8+
testCompile 'org.assertj:assertj-core'
9+
testRuntime project(':spring-restdocs-asciidoctor-support')
10+
}

spring-restdocs-asciidoctor/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesPreprocessor.java renamed to spring-restdocs-asciidoctor-1.5/src/main/java/org/springframework/restdocs/asciidoctor/DefaultAttributesAsciidoctorJ15Preprocessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
2626
*
2727
* @author Andy Wilkinson
2828
*/
29-
final class DefaultAttributesPreprocessor extends Preprocessor {
29+
final class DefaultAttributesAsciidoctorJ15Preprocessor extends Preprocessor {
3030

3131
private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver();
3232

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import org.asciidoctor.Asciidoctor;
20+
import org.asciidoctor.extension.spi.ExtensionRegistry;
21+
22+
/**
23+
* AsciidoctorJ 1.5 {@link ExtensionRegistry} for Spring REST Docs.
24+
*
25+
* @author Andy Wilkinson
26+
*/
27+
public final class RestDocsAsciidoctorJ15ExtensionRegistry implements ExtensionRegistry {
28+
29+
@Override
30+
public void register(Asciidoctor asciidoctor) {
31+
if (!asciidoctorJ15()) {
32+
return;
33+
}
34+
asciidoctor.javaExtensionRegistry()
35+
.preprocessor(new DefaultAttributesAsciidoctorJ15Preprocessor());
36+
asciidoctor.rubyExtensionRegistry()
37+
.loadClass(RestDocsAsciidoctorJ15ExtensionRegistry.class
38+
.getResourceAsStream("/extensions/operation_block_macro.rb"))
39+
.blockMacro("operation", "OperationBlockMacro");
40+
}
41+
42+
private boolean asciidoctorJ15() {
43+
try {
44+
return !Class.forName("org.asciidoctor.extension.JavaExtensionRegistry")
45+
.isInterface();
46+
}
47+
catch (Throwable ex) {
48+
return false;
49+
}
50+
}
51+
52+
}
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,36 +26,45 @@
2626
import static org.assertj.core.api.Assertions.assertThat;
2727

2828
/**
29-
* Tests for {@link DefaultAttributesPreprocessor}.
29+
* Tests for {@link DefaultAttributesAsciidoctorJ15Preprocessor}.
3030
*
3131
* @author Andy Wilkinson
3232
*/
33-
public class DefaultAttributesPreprocessorTests {
33+
public class DefaultAttributesAsciidoctorJ15PreprocessorTests {
3434

3535
@Test
3636
public void snippetsAttributeIsSet() {
37-
Options options = new Options();
38-
options.setAttributes(new Attributes("projectdir=../../.."));
39-
String converted = Asciidoctor.Factory.create().convert("{snippets}", options);
37+
String converted = createAsciidoctor().convert("{snippets}",
38+
createOptions("projectdir=../../.."));
4039
assertThat(converted)
4140
.contains("build" + File.separatorChar + "generated-snippets");
4241
}
4342

4443
@Test
4544
public void snippetsAttributeFromConvertArgumentIsNotOverridden() {
46-
Options options = new Options();
47-
options.setAttributes(new Attributes("snippets=custom projectdir=../../.."));
48-
String converted = Asciidoctor.Factory.create().convert("{snippets}", options);
45+
String converted = createAsciidoctor().convert("{snippets}",
46+
createOptions("snippets=custom projectdir=../../.."));
4947
assertThat(converted).contains("custom");
5048
}
5149

5250
@Test
5351
public void snippetsAttributeFromDocumentPreambleIsNotOverridden() {
54-
Options options = new Options();
55-
options.setAttributes(new Attributes("projectdir=../../.."));
56-
String converted = Asciidoctor.Factory.create()
57-
.convert(":snippets: custom\n{snippets}", options);
52+
String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}",
53+
createOptions("projectdir=../../.."));
5854
assertThat(converted).contains("custom");
5955
}
6056

57+
private Options createOptions(String attributes) {
58+
Options options = new Options();
59+
options.setAttributes(new Attributes(attributes));
60+
return options;
61+
}
62+
63+
private Asciidoctor createAsciidoctor() {
64+
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
65+
asciidoctor.javaExtensionRegistry()
66+
.preprocessor(new DefaultAttributesAsciidoctorJ15Preprocessor());
67+
return asciidoctor;
68+
}
69+
6170
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
description = 'AsciidoctorJ 1.6 extensions for Spring REST Docs'
2+
3+
dependencies {
4+
compileOnly "org.asciidoctor:asciidoctorj:$asciidoctorj16Version"
5+
implementation project(':spring-restdocs-asciidoctor-support')
6+
testCompile 'junit:junit'
7+
testCompile "org.asciidoctor:asciidoctorj:$asciidoctorj16Version"
8+
testCompile 'org.assertj:assertj-core'
9+
testRuntime project(':spring-restdocs-asciidoctor-support')
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import org.asciidoctor.ast.Document;
20+
import org.asciidoctor.extension.Preprocessor;
21+
import org.asciidoctor.extension.PreprocessorReader;
22+
23+
/**
24+
* {@link Preprocessor} that sets defaults for REST Docs-related {@link Document}
25+
* attributes.
26+
*
27+
* @author Andy Wilkinson
28+
*/
29+
final class DefaultAttributesAsciidoctorJ16Preprocessor extends Preprocessor {
30+
31+
private final SnippetsDirectoryResolver snippetsDirectoryResolver = new SnippetsDirectoryResolver();
32+
33+
@Override
34+
public void process(Document document, PreprocessorReader reader) {
35+
document.setAttribute("snippets", this.snippetsDirectoryResolver
36+
.getSnippetsDirectory(document.getAttributes()), false);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import org.asciidoctor.Asciidoctor;
20+
import org.asciidoctor.extension.spi.ExtensionRegistry;
21+
22+
/**
23+
* AsciidoctorJ 1.6 {@link ExtensionRegistry} for Spring REST Docs.
24+
*
25+
* @author Andy Wilkinson
26+
*/
27+
public final class RestDocsAsciidoctorJ16ExtensionRegistry implements ExtensionRegistry {
28+
29+
@Override
30+
public void register(Asciidoctor asciidoctor) {
31+
if (!asciidoctorJ16()) {
32+
return;
33+
}
34+
asciidoctor.javaExtensionRegistry()
35+
.preprocessor(new DefaultAttributesAsciidoctorJ16Preprocessor());
36+
asciidoctor.rubyExtensionRegistry()
37+
.loadClass(RestDocsAsciidoctorJ16ExtensionRegistry.class
38+
.getResourceAsStream("/extensions/operation_block_macro.rb"))
39+
.blockMacro("operation", "OperationBlockMacro");
40+
}
41+
42+
private boolean asciidoctorJ16() {
43+
try {
44+
return Class.forName("org.asciidoctor.extension.JavaExtensionRegistry")
45+
.isInterface();
46+
}
47+
catch (Throwable ex) {
48+
return false;
49+
}
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.asciidoctor;
18+
19+
import java.io.File;
20+
21+
import org.asciidoctor.Asciidoctor;
22+
import org.asciidoctor.Attributes;
23+
import org.asciidoctor.Options;
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link DefaultAttributesAsciidoctorJ16Preprocessor}.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
public class DefaultAttributesAsciidoctorJ16PreprocessorTests {
34+
35+
@Test
36+
public void snippetsAttributeIsSet() {
37+
String converted = createAsciidoctor().convert("{snippets}",
38+
createOptions("projectdir=../../.."));
39+
assertThat(converted)
40+
.contains("build" + File.separatorChar + "generated-snippets");
41+
}
42+
43+
@Test
44+
public void snippetsAttributeFromConvertArgumentIsNotOverridden() {
45+
String converted = createAsciidoctor().convert("{snippets}",
46+
createOptions("snippets=custom projectdir=../../.."));
47+
assertThat(converted).contains("custom");
48+
}
49+
50+
@Test
51+
public void snippetsAttributeFromDocumentPreambleIsNotOverridden() {
52+
String converted = createAsciidoctor().convert(":snippets: custom\n{snippets}",
53+
createOptions("projectdir=../../.."));
54+
assertThat(converted).contains("custom");
55+
}
56+
57+
private Options createOptions(String attributes) {
58+
Options options = new Options();
59+
options.setAttributes(new Attributes(attributes));
60+
return options;
61+
}
62+
63+
private Asciidoctor createAsciidoctor() {
64+
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
65+
asciidoctor.javaExtensionRegistry()
66+
.preprocessor(new DefaultAttributesAsciidoctorJ16Preprocessor());
67+
return asciidoctor;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)