Skip to content

Commit cbeeb51

Browse files
committed
(#764) Add better support for allow-uri-read
1 parent e660562 commit cbeeb51

File tree

17 files changed

+242
-103
lines changed

17 files changed

+242
-103
lines changed

buildSrc/src/main/groovy/org/asciidoctor/internal/model5/TestAllPlugins.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class TestAllPlugins implements Plugin<Project> {
6666
gradleTest.testSets.create('jdk17') { GradleTestSet ts ->
6767
ts.versions(gradle9OrLater)
6868
ts.gradleArguments('-s')
69+
ts.debug = true
6970
}
7071

7172
gradleTest.testSets.create('jdk21') { GradleTestSet ts ->

docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* xref:model5/asciidoctorj/kroki.adoc[]
1818
* xref:model5/asciidoctorj/adding-an-external-backend.adoc[]
1919
* xref:model5/asciidoctorj/adding-an-external-extension.adoc[]
20+
* xref:model5/asciidoctorj/engine-options.adoc[]
2021
2122
.Asciidoctor.js
2223
* xref:model5/asciidoctorjs/html.adoc[]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
= Engine options
2+
3+
[tabs]
4+
====
5+
Groovy::
6+
+
7+
--
8+
[source,groovy]
9+
----
10+
import org.asciidoctor.gradle.model5.jvm.toolchains.AsciidoctorjToolchain
11+
12+
asciidoc {
13+
toolchains {
14+
asciidoctorj(AsciidoctorjToolchain) {
15+
engineOptions {
16+
eruby = 'erb' // <.>
17+
catalogAssets = true // <.>
18+
sourceMap = true // <.>
19+
}
20+
}
21+
}
22+
}
23+
----
24+
<.> The template engine to use.
25+
It is a case-insensitive string and has to be one of `erb`, `erubi`, or `erubis`.
26+
<.> Whether to capture images and links in the reference table.
27+
<.> Whether to track file and line numbers for parsed blocks.
28+
The default is to track.
29+
There is a slight performance improvement, by turning it off.
30+
--
31+
====

docs/modules/ROOT/pages/model5/asciidoctorj/kroki.adoc

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,112 @@ plugins {
3838
:partials-package-part: jvm
3939
include::partial$kroki-options.adoc[]
4040

41+
== Using Kroki with EPUB and PDF
42+
43+
In order to you use Kroki images with EPUB and PDF you have one of two options to have the options downloaded.
44+
45+
* Have Kroki download the images.
46+
* Set the `allow-uri-read` attribute on the source set.
47+
48+
Both of these options affect all the publications that use the specific toolchain.
49+
If this is not to your liking, you can create an additional `asciidoctorj` toolchain and set the Kroki setting only on that toolchain.
50+
51+
.Using an additional toolchain
52+
[tabs]
53+
====
54+
Groovy::
55+
+
56+
--
57+
[source,groovy]
58+
[subs=attributes+]
59+
.build.gradle
60+
----
61+
import org.asciidoctor.gradle.model5.jvm.toolchains.AsciidoctorjToolchain
62+
import org.asciidoctor.gradle.model5.jvm.extensions.AsciidoctorjKrokiExtension
63+
64+
asciidoc {
65+
toolchains {
66+
asciidoctorj2(AsciidoctorjToolchain) {
67+
asciidocExtensions {
68+
kroki(AsciidoctorjKrokiExtension) {
69+
fetchDiagrams = true
70+
}
71+
}
72+
}
73+
}
74+
75+
publications {
76+
main {
77+
output 'asciidoctorj', 'html' // <.>
78+
output 'asciidoctorj2', 'epub' // <.>
79+
}
80+
}
81+
}
82+
----
83+
<.> The HTML backend will use the links to the Kroki server.
84+
<.> The EPUB backend will use the images downloaded by the second `asciidoctorj` toolchain.
85+
--
86+
====
87+
88+
.Setting the `allow-uri-read` for the source set
89+
[tabs]
90+
====
91+
Groovy::
92+
+
93+
--
94+
[source,groovy]
95+
[subs=attributes+]
96+
.build.gradle
97+
----
98+
import org.asciidoctor.gradle.model5.jvm.toolchains.AsciidoctorjToolchain
99+
import org.asciidoctor.gradle.model5.jvm.extensions.AsciidoctorjKrokiExtension
100+
101+
asciidoc {
102+
publications {
103+
main {
104+
sourceSet {
105+
attributes {
106+
add 'allow-uri-read', 1 // <.>
107+
}
108+
}
109+
}
110+
}
111+
}
112+
----
113+
<.> Sets the attribute.
114+
You might consider https://docs.asciidoctor.org/asciidoc/latest/directives/include-uri/#caching-uri-content[caching the content], by adding a custom extension which uses the `open-uri-cached` GEM.
115+
--
116+
====
117+
118+
It might be better to only turn that on for PDF, which is the recommended approach if you have multiple outputs on the same source set.
119+
120+
.Setting the `allow-uri-read` for the source set
121+
[tabs]
122+
====
123+
Groovy::
124+
+
125+
--
126+
[source,groovy]
127+
[subs=attributes+]
128+
.build.gradle
129+
----
130+
import org.asciidoctor.gradle.model5.jvm.formatters.AsciidoctorjPdf
131+
132+
asciidoc {
133+
toolchains {
134+
asciidoctorj {
135+
registeredOutputFormatters {
136+
pdf(AsciidoctorjPdf) {
137+
allowUriRead = true
138+
}
139+
}
140+
}
141+
}
142+
}
143+
----
144+
<.> Sets the attribute only on the PDF output formatter
145+
You might consider https://docs.asciidoctor.org/asciidoc/latest/directives/include-uri/#caching-uri-content[caching the content], by adding a custom extension which uses the `open-uri-cached` GEM.
146+
--
147+
====
148+
41149

docs/modules/ROOT/pages/model5/asciidoctorj/pdf.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ asciidoc {
3333
pdf(AsciidoctorjPdf) {
3434
useTheme('default-with-font-fallbacks') // <.>
3535
useTheme('myLocal') // <.>
36+
allowUriRead = true // <.>
3637
}
3738
}
3839
}
@@ -46,6 +47,8 @@ asciidoc {
4647
----
4748
<.> Use a built-in theme.
4849
<.> Use the `myLocal` theme from `asciidocPdfThemes`.
50+
<.> Turn this on if you need to read remote images via URI.
51+
Please read https://docs.asciidoctor.org/asciidoc/latest/directives/include-uri/[Include Content by URI] to understand this setting.
4952

5053
== Setting other parameters
5154

docs/modules/ROOT/pages/model5/asciidoctorjs/kroki.adoc

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -37,80 +37,3 @@ plugins {
3737
:partials-package-part: js
3838
include::partial$kroki-options.adoc[]
3939

40-
== Using Kroki with EPUB and PDF
41-
42-
In order to you use Kroki images with EPUB and PDF you have one of two options to have the options downloaded.
43-
44-
* Have Kroki download the images.
45-
* Set the `allow-uri-read` attribute on the source set.
46-
47-
Both of these options affect all the publications that use the specific toolchain.
48-
If this is not to your liking, you can create an additional `asciidoctorj` toolchain and set the Kroki setting only on that toolchain.
49-
50-
.Using an additional toolchain
51-
[tabs]
52-
====
53-
Groovy::
54-
+
55-
--
56-
[source,groovy]
57-
[subs=attributes+]
58-
.build.gradle
59-
----
60-
import org.asciidoctor.gradle.model5.jvm.toolchains.AsciidoctorjToolchain
61-
import org.asciidoctor.gradle.model5.jvm.extensions.AsciidoctorjKrokiExtension
62-
63-
asciidoc {
64-
toolchains {
65-
asciidoctorj2(AsciidoctorjToolchain) {
66-
asciidocExtensions {
67-
kroki(AsciidoctorjKrokiExtension) {
68-
fetchDiagrams = true
69-
}
70-
}
71-
}
72-
}
73-
74-
publications {
75-
main {
76-
output 'asciidoctorj', 'html' // <.>
77-
output 'asciidoctorj2', 'epub' // <.>
78-
}
79-
}
80-
}
81-
----
82-
<.> The HTML backend will use the links to the Kroki server.
83-
<.> The EPUB backend will use the images downloaded by the second `asciidoctorj` toolchain.
84-
--
85-
====
86-
87-
.Setting the `allow-uri-read` for the source set
88-
[tabs]
89-
====
90-
Groovy::
91-
+
92-
--
93-
[source,groovy]
94-
[subs=attributes+]
95-
.build.gradle
96-
----
97-
import org.asciidoctor.gradle.model5.jvm.toolchains.AsciidoctorjToolchain
98-
import org.asciidoctor.gradle.model5.jvm.extensions.AsciidoctorjKrokiExtension
99-
100-
asciidoc {
101-
publications {
102-
main {
103-
sourceSet {
104-
attributes {
105-
add 'allow-uri-read', 1 // <.>
106-
}
107-
}
108-
}
109-
}
110-
}
111-
----
112-
<.> Sets the attribute.
113-
You might consider https://docs.asciidoctor.org/asciidoc/latest/directives/include-uri/#caching-uri-content[caching the content], by adding a custom extension which uses the `open-uri-cached` GEM.
114-
--
115-
====
116-

docs/modules/ROOT/pages/model5/configure-toolchains.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Users do not usually need to create toolchains directly, but might need to confi
99
(Output formatters are effectively what is also know as Asciidoctor backends).
1010

1111
In some special cases a user might want to create another toolchain of the same type, but configure it differently.
12+
1213
[source,groovy]
1314
----
1415
import org.asciidoctor.gradle.model5.jvm.core.AsciidoctorJToolchain // <.>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.asciidoctor.gradle.model5.core.formatters;
2+
3+
/**
4+
* Formatter needs the option to read URIs.
5+
*
6+
* @author Schalk W. Cronjé
7+
* @since 5.0
8+
*/
9+
public interface HasAllowUriRead {
10+
/**
11+
* Sets whether content can be read from URIs.
12+
*
13+
* @param flag {@code true} to allow URI reads.
14+
*/
15+
void setAllowUriRead(boolean flag);
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.asciidoctor.gradle.model5.core.internal.formatters
2+
3+
import groovy.transform.CompileStatic
4+
import org.asciidoctor.gradle.model5.core.attributes.HasAttributeProvider
5+
import org.asciidoctor.gradle.model5.core.formatters.HasAllowUriRead
6+
import org.gradle.api.Project
7+
import org.gradle.api.provider.Property
8+
import org.gradle.api.provider.Provider
9+
10+
import javax.inject.Inject
11+
12+
import static java.util.Collections.EMPTY_MAP
13+
import static org.ysb33r.grolifant5.api.core.StringTools.EMPTY
14+
15+
/**
16+
* Default implementation of {@link HasAllowUriRead}.
17+
*
18+
* @author Schalk W. Cronjé
19+
*
20+
* @since 5.0
21+
*/
22+
@CompileStatic
23+
class DefaultAllowUriRead implements HasAllowUriRead, HasAttributeProvider {
24+
25+
final Provider<Map<String, Object>> attributeProvider
26+
private final Property<Boolean> allowUri
27+
28+
@Inject
29+
DefaultAllowUriRead(Project project) {
30+
this.allowUri = project.objects.property(Boolean).value(false)
31+
this.attributeProvider = this.allowUri.map {
32+
it ? ['allow-uri-read': EMPTY] : EMPTY_MAP
33+
}
34+
}
35+
36+
@Override
37+
void setAllowUriRead(boolean flag) {
38+
this.allowUri.set(flag)
39+
}
40+
}

model5/core/src/main/groovy/org/asciidoctor/gradle/model5/core/publications/AsciidoctorPublication.groovy

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.gradle.api.Named
3232
import org.gradle.api.NamedDomainObjectContainer
3333
import org.gradle.api.Project
3434
import org.gradle.api.model.ObjectFactory
35+
import org.gradle.api.provider.Provider
3536
import org.gradle.api.tasks.TaskProvider
3637
import org.gradle.api.tasks.util.PatternFilterable
3738
import org.ysb33r.grolifant5.api.core.ClosureUtils
@@ -158,7 +159,12 @@ class AsciidoctorPublication implements Named {
158159
final newOutput = this.outputs.create(finalName).tap { DefaultAsciidoctorOutputData it ->
159160
configureFrom(owner.name, toolchain, formatter, sourceSet)
160161
}
161-
final task = registerConversionTask(toolchain, newOutput, formatter.copyResources)
162+
final task = registerConversionTask(
163+
toolchain,
164+
newOutput,
165+
formatter.attributeProvider,
166+
formatter.copyResources
167+
)
162168

163169
task.configure { AsciidoctorTask t ->
164170
formatter.configureTaskInputs(t.inputs)
@@ -170,12 +176,13 @@ class AsciidoctorPublication implements Named {
170176
private TaskProvider<? extends AsciidoctorTask> registerConversionTask(
171177
AsciidoctorToolchain toolchain,
172178
AsciidoctorOutputData outputData,
179+
Provider<Map<String,Object>> formatterAttrs,
173180
boolean copyResources
174181
) {
175182
final taskFactory = objectFactory.newInstance(TaskFactory)
176183
final taskName = PublicationUtils.conversionTaskName(name, outputData.name)
177184
final extensionAttributes = objectFactory.mapProperty(String, Object)
178-
185+
extensionAttributes.putAll(formatterAttrs)
179186
toolchain.asciidocExtensions.all {
180187
AsciidoctorExtension it -> extensionAttributes.putAll(it.attributeProvider)
181188
}
@@ -190,7 +197,6 @@ class AsciidoctorPublication implements Named {
190197
atm.baseDir = sources.baseDir.baseDirStrategy.flatMap { it.getBaseDir(sources.sourceDir) }
191198
atm.adjustBaseDirPerFile = sources.baseDir.baseDirStrategy.flatMap { it.adjustBaseDirPerFile }
192199
atm.fatalWarnings = sources.fatalWarnings
193-
194200
atm.externalSources = copyResources ? sources.externalSources : sources.externalSources.map {
195201
new DefaultProvidedExternalSources(
196202
it.externalSources.map { list ->

0 commit comments

Comments
 (0)