6666 */
6767public class AsciidoctorConventionPlugin implements Plugin <Project > {
6868
69+ private static final String ASCIIDOCTORJ_VERSION = "2.4.3" ;
6970 private static final String SPRING_ASCIIDOCTOR_BACKENDS_VERSION = "0.0.5" ;
70- private static final String SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION = "0.6.1" ;
7171 private static final String SPRING_DOC_RESOURCES_VERSION = "0.2.5" ;
7272
7373 private static final String SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY =
7474 String .format ("io.spring.asciidoctor.backends:spring-asciidoctor-backends:%s" ,
7575 SPRING_ASCIIDOCTOR_BACKENDS_VERSION );
7676
77- private static final String SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY =
78- String .format ("io.spring.asciidoctor:spring-asciidoctor-extensions-block-switch:%s" ,
79- SPRING_ASCIIDOCTOR_EXTENSIONS_BLOCK_SWITCH_VERSION );
80-
8177 @ SuppressWarnings ("unused" )
8278 private static final String SPRING_DOC_RESOURCES_DEPENDENCY =
8379 String .format ("io.spring.docresources:spring-doc-resources:%s" , SPRING_DOC_RESOURCES_VERSION );
@@ -87,20 +83,16 @@ public void apply(Project project) {
8783
8884 project .getPlugins ().withType (AsciidoctorJPlugin .class , asciidoctorPlugin -> {
8985
90- configureMavenCentralRepositoryForAsciidoctor (project );
86+ setAsciidoctorJVersion (project );
9187 makeAllWarningsFatal (project );
88+ createAsciidoctorExtensionsConfiguration (project );
9289
93- Sync unzipResources = createSyncDocumentationResourcesTask (project );
90+ Sync unzipResources = createUnzipDocumentationResourcesTask (project );
9491
9592 project .getTasks ().withType (AbstractAsciidoctorTask .class , asciidoctorTask -> {
9693
9794 asciidoctorTask .dependsOn (unzipResources );
98- configureAttributes (project , asciidoctorTask );
99- configureExtensions (project , asciidoctorTask );
100- configureForkOptions (asciidoctorTask );
101- configureOptions (asciidoctorTask );
102- asciidoctorTask .baseDirFollowsSourceDir ();
103- asciidoctorTask .useIntermediateWorkDir ();
95+ configureAsciidoctorTask (project , asciidoctorTask );
10496
10597 asciidoctorTask .resources (resourcesSpec -> {
10698 resourcesSpec .setDuplicatesStrategy (DuplicatesStrategy .INCLUDE );
@@ -112,33 +104,49 @@ public void apply(Project project) {
112104 // resourcesSrcDirSpec.include("images/**");
113105 });
114106 });
115-
116- if (asciidoctorTask instanceof AsciidoctorTask ) {
117- configureHtmlOnlyAttributes (project , asciidoctorTask );
118- }
119107 });
120108 });
121109 }
122110
123- private void configureMavenCentralRepositoryForAsciidoctor (Project project ) {
124- project .getGradle ().afterProject (it -> it .getRepositories ().mavenCentral ());
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 -> {
122+
123+ project .getConfigurations ()
124+ .matching (it -> "dependencyManagement" .equals (it .getName ()))
125+ .all (configuration ::extendsFrom );
126+
127+ configuration .getDependencies ()
128+ .add (project .getDependencies ().create (SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY ));
129+
130+ // TODO: Why is the asiidoctorj-pdf dependency needed?
131+ configuration .getDependencies ()
132+ .add (project .getDependencies ().create ("org.asciidoctor:asciidoctorj-pdf:1.5.3" ));
133+ });
125134 }
126135
127136 /**
128- * Requests the base Spring Documentation Resources from {@literal https://repo.spring.io/release } and uses it
129- * 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.
130139 *
131140 * @param project {@literal this} Gradle {@link Project}.
132- * @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
133142 * used to generate documentation.
134- * @see <a href="https://repo.spring.io/ui/native/release/io/spring/docresources/spring-doc-resources">spring-doc-resources</a>
135143 * @see org.gradle.api.tasks.Sync
136144 * @see org.gradle.api.Project
137145 */
138146 @ SuppressWarnings ("all" )
139- private Sync createSyncDocumentationResourcesTask (Project project ) {
147+ private Sync createUnzipDocumentationResourcesTask (Project project ) {
140148
141- Configuration documentationResources = project .getConfigurations ().maybeCreate ("documentationResources" );
149+ Configuration documentationResources = project .getConfigurations ().create ("documentationResources" );
142150
143151 documentationResources .getDependencies ()
144152 .add (project .getDependencies ().create (SPRING_ASCIIDOCTOR_BACKENDS_DEPENDENCY ));
@@ -164,31 +172,43 @@ private Sync createSyncDocumentationResourcesTask(Project project) {
164172 return unzipResources ;
165173 }
166174
167- @ 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+
168193 private void configureAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
169194
170195 Map <String , Object > attributes = new HashMap <>();
171196
172197 attributes .put ("attribute-missing" , "warn" );
173198 attributes .put ("docinfo" , "shared" );
174- attributes .put ("icons" , "font" );
175199 attributes .put ("idprefix" , "" );
176200 attributes .put ("idseparator" , "-" );
177201 attributes .put ("sectanchors" , "" );
178202 attributes .put ("sectnums" , "" );
179203 attributes .put ("today-year" , LocalDate .now ().getYear ());
180204
181- asciidoctorTask .attributes (attributes );
182- }
183-
184- private void configureExtensions (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
205+ Object version = project .getVersion ();
185206
186- Configuration extensionsConfiguration = project .getConfigurations ().maybeCreate ("asciidoctorExtensions" );
187-
188- extensionsConfiguration .defaultDependencies (dependencies -> dependencies .add (project .getDependencies ()
189- .create (SPRING_ASCIIDOCTOR_EXTENSION_BLOCK_SWITCH_DEPENDENCY )));
207+ if (version != null && !Project .DEFAULT_VERSION .equals (version )) {
208+ attributes .put ("revnumber" , version );
209+ }
190210
191- asciidoctorTask .configurations ( extensionsConfiguration );
211+ asciidoctorTask .attributes ( attributes );
192212 }
193213
194214 private void configureForkOptions (AbstractAsciidoctorTask asciidoctorTask ) {
@@ -200,37 +220,23 @@ private void configureForkOptions(AbstractAsciidoctorTask asciidoctorTask) {
200220 }
201221 }
202222
203- private void configureHtmlOnlyAttributes (Project project , AbstractAsciidoctorTask asciidoctorTask ) {
223+ private void configureHtmlOnlyAttributes (AbstractAsciidoctorTask asciidoctorTask ) {
204224
205225 Map <String , Object > attributes = new HashMap <>();
206226
207- attributes .put ("source-highlighter" , "highlight.js" );
208227 attributes .put ("highlightjsdir" , "js/highlight" );
209228 attributes .put ("highlightjs-theme" , "github" );
210- attributes .put ("linkcss " , true );
229+ attributes .put ("source-highlighter " , "highlight.js" );
211230 attributes .put ("icons" , "font" );
212-
213- asciidoctorTask .getAttributeProviders ().add (() -> {
214-
215- Object version = project .getVersion ();
216-
217- Map <String , Object > localAttributes = new HashMap <>();
218-
219- if (version != null && !Project .DEFAULT_VERSION .equals (version )) {
220- localAttributes .put ("revnumber" , version );
221- }
222-
223- return localAttributes ;
224- });
231+ attributes .put ("imagesdir" , "./images" );
232+ attributes .put ("linkcss" , true );
233+ attributes .put ("stylesdir" , "css/" );
234+ //attributes.put("stylesheet", "spring.css");
225235
226236 asciidoctorTask .attributes (attributes );
227237 }
228238
229239 private void configureOptions (AbstractAsciidoctorTask asciidoctorTask ) {
230240 asciidoctorTask .options (Collections .singletonMap ("doctype" , "book" ));
231241 }
232-
233- private void makeAllWarningsFatal (Project project ) {
234- project .getExtensions ().getByType (AsciidoctorJExtension .class ).fatalWarnings (".*" );
235- }
236242}
0 commit comments