11/*
2- * Copyright 2022 -present the original author or authors.
2+ * Copyright 2017 -present 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.
66 * You may obtain a copy of the License at
77 *
8- * https://www.apache.org/licenses/LICENSE-2.0
8+ * https://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * 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.
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+ * or implied. See the License for the specific language governing
14+ * permissions and limitations under the License.
1515 */
1616package io .spring .gradle .convention ;
1717
1818import java .io .File ;
19- import java .net .URI ;
2019import java .time .LocalDate ;
2120import java .util .ArrayList ;
2221import java .util .Collections ;
2928import org .asciidoctor .gradle .jvm .AsciidoctorJExtension ;
3029import org .asciidoctor .gradle .jvm .AsciidoctorJPlugin ;
3130import org .asciidoctor .gradle .jvm .AsciidoctorTask ;
31+ import org .gradle .api .JavaVersion ;
3232import org .gradle .api .Plugin ;
3333import org .gradle .api .Project ;
3434import org .gradle .api .artifacts .Configuration ;
35- import org .gradle .api .artifacts .dsl .RepositoryHandler ;
3635import org .gradle .api .file .DuplicatesStrategy ;
3736import org .gradle .api .file .FileTree ;
3837import org .gradle .api .tasks .Sync ;
3938
4039/**
4140 * Conventions that are applied in the presence of the {@link AsciidoctorJPlugin}.
42- *
41+ * <p/>
4342 * When the plugin is applied:
4443 *
4544 * <ul>
6362 *
6463 * @author Andy Wilkinson
6564 * @author Rob Winch
65+ * @author John Blum
6666 */
6767public class AsciidoctorConventionPlugin implements Plugin <Project > {
6868
69- private static final String SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION = "0.4.2.RELEASE" ;
69+ private static final String ASCIIDOCTORJ_VERSION = "2.4.3" ;
70+ private static final String SPRING_ASCIIDOCTOR_BACKENDS_VERSION = "0.0.5" ;
7071 private static final String SPRING_DOC_RESOURCES_VERSION = "0.2.5" ;
7172
72- private static final String SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY =
73- String .format ("io.spring.asciidoctor:spring-asciidoctor-extensions-block-switch :%s" ,
74- SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION );
73+ private static final String SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY =
74+ String .format ("io.spring.asciidoctor.backends :spring-asciidoctor-backends :%s" ,
75+ SPRING_ASCIIDOCTOR_BACKENDS_VERSION );
7576
77+ @ SuppressWarnings ("unused" )
7678 private static final String SPRING_DOC_RESOURCES_DEPENDENCY =
7779 String .format ("io.spring.docresources:spring-doc-resources:%s" , SPRING_DOC_RESOURCES_VERSION );
7880
@@ -81,19 +83,16 @@ public void apply(Project project) {
8183
8284 project .getPlugins ().withType (AsciidoctorJPlugin .class , asciidoctorPlugin -> {
8385
84- createDefaultAsciidoctorRepository (project );
86+ setAsciidoctorJVersion (project );
8587 makeAllWarningsFatal (project );
88+ createAsciidoctorExtensionsConfiguration (project );
8689
8790 Sync unzipResources = createUnzipDocumentationResourcesTask (project );
8891
8992 project .getTasks ().withType (AbstractAsciidoctorTask .class , asciidoctorTask -> {
9093
9194 asciidoctorTask .dependsOn (unzipResources );
92- configureAttributes (project , asciidoctorTask );
93- configureExtensions (project , asciidoctorTask );
94- configureOptions (asciidoctorTask );
95- asciidoctorTask .baseDirFollowsSourceDir ();
96- asciidoctorTask .useIntermediateWorkDir ();
95+ configureAsciidoctorTask (project , asciidoctorTask );
9796
9897 asciidoctorTask .resources (resourcesSpec -> {
9998 resourcesSpec .setDuplicatesStrategy (DuplicatesStrategy .INCLUDE );
@@ -105,45 +104,52 @@ public void apply(Project project) {
105104 // resourcesSrcDirSpec.include("images/**");
106105 });
107106 });
108-
109- if (asciidoctorTask instanceof AsciidoctorTask ) {
110- configureHtmlOnlyAttributes (project , asciidoctorTask );
111- }
112107 });
113108 });
114109 }
115110
116- private void createDefaultAsciidoctorRepository (Project project ) {
111+ private void setAsciidoctorJVersion (Project project ) {
112+ project .getExtensions ().getByType (AsciidoctorJExtension .class ).setVersion (ASCIIDOCTORJ_VERSION );
113+ }
114+
115+ private void makeAllWarningsFatal (Project project ) {
116+ project .getExtensions ().getByType (AsciidoctorJExtension .class ).fatalWarnings (".*" );
117+ }
118+
119+ private void createAsciidoctorExtensionsConfiguration (Project project ) {
120+
121+ project .getConfigurations ().create ("asciidoctorExtensions" , configuration -> {
117122
118- project .getGradle ().afterProject (it -> {
123+ project .getConfigurations ()
124+ .matching (it -> "dependencyManagement" .equals (it .getName ()))
125+ .all (configuration ::extendsFrom );
119126
120- RepositoryHandler repositories = it .getRepositories ();
127+ configuration .getDependencies ()
128+ .add (project .getDependencies ().create (SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY ));
121129
122- if (repositories .isEmpty ()) {
123- repositories .mavenCentral ();
124- repositories .maven (repo -> repo .setUrl (URI .create ("https://repo.spring.io/release" )));
125- }
130+ // TODO: Why is the asiidoctorj-pdf dependency needed?
131+ configuration .getDependencies ()
132+ .add (project .getDependencies ().create ("org.asciidoctor:asciidoctorj-pdf:1.5.3" ));
126133 });
127134 }
128135
129136 /**
130- * Requests the base Spring Documentation Resources from {@literal https://repo.spring.io/release } and uses it
131- * to format and render documentation.
137+ * Requests the base Spring Documentation Resources from {@literal Maven Central } and uses it to format
138+ * and render documentation.
132139 *
133140 * @param project {@literal this} Gradle {@link Project}.
134- * @return a {@link Sync} task used to copy the Spring Documentation Resources to a build directory
141+ * @return a {@link Sync} task that copies Spring Documentation Resources to the build directory
135142 * used to generate documentation.
136- * @see <a href="https://repo.spring.io/ui/native/release/io/spring/docresources/spring-doc-resources">spring-doc-resources</a>
137143 * @see org.gradle.api.tasks.Sync
138144 * @see org.gradle.api.Project
139145 */
140146 @ SuppressWarnings ("all" )
141147 private Sync createUnzipDocumentationResourcesTask (Project project ) {
142148
143- Configuration documentationResources = project .getConfigurations ().maybeCreate ("documentationResources" );
149+ Configuration documentationResources = project .getConfigurations ().create ("documentationResources" );
144150
145151 documentationResources .getDependencies ()
146- .add (project .getDependencies ().create (SPRING_DOC_RESOURCES_DEPENDENCY ));
152+ .add (project .getDependencies ().create (SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY ));
147153
148154 Sync unzipResources = project .getTasks ().create ("unzipDocumentationResources" , Sync .class , sync -> {
149155
@@ -166,65 +172,71 @@ private Sync createUnzipDocumentationResourcesTask(Project project) {
166172 return unzipResources ;
167173 }
168174
169- @ SuppressWarnings ("unused" )
175+ private void configureAsciidoctorTask (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
176+
177+ asciidoctorTask .baseDirFollowsSourceDir ();
178+ asciidoctorTask .configurations ("asciidoctorExtensions" );
179+ //asciidoctorTask.useIntermediateWorkDir();
180+
181+ configureAttributes (project , asciidoctorTask );
182+ configureForkOptions (asciidoctorTask );
183+ configureOptions (asciidoctorTask );
184+
185+ if (asciidoctorTask instanceof AsciidoctorTask ) {
186+ boolean pdf = asciidoctorTask .getName ().toLowerCase ().contains ("pdf" );
187+ String backend = pdf ? "spring-pdf" : "spring-html" ;
188+ ((AsciidoctorTask ) asciidoctorTask ).outputOptions ((outputOptions ) -> outputOptions .backends (backend ));
189+ configureHtmlOnlyAttributes (asciidoctorTask );
190+ }
191+ }
192+
170193 private void configureAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
171194
172195 Map <String , Object > attributes = new HashMap <>();
173196
174197 attributes .put ("attribute-missing" , "warn" );
175- attributes .put ("icons " , "font " );
198+ attributes .put ("docinfo " , "shared " );
176199 attributes .put ("idprefix" , "" );
177200 attributes .put ("idseparator" , "-" );
178- attributes .put ("docinfo" , "shared" );
179201 attributes .put ("sectanchors" , "" );
180202 attributes .put ("sectnums" , "" );
181203 attributes .put ("today-year" , LocalDate .now ().getYear ());
182204
183- asciidoctorTask .attributes (attributes );
184- }
185-
186- private void configureExtensions (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
205+ Object version = project .getVersion ();
187206
188- Configuration extensionsConfiguration = project .getConfigurations ().maybeCreate ("asciidoctorExtensions" );
207+ if (version != null && !Project .DEFAULT_VERSION .equals (version )) {
208+ attributes .put ("revnumber" , version );
209+ }
189210
190- extensionsConfiguration . defaultDependencies ( dependencies -> dependencies . add ( project . getDependencies ()
191- . create ( SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY )));
211+ asciidoctorTask . attributes ( attributes );
212+ }
192213
193- asciidoctorTask .configurations (extensionsConfiguration );
214+ private void configureForkOptions (AbstractAsciidoctorTask asciidoctorTask ) {
215+ if (JavaVersion .current ().isCompatibleWith (JavaVersion .VERSION_16 )) {
216+ asciidoctorTask .forkOptions (options -> options .jvmArgs (
217+ "--add-opens" , "java.base/sun.nio.ch=ALL-UNNAMED" ,
218+ "--add-opens" , "java.base/java.io=ALL-UNNAMED" )
219+ );
220+ }
194221 }
195222
196- private void configureHtmlOnlyAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
223+ private void configureHtmlOnlyAttributes (AbstractAsciidoctorTask asciidoctorTask ) {
197224
198225 Map <String , Object > attributes = new HashMap <>();
199226
200- attributes .put ("source-highlighter" , "highlight.js" );
201227 attributes .put ("highlightjsdir" , "js/highlight" );
202228 attributes .put ("highlightjs-theme" , "github" );
203- attributes .put ("linkcss " , true );
229+ attributes .put ("source-highlighter " , "highlight.js" );
204230 attributes .put ("icons" , "font" );
205- attributes .put ("stylesheet" , "css/spring.css" );
206-
207- asciidoctorTask .getAttributeProviders ().add (() -> {
208-
209- Object version = project .getVersion ();
210-
211- Map <String , Object > localAttributes = new HashMap <>();
212-
213- if (version != null && !Project .DEFAULT_VERSION .equals (version )) {
214- localAttributes .put ("revnumber" , version );
215- }
216-
217- return localAttributes ;
218- });
231+ attributes .put ("imagesdir" , "./images" );
232+ attributes .put ("linkcss" , true );
233+ attributes .put ("stylesdir" , "css/" );
234+ //attributes.put("stylesheet", "spring.css");
219235
220236 asciidoctorTask .attributes (attributes );
221237 }
222238
223239 private void configureOptions (AbstractAsciidoctorTask asciidoctorTask ) {
224240 asciidoctorTask .options (Collections .singletonMap ("doctype" , "book" ));
225241 }
226-
227- private void makeAllWarningsFatal (Project project ) {
228- project .getExtensions ().getByType (AsciidoctorJExtension .class ).fatalWarnings (".*" );
229- }
230242}
0 commit comments