1+ /*
2+ * Copyright 2013 - 2025 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+ * http://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+ package org.asciidoctor.gradle.model5.downdoc
17+
18+ import groovy.transform.CompileStatic
19+ import org.asciidoctor.gradle.model5.core.internal.publications.PublicationUtils
20+ import org.asciidoctor.gradle.model5.core.toolchains.AsciidoctorToolchain
21+ import org.asciidoctor.gradle.model5.downdoc.formatters.DowndocMarkdownOutputFormatter
22+ import org.asciidoctor.gradle.model5.downdoc.toolchains.DowndocToolchain
23+ import org.gradle.api.Action
24+ import org.gradle.api.ExtensiblePolymorphicDomainObjectContainer
25+ import org.gradle.api.NamedDomainObjectFactory
26+ import org.gradle.api.model.ObjectFactory
27+
28+ import java.util.function.Function
29+
30+ /**
31+ * Utilities for working with the new Asciidoctor model in an {@code downdoc } context.
32+ *
33+ * @author Schalk W. Cronjé
34+ *
35+ * @since 5.0
36+ */
37+ @CompileStatic
38+ class DowndocModel {
39+
40+ /**
41+ * The toolchain preparation task name
42+ * @param toolchainName Name of the toolchains
43+ * @return Task name
44+ */
45+ static String toolchainPrepareTaskName (String toolchainName ) {
46+ " ${ PublicationUtils.TASK_PREFIX} Prepare${ toolchainName.capitalize()} Toolchain"
47+ }
48+
49+ /**
50+ * Registers an output formatter on all the {@code downdoc } toolchains.
51+ *
52+ * @param toolchains Toolchain container
53+ * @param formatterClass The formatter class
54+ * @param factoryClass The factory for the formatter.
55+ * @param objectFactory objectFactory
56+ */
57+ static <T extends DowndocMarkdownOutputFormatter > void registerOutputFormatterFactory (
58+ ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain > toolchains ,
59+ Class<T> formatterClass ,
60+ Class<? extends NamedDomainObjectFactory<T> > factoryClass ,
61+ ObjectFactory objectFactory
62+ ) {
63+ registerOutputFormatterFactory(toolchains, formatterClass) { DowndocToolchain tc ->
64+ objectFactory. newInstance(factoryClass)
65+ }
66+ }
67+
68+ /**
69+ * Registers an output formatter on all the {@code downdoc } toolchains.
70+ *
71+ * @param toolchains Toolchain container
72+ * @param formatterClass The formatter class
73+ * @param factoryFunction A function that will create a factory given a specific toolchain instance.
74+ */
75+ static <T extends DowndocMarkdownOutputFormatter > void registerOutputFormatterFactory (
76+ ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain > toolchains ,
77+ Class<T> formatterClass ,
78+ Function<DowndocToolchain , NamedDomainObjectFactory<T> > factoryFunction
79+ ) {
80+ toolchains. withType(DowndocToolchain ). configureEach { tc ->
81+ tc. registeredOutputFormatters. registerFactory(formatterClass, factoryFunction. apply(tc))
82+ }
83+ }
84+
85+ /**
86+ * Registers a named formatter on each of the {@code downdoc } toolchains
87+ * @param toolchains Toolchains
88+ * @param formatterClass Formatter class
89+ * @param name Name of the formatter
90+ */
91+ static <T extends DowndocMarkdownOutputFormatter > void registerOutputFormatterOnAllToolchains (
92+ ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain > toolchains ,
93+ Class<T> formatterClass ,
94+ String name
95+ ) {
96+ registerOutputFormatterOnAllToolchains(toolchains, formatterClass, name) {
97+ }
98+ }
99+
100+ /**
101+ * Registers a named formatter on each of the {@code downdoc } toolchains.
102+ * @param toolchains Toolchains.
103+ * @param formatterClass Formatter class.
104+ * @param name Name of the .
105+ * @param configurator Configurator of the output formatter.
106+ */
107+ static <T extends DowndocMarkdownOutputFormatter > void registerOutputFormatterOnAllToolchains (
108+ ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain > toolchains ,
109+ Class<T> formatterClass ,
110+ String name ,
111+ Action<T> configurator
112+ ) {
113+ toolchains. withType(DowndocToolchain ). configureEach { tc ->
114+ final fc = tc. registeredOutputFormatters. register(name, formatterClass)
115+ fc. configure(configurator)
116+ }
117+ }
118+
119+ // /**
120+ // * Registers an extension on all the {@code asciidoctor.js} toolchains.
121+ // *
122+ // * @param toolchains Toolchain container
123+ // * @param extensionClass The extension class
124+ // * @param factoryClass The factory for the extension.
125+ // * @param objectFactory objectFactory
126+ // */
127+ // static <T extends AsciidoctorjsExtension> void registerExtensionFactory(
128+ // ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain> toolchains,
129+ // Class<T> extensionClass,
130+ // Class<? extends NamedDomainObjectFactory<T>> factoryClass,
131+ // ObjectFactory objectFactory
132+ // ) {
133+ // registerExtensionFactory(toolchains, extensionClass) { DowndocToolchain tc ->
134+ // objectFactory.newInstance(factoryClass, tc)
135+ // }
136+ // }
137+ //
138+ // /**
139+ // * Registers an extension factory on the {@code asciidoctor.js} toolchains.
140+ // *
141+ // * @param toolchains Toolchain container
142+ // * @param extensionClass The extension class
143+ // * @param factoryFunction A function that will create a factory given a specific toolchain instance.
144+ // */
145+ // static <T extends AsciidoctorjsExtension> void registerExtensionFactory(
146+ // ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain> toolchains,
147+ // Class<T> extensionClass,
148+ // Function<DowndocToolchain, NamedDomainObjectFactory<T>> factoryFunction
149+ // ) {
150+ // toolchains.withType(DowndocToolchain).configureEach { tc ->
151+ // tc.asciidocExtensions.registerFactory(extensionClass, factoryFunction.apply(tc))
152+ // }
153+ // }
154+ //
155+ // /**
156+ // * Registers an extension on each of the {@code asciidoctorj} toolchains
157+ // * @param toolchains Toolchains
158+ // * @param extensionClass Extension class
159+ // * @param name Name of the extension
160+ // */
161+ // static <T extends AsciidoctorjsExtension> void registerExtensionOnAllToolchains(
162+ // ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain> toolchains,
163+ // Class<T> extensionClass,
164+ // String name
165+ // ) {
166+ // registerExtensionOnAllToolchains(toolchains, extensionClass, name) {
167+ // }
168+ // }
169+ //
170+ // /**
171+ // * Registers an extension on each of the {@code asciidoctor.js} toolchains.
172+ // * @param toolchains Toolchains.
173+ // * @param extensionClass Extension class.
174+ // * @param name Name of the extension.
175+ // * @param configurator Configurator of the extension.
176+ // */
177+ // static <T extends AsciidoctorjsExtension> void registerExtensionOnAllToolchains(
178+ // ExtensiblePolymorphicDomainObjectContainer<AsciidoctorToolchain> toolchains,
179+ // Class<T> extensionClass,
180+ // String name,
181+ // Action<T> configurator
182+ // ) {
183+ // toolchains.withType(DowndocToolchain).configureEach { tc ->
184+ // final fc = tc.asciidocExtensions.register(name, extensionClass)
185+ // fc.configure(configurator)
186+ // }
187+ // }
188+ }
0 commit comments