Skip to content

Commit

Permalink
Fix incompatibility with AsciidoctorJ 3.0
Browse files Browse the repository at this point in the history
AsciidoctorJ 3.0 contains a breaking change to the signature of
Preprocessor#process. This commit avoids this incompatibility
by rewriting the preprocessor extension in Ruby.

Fixes gh-949
  • Loading branch information
wilkinsona committed Oct 31, 2024
1 parent 690371b commit 0abc763
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 72 deletions.
9 changes: 9 additions & 0 deletions spring-restdocs-asciidoctor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id "java-library"
id "maven-publish"
id "io.spring.compatibility-test" version "0.0.3"
}

description = "Spring REST Docs Asciidoctor Extension"
Expand All @@ -19,3 +20,11 @@ dependencies {

testRuntimeOnly("org.asciidoctor:asciidoctorj-pdf")
}

compatibilityTest {
dependency("AsciidoctorJ") { asciidoctorj ->
asciidoctorj.groupId = "org.asciidoctor"
asciidoctorj.artifactId = "asciidoctorj"
asciidoctorj.versions = ["3.0.0"]
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,9 @@ public final class RestDocsExtensionRegistry implements ExtensionRegistry {

@Override
public void register(Asciidoctor asciidoctor) {
asciidoctor.javaExtensionRegistry().preprocessor(new DefaultAttributesPreprocessor());
asciidoctor.rubyExtensionRegistry()
.loadClass(RestDocsExtensionRegistry.class.getResourceAsStream("/extensions/default_attributes.rb"))
.preprocessor("DefaultAttributes");
asciidoctor.rubyExtensionRegistry()
.loadClass(RestDocsExtensionRegistry.class.getResourceAsStream("/extensions/operation_block_macro.rb"))
.blockMacro("operation", "OperationBlockMacro");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,9 +30,14 @@
*
* @author Andy Wilkinson
*/
class SnippetsDirectoryResolver {
public class SnippetsDirectoryResolver {

File getSnippetsDirectory(Map<String, Object> attributes) {
/**
* Returns the snippets directory derived from the given {@code attributes}.
* @param attributes the attributes
* @return the snippets directory
*/
public File getSnippetsDirectory(Map<String, Object> attributes) {
if (System.getProperty("maven.home") != null) {
return getMavenSnippetsDirectory(attributes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'asciidoctor/extensions'
require 'java'

class DefaultAttributes < Asciidoctor::Extensions::Preprocessor

def process(document, reader)
resolver = org.springframework.restdocs.asciidoctor.SnippetsDirectoryResolver.new()
attributes = document.attributes
attributes["snippets"] = resolver.getSnippetsDirectory(attributes) unless attributes.has_key?("snippets")
false
end

end

This file was deleted.

0 comments on commit 0abc763

Please sign in to comment.