Skip to content

Commit 8a87442

Browse files
committed
[MSITE-1000] Introduce parser configuration parameter
1 parent d78b8da commit 8a87442

6 files changed

Lines changed: 272 additions & 2 deletions

File tree

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ under the License.
199199
<!-- for dependencies -->
200200
<jettyVersion>9.4.53.v20231009</jettyVersion>
201201
<doxiaVersion>2.0.0-M8</doxiaVersion>
202-
<doxiaSitetoolsVersion>2.0.0-M16</doxiaSitetoolsVersion>
202+
<doxiaSitetoolsVersion>2.0.0-M17-SNAPSHOT</doxiaSitetoolsVersion>
203203
<wagonVersion>3.5.3</wagonVersion>
204204
<slf4jVersion>1.7.36</slf4jVersion>
205205
<!-- for ITs -->
@@ -513,6 +513,17 @@ under the License.
513513
<build>
514514
<pluginManagement>
515515
<plugins>
516+
<plugin>
517+
<groupId>org.apache.maven.plugins</groupId>
518+
<artifactId>maven-plugin-plugin</artifactId>
519+
<configuration>
520+
<externalJavadocBaseUrls>
521+
<externalJavadocBaseUrl>https://maven.apache.org/doxia/doxia/doxia-core/apidocs/</externalJavadocBaseUrl>
522+
<externalJavadocBaseUrl>https://maven.apache.org/doxia/doxia-sitetools/doxia-site-renderer/apidocs/</externalJavadocBaseUrl>
523+
<externalJavadocBaseUrl>https://docs.oracle.com/javase/8/docs/api/</externalJavadocBaseUrl>
524+
</externalJavadocBaseUrls>
525+
</configuration>
526+
</plugin>
516527
<plugin>
517528
<groupId>org.apache.maven.plugins</groupId>
518529
<artifactId>maven-plugin-report-plugin</artifactId>

src/main/java/org/apache/maven/plugins/site/render/AbstractSiteRenderingMojo.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMo
9494
@Parameter
9595
private Map<String, Object> attributes;
9696

97+
/**
98+
* Parser configurations (per Doxia markup source file paths).
99+
* Each entry has the following format
100+
* <p/>
101+
* <pre>
102+
* &lt;patterns&gt;
103+
* &lt;pattern&gt;glob:**&#47;*.md&lt;/pattern&gt;&lt;!-- is either glob or regex syntax --&gt;
104+
* &lt;/patterns&gt;
105+
* &lt;emitComments&gt;true&lt;/emitComments&gt;&lt;!-- false by default --&gt;
106+
* &lt;emitAnchorsForIndexableEntries&gt;false&lt;/emitAnchorsForIndexableEntries&gt;&lt;!-- true by default --&gt;
107+
* </pre>
108+
*
109+
* @since 4.0.0
110+
* @see java.nio.file.FileSystem#getPathMatcher(String) FileSystem.getPathMatcher(String) for the supported patterns
111+
*/
112+
@Parameter
113+
private List<ParserConfiguration> parserConfigurations;
114+
97115
/**
98116
* Site renderer.
99117
*/
@@ -329,7 +347,7 @@ protected SiteRenderingContext createSiteRenderingContext(Locale locale)
329347
context.setProcessedContentOutput(processedDir);
330348
}
331349
}
332-
350+
context.setParserConfigurationRetriever(new ParserConfigurationRetrieverImpl(parserConfigurations));
333351
return context;
334352
}
335353

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.site.render;
20+
21+
import java.nio.file.FileSystem;
22+
import java.nio.file.Path;
23+
import java.nio.file.PathMatcher;
24+
import java.util.LinkedList;
25+
import java.util.List;
26+
import java.util.regex.Pattern;
27+
import java.util.stream.Collectors;
28+
29+
import org.apache.maven.doxia.parser.Parser;
30+
31+
/** Configuration for a Doxia {@link Parser} (bound to a specific markup source path pattern) **/
32+
public class ParserConfiguration implements org.apache.maven.doxia.siterenderer.ParserConfiguration {
33+
34+
/**
35+
* List of patterns in the format described at {@link FileSystem#getPathMatcher(String)}, i.e. {@code <syntax>:<pattern>}
36+
* where {@code <syntax} is either {@code glob} or {@code regex}.
37+
* If one of the patterns matches the file being parsed this configuration is applied.
38+
* @see FileSystem#getPathMatcher(String)
39+
* @see Pattern
40+
*/
41+
private final List<String> patterns;
42+
43+
/**
44+
* List of {@link PathMatcher}s for all of the {@link #patterns}. Lazily populated via {@link FileSystem#getPathMatcher(String)}.
45+
*/
46+
private List<PathMatcher> matchers;
47+
48+
private boolean emitComments;
49+
50+
private boolean emitAnchorsForIndexableEntries;
51+
52+
public ParserConfiguration() {
53+
patterns = new LinkedList<>();
54+
matchers = null;
55+
}
56+
57+
/**
58+
* Switches the feature {@link Parser#setEmitComments(boolean)} either on or off.
59+
* Default is off.
60+
*
61+
* @param emitComments {@code true} to switch it on, otherwise leave it off
62+
* @see Parser#setEmitComments(boolean)
63+
*/
64+
public void setEmitComments(boolean emitComments) {
65+
this.emitComments = emitComments;
66+
}
67+
68+
/**
69+
* Switches the feature {@link Parser#setEmitAnchorsForIndexableEntries(boolean)} either on or off.
70+
* Default is on.
71+
*
72+
* @param emitAnchorsForIndexableEntries {@code true} to switch it on, otherwise leave it off
73+
* @see Parser#setEmitAnchorsForIndexableEntries(boolean)
74+
*/
75+
public void setEmitAnchorsForIndexableEntries(boolean emitAnchorsForIndexableEntries) {
76+
this.emitAnchorsForIndexableEntries = emitAnchorsForIndexableEntries;
77+
}
78+
79+
/**
80+
* A pattern in the format described at {@link FileSystem#getPathMatcher(String)}, i.e. {@code <syntax>:<pattern>}
81+
* where {@code <syntax} is either {@code glob} or {@code regex}.
82+
* If one of the patterns matches the file being parsed this configuration is applied.
83+
* @see FileSystem#getPathMatcher(String)
84+
* @see Pattern
85+
*/
86+
public void addPattern(String pattern) {
87+
patterns.add(pattern);
88+
}
89+
90+
/**
91+
* Returns {@code true} the given file path matches one of the {@link #patterns} given via {@link #addPattern(String)}
92+
* @param filePath the file path to check
93+
* @return {@code true} if the given file path matches at least one of the patterns, {@code false} otherwise.
94+
* @throws IllegalArgumentException
95+
* If one of the patterns does not comply with the form: {@code syntax:pattern}
96+
* @throws java.util.regex.PatternSyntaxException
97+
* If one of the regex patterns is invalid
98+
* @throws UnsupportedOperationException
99+
* If one of the patterns syntax prefix is not known to the implementation
100+
* @see FileSystem#getPathMatcher(String)
101+
*/
102+
public boolean matches(Path filePath) {
103+
if (matchers == null) {
104+
// lazily populate all matchers
105+
matchers = patterns.stream()
106+
.map(p -> filePath.getFileSystem().getPathMatcher(p))
107+
.collect(Collectors.toList());
108+
}
109+
return matchers.stream().anyMatch(m -> m.matches(filePath));
110+
}
111+
112+
@Override
113+
public void accept(Parser parser) {
114+
parser.setEmitComments(emitComments);
115+
// parser.setEmitAnchorsForIndexableEntries(emitAnchorsForIndexableEntries);
116+
}
117+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.site.render;
20+
21+
import java.nio.file.Path;
22+
import java.util.Collection;
23+
import java.util.Optional;
24+
25+
import org.apache.maven.doxia.siterenderer.ParserConfigurationRetriever;
26+
27+
public class ParserConfigurationRetrieverImpl implements ParserConfigurationRetriever {
28+
29+
private final Collection<ParserConfiguration> parserConfigurations;
30+
31+
public ParserConfigurationRetrieverImpl(Collection<ParserConfiguration> parserConfigurations) {
32+
this.parserConfigurations = parserConfigurations;
33+
}
34+
35+
@Override
36+
public Optional<ParserConfiguration> apply(Path filePath) {
37+
return parserConfigurations.stream().filter(c -> c.matches(filePath)).findFirst();
38+
}
39+
}

src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public Sink createSink(File outputDirectory, String outputName) {
131131
docRenderingContext.getBasedirRelativePath(),
132132
document,
133133
docRenderingContext.getParserId(),
134+
docRenderingContext.getParserConfiguration(), // TODO: use another config?
134135
docRenderingContext.getExtension(),
135136
docRenderingContext.isEditable(),
136137
docRenderingContext.getGenerator());
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.site.render;
20+
21+
import java.nio.file.Paths;
22+
import java.util.Arrays;
23+
import java.util.Optional;
24+
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertThrows;
29+
30+
public class ParserConfigurationRetrieverImplTest {
31+
32+
@Test
33+
public void testEmptyConfigurations() {
34+
ParserConfiguration config1 = new ParserConfiguration();
35+
ParserConfiguration config2 = new ParserConfiguration();
36+
assertEquals(
37+
Optional.empty(),
38+
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
39+
}
40+
41+
@Test
42+
public void testConfigurationWithInvalidPattern() {
43+
ParserConfiguration config1 = new ParserConfiguration();
44+
config1.addPattern("invalidprefix:*");
45+
ParserConfigurationRetrieverImpl parserConfigurationRetrieverImpl =
46+
new ParserConfigurationRetrieverImpl(Arrays.asList(config1));
47+
assertThrows(RuntimeException.class, () -> {
48+
parserConfigurationRetrieverImpl.apply(Paths.get("some", "file"));
49+
});
50+
}
51+
52+
@Test
53+
public void testNonMatchingConfigurations() {
54+
ParserConfiguration config1 = new ParserConfiguration();
55+
config1.addPattern("glob:**/*.md");
56+
ParserConfiguration config2 = new ParserConfiguration();
57+
config2.addPattern("regex:.*\\.apt");
58+
assertEquals(
59+
Optional.empty(),
60+
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
61+
}
62+
63+
@Test
64+
public void testNonOverlappingConfigurations() {
65+
ParserConfiguration config1 = new ParserConfiguration();
66+
config1.addPattern("regex:.*\\.apt");
67+
ParserConfiguration config2 = new ParserConfiguration();
68+
config2.addPattern("glob:**/*");
69+
assertEquals(
70+
Optional.of(config2),
71+
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
72+
}
73+
74+
@Test
75+
public void testOverlappingConfigurations() {
76+
ParserConfiguration config1 = new ParserConfiguration();
77+
config1.addPattern("glob:**/*");
78+
ParserConfiguration config2 = new ParserConfiguration();
79+
config2.addPattern("regex:.*");
80+
assertEquals(
81+
Optional.of(config1),
82+
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
83+
}
84+
}

0 commit comments

Comments
 (0)