Skip to content

Commit 141d137

Browse files
committed
Fix Antora version selection in PDF cross-references
1 parent ee42bdc commit 141d137

5 files changed

Lines changed: 214 additions & 24 deletions

File tree

‎commons/doc-maven-plugin/README.md‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -742,24 +742,7 @@ The following example shows the full configuration to use the 2.1.3 version.
742742
If you need to create your own shared content artifact,
743743
see the `forgerock-doc-common-content` project as an example.
744744

745-
* * *
746-
747-
The contents of this file are subject to the terms of the Common Development and
748-
Distribution License (the License). You may not use this file except in compliance with the
749-
License.
750-
751-
You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
752-
specific language governing permission and limitations under the License.
753-
754-
When distributing Covered Software, include this CDDL Header Notice in each file and include
755-
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
756-
Header, with the fields enclosed by brackets [] replaced by your own identifying
757-
information: "Portions copyright [year] [name of copyright owner]".
758-
759-
Copyright 2012-2015 ForgeRock AS.
760-
761-
762-
## Documentation site versions in AsciiDoc PDF links
745+
# Documentation site versions in AsciiDoc PDF links
763746

764747
The `asciidoc-to-pdf` goal converts cross-guide and cross-component xrefs to
765748
links on `siteUrl` (default `https://doc.openidentityplatform.org`). By default,
@@ -795,16 +778,22 @@ preserved. Only the PDF source copy is rewritten; Antora sources are unchanged.
795778

796779
Version selection, in order:
797780

798-
1. An explicit Antora version in the xref, such as
781+
1. An explicit Antora version in an xref that names a module, such as
799782
`xref:4.9@opendj:install-guide:index.adoc[Install]`.
800783
2. An entry for the target component in `componentDocVersions`.
801784
3. `docVersion`, only when the target is this project's component.
802785
4. The unversioned current documentation.
803786

804-
An empty map entry or an explicit empty version (`xref:@opendj::index.adoc[Current]`)
805-
selects the unversioned site. A component without an entry never inherits
787+
An empty map entry or Antora's versionless token `_` (`xref:_@opendj::index.adoc[Current]`)
788+
selects the unversioned site. The empty version form `xref:@opendj::index.adoc[Current]`
789+
is not a valid Antora resource ID and is not converted. A component without an entry never inherits
806790
another component's version.
807791

792+
Version-only resource IDs such as `xref:15.1@page.adoc[Older]` are left unchanged.
793+
To generate a site link in the PDF, name the target module explicitly, for example
794+
`xref:15.1@how-to:page.adoc[Older]`. This also avoids depending on the including
795+
page's module when the xref occurs in a shared partial.
796+
808797
When a release moves from current to archived documentation, first publish and
809798
verify its historical site paths, then rebuild its PDF from the immutable
810799
release sources using the corresponding `docVersion` and independently chosen
@@ -814,3 +803,20 @@ project's normal PDF build with that property. Replace the archived PDF with
814803
the rebuilt artifact. Previously distributed PDFs retain their original links;
815804
this converter cannot update them. Do not select a historical version path
816805
before that version is published on the site.
806+
807+
* * *
808+
809+
The contents of this file are subject to the terms of the Common Development and
810+
Distribution License (the License). You may not use this file except in compliance with the
811+
License.
812+
813+
You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
814+
specific language governing permission and limitations under the License.
815+
816+
When distributing Covered Software, include this CDDL Header Notice in each file and include
817+
the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
818+
Header, with the fields enclosed by brackets [] replaced by your own identifying
819+
information: "Portions copyright [year] [name of copyright owner]".
820+
821+
Copyright 2012-2015 ForgeRock AS.
822+
Portions copyright 2026 3A Systems, LLC.

‎commons/doc-maven-plugin/src/main/java/org/openidentityplatform/doc/maven/AsciidocToPdfMojo.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ static void preparePdfSource(File source, File target, String siteUrl, String co
188188
}
189189

190190
private static final Pattern XREF_PATTERN = Pattern.compile("xref:([^\\[\\s]+)\\[");
191-
private static final Pattern RESOURCE_ID_PATTERN = Pattern.compile("(?:([^:/@$]*)@)?(?:([^:/@$]+):)?([^:/@$]*):([^:@$]+)");
191+
private static final Pattern RESOURCE_ID_PATTERN =
192+
Pattern.compile("(?:([^:/@$]+)@)?(?:([^:/@$]+):)?([^:/@$]*):([^:@$]+)");
192193

193194
/**
194195
* Plain Asciidoctor knows neither the other guides nor the other Antora components, so it
@@ -199,7 +200,8 @@ static void preparePdfSource(File source, File target, String siteUrl, String co
199200
* <li>{@code xref:../module/page.adoc#anchor[text]}</li>
200201
* </ul>
201202
* Explicit {@code version@component:module:page.adoc} resource IDs override configured
202-
* component versions; an empty explicit version selects the unversioned site.
203+
* component versions; an explicit {@code _} version selects the unversioned site.
204+
* Resource IDs without a module coordinate, such as {@code version@page.adoc}, are left unchanged.
203205
* A relative xref into {@code currentModule} becomes a same-guide xref. Other xrefs are left as they are.
204206
*/
205207
static String convertXrefsForPdf(String adoc, String siteUrl, String component, String currentModule) {
@@ -250,6 +252,10 @@ private static String convertXrefForPdf(String target, String site, String compo
250252
return "xref:" + target;
251253
}
252254
explicitVersion = id.group(1);
255+
// Antora uses _ to explicitly select the versionless component version.
256+
if ("_".equals(explicitVersion)) {
257+
explicitVersion = "";
258+
}
253259
targetComponent = id.group(2) != null ? id.group(2) : component;
254260
module = id.group(3).isEmpty() ? "ROOT" : id.group(3);
255261
page = id.group(4);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* The contents of this file are subject to the terms of the Common Development and
3+
* Distribution License (the License). You may not use this file except in compliance with the
4+
* License.
5+
*
6+
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7+
* specific language governing permission and limitations under the License.
8+
*
9+
* When distributing Covered Software, include this CDDL Header Notice in each file and include
10+
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11+
* Header, with the fields enclosed by brackets [] replaced by your own identifying
12+
* information: "Portions copyright [year] [name of copyright owner]".
13+
*
14+
* Copyright 2026 3A Systems, LLC.
15+
*/
16+
17+
package org.openidentityplatform.doc.maven;
18+
19+
import org.apache.commons.io.FileUtils;
20+
import org.apache.maven.execution.MavenSession;
21+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
22+
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
23+
import org.apache.maven.project.MavenProject;
24+
import org.codehaus.plexus.util.xml.Xpp3Dom;
25+
26+
import java.io.File;
27+
import java.io.Reader;
28+
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.Files;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class AsciidocToPdfMojoConfigurationTest extends AbstractMojoTestCase {
34+
35+
private File buildDirectory;
36+
37+
@Override
38+
protected void setUp() throws Exception {
39+
super.setUp();
40+
buildDirectory = Files.createTempDirectory("pdf-configuration").toFile();
41+
}
42+
43+
@Override
44+
protected void tearDown() throws Exception {
45+
try {
46+
FileUtils.deleteDirectory(buildDirectory);
47+
} finally {
48+
super.tearDown();
49+
}
50+
}
51+
52+
public void testEmptyPomEntryOverridesProjectVersion() throws Exception {
53+
MavenProject project = readProject();
54+
AsciidocToPdfMojo mojo = (AsciidocToPdfMojo) lookupConfiguredMojo(project, "asciidoc-to-pdf");
55+
assertPreparedLinks(mojo, "");
56+
}
57+
58+
public void testPomVersionsReachPreparedPdfSource() throws Exception {
59+
MavenProject project = readProject();
60+
Xpp3Dom configuration = (Xpp3Dom) project.getBuildPlugins().get(0).getConfiguration();
61+
Xpp3Dom versions = configuration.getChild("componentDocVersions");
62+
versions.removeChild(versions.getChild("openam"));
63+
AsciidocToPdfMojo mojo = (AsciidocToPdfMojo) lookupConfiguredMojo(project, "asciidoc-to-pdf");
64+
assertPreparedLinks(mojo, "15.2/");
65+
}
66+
67+
public void testMavenUserPropertySelectsHistoricalVersion() throws Exception {
68+
MavenProject project = readProject();
69+
Xpp3Dom configuration = (Xpp3Dom) project.getBuildPlugins().get(0).getConfiguration();
70+
configuration.removeChild(configuration.getChild("docVersion"));
71+
Xpp3Dom versions = configuration.getChild("componentDocVersions");
72+
versions.removeChild(versions.getChild("openam"));
73+
MavenSession session = newMavenSession(project);
74+
session.getUserProperties().setProperty("docVersion", "15.2");
75+
AsciidocToPdfMojo mojo = (AsciidocToPdfMojo) lookupConfiguredMojo(session,
76+
newMojoExecution("asciidoc-to-pdf"));
77+
assertPreparedLinks(mojo, "15.2/");
78+
}
79+
80+
private MavenProject readProject() throws Exception {
81+
File pom = getTestFile("src/test/resources/asciidoc-to-pdf/pom.xml");
82+
try (Reader reader = Files.newBufferedReader(pom.toPath(), StandardCharsets.UTF_8)) {
83+
MavenProject project = new MavenProject(new MavenXpp3Reader().read(reader));
84+
project.setFile(pom);
85+
project.getBuild().setDirectory(buildDirectory.getAbsolutePath());
86+
return project;
87+
}
88+
}
89+
90+
private void assertPreparedLinks(AsciidocToPdfMojo mojo, String versionPrefix) throws Exception {
91+
FileUtils.writeStringToFile(new File(mojo.getAsciidocBuildSourceDirectory(), "how-to/page.adoc"),
92+
"xref:reference:page.adoc#a[A] xref:opendj::index.adoc[B]", StandardCharsets.UTF_8);
93+
mojo.execute();
94+
assertThat(new File(mojo.getPdfSourceDirectory(), "how-to/page.adoc")).hasContent(
95+
"link:https://doc.openidentityplatform.org/openam/" + versionPrefix + "reference/page.html#a[A] "
96+
+ "link:https://doc.openidentityplatform.org/opendj/4.10/index.html[B]");
97+
}
98+
}

‎commons/doc-maven-plugin/src/test/java/org/openidentityplatform/doc/maven/AsciidocToPdfMojoTest.java‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,51 @@ public void testExplicitVersionOverridesConfiguredComponentVersion() {
8484
versions.put("openam", "15.1");
8585
assertThat(AsciidocToPdfMojo.convertXrefsForPdf(
8686
"xref:4.7@opendj:ROOT:index.adoc[Older] xref:15.0@reference:page.adoc#a[Reference] "
87-
+ "xref:reference:page.adoc[Configured] xref:@opendj::index.adoc[Current]",
87+
+ "xref:reference:page.adoc[Configured] xref:_@opendj::index.adoc[Current]",
8888
SITE, "openam", "how-to", "15.2", versions)).isEqualTo(
8989
"link:" + SITE + "/opendj/4.7/index.html[Older] "
9090
+ "link:" + SITE + "/openam/15.0/reference/page.html#a[Reference] "
9191
+ "link:" + SITE + "/openam/15.1/reference/page.html[Configured] "
9292
+ "link:" + SITE + "/opendj/index.html[Current]");
9393
}
9494

95+
@Test
96+
public void testEmptyExplicitVersionIsNotAnAntoraResourceId() {
97+
String adoc = "xref:@opendj::index.adoc[Current] xref:@reference:page.adoc[A] xref:@page.adoc[B]";
98+
assertThat(AsciidocToPdfMojo.convertXrefsForPdf(adoc, SITE, "openam", "how-to", "15.2",
99+
Collections.emptyMap())).isEqualTo(adoc);
100+
}
101+
102+
@Test
103+
public void testExplicitVersionWithoutModuleIsUnchanged() {
104+
String adoc = "xref:15.1@page.adoc#a[Older] xref:_@page.adoc#b[Current]";
105+
assertThat(AsciidocToPdfMojo.convertXrefsForPdf(adoc, SITE, "openam", "how-to", "15.2",
106+
Collections.singletonMap("openam", "15.0"))).isEqualTo(adoc);
107+
}
108+
109+
@Test
110+
public void testEmptyComponentEntriesOverrideProjectVersion() {
111+
Map<String, String> versions = new HashMap<>();
112+
for (String empty : new String[] {null, ""}) {
113+
versions.put("openam", empty);
114+
versions.put("opendj", empty);
115+
assertThat(AsciidocToPdfMojo.convertXrefsForPdf(
116+
"xref:reference:page.adoc[A] xref:opendj::index.adoc[B]",
117+
SITE, "openam", "how-to", "15.2", versions)).isEqualTo(
118+
"link:" + SITE + "/openam/reference/page.html[A] "
119+
+ "link:" + SITE + "/opendj/index.html[B]");
120+
}
121+
}
122+
123+
@Test
124+
public void testConfiguredVersionsAreLiteralSitePaths() {
125+
assertThat(AsciidocToPdfMojo.convertXrefsForPdf(
126+
"xref:reference:page.adoc[A] xref:opendj::index.adoc[B]",
127+
SITE, "openam", "how-to", "_", Collections.singletonMap("opendj", "_"))).isEqualTo(
128+
"link:" + SITE + "/openam/_/reference/page.html[A] "
129+
+ "link:" + SITE + "/opendj/_/index.html[B]");
130+
}
131+
95132
@Test
96133
public void testHistoricalSameGuideLinksStayInternal() {
97134
String adoc = "xref:../how-to/page.adoc#a[A] xref:#local[Local] xref:page.adoc[Page]";
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
* The contents of this file are subject to the terms of the Common Development and
4+
* Distribution License (the License). You may not use this file except in compliance with the
5+
* License.
6+
*
7+
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
8+
* specific language governing permission and limitations under the License.
9+
*
10+
* When distributing Covered Software, include this CDDL Header Notice in each file and include
11+
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
12+
* Header, with the fields enclosed by brackets [] replaced by your own identifying
13+
* information: "Portions copyright [year] [name of copyright owner]".
14+
*
15+
* Copyright 2026 3A Systems, LLC.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0">
19+
<modelVersion>4.0.0</modelVersion>
20+
<groupId>org.openidentityplatform.commons</groupId>
21+
<artifactId>pdf-configuration-test</artifactId>
22+
<version>1.0.0</version>
23+
<packaging>pom</packaging>
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.openidentityplatform.commons</groupId>
28+
<artifactId>doc-maven-plugin</artifactId>
29+
<configuration>
30+
<projectName>OpenAM</projectName>
31+
<docVersion>15.2</docVersion>
32+
<componentDocVersions>
33+
<opendj>4.10</opendj>
34+
<openam/>
35+
</componentDocVersions>
36+
<documents>
37+
<document>another-guide</document>
38+
</documents>
39+
</configuration>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
</project>

0 commit comments

Comments
 (0)