Skip to content

Commit

Permalink
[MSITE-1000] Introduce parser configuration parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Jan 4, 2024
1 parent d78b8da commit 8a87442
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ under the License.
<!-- for dependencies -->
<jettyVersion>9.4.53.v20231009</jettyVersion>
<doxiaVersion>2.0.0-M8</doxiaVersion>
<doxiaSitetoolsVersion>2.0.0-M16</doxiaSitetoolsVersion>
<doxiaSitetoolsVersion>2.0.0-M17-SNAPSHOT</doxiaSitetoolsVersion>
<wagonVersion>3.5.3</wagonVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<!-- for ITs -->
Expand Down Expand Up @@ -513,6 +513,17 @@ under the License.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<configuration>
<externalJavadocBaseUrls>
<externalJavadocBaseUrl>https://maven.apache.org/doxia/doxia/doxia-core/apidocs/</externalJavadocBaseUrl>
<externalJavadocBaseUrl>https://maven.apache.org/doxia/doxia-sitetools/doxia-site-renderer/apidocs/</externalJavadocBaseUrl>
<externalJavadocBaseUrl>https://docs.oracle.com/javase/8/docs/api/</externalJavadocBaseUrl>
</externalJavadocBaseUrls>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-report-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMo
@Parameter
private Map<String, Object> attributes;

/**
* Parser configurations (per Doxia markup source file paths).
* Each entry has the following format
* <p/>
* <pre>
* &lt;patterns&gt;
* &lt;pattern&gt;glob:**&#47;*.md&lt;/pattern&gt;&lt;!-- is either glob or regex syntax --&gt;
* &lt;/patterns&gt;
* &lt;emitComments&gt;true&lt;/emitComments&gt;&lt;!-- false by default --&gt;
* &lt;emitAnchorsForIndexableEntries&gt;false&lt;/emitAnchorsForIndexableEntries&gt;&lt;!-- true by default --&gt;
* </pre>
*
* @since 4.0.0
* @see java.nio.file.FileSystem#getPathMatcher(String) FileSystem.getPathMatcher(String) for the supported patterns
*/
@Parameter
private List<ParserConfiguration> parserConfigurations;

/**
* Site renderer.
*/
Expand Down Expand Up @@ -329,7 +347,7 @@ protected SiteRenderingContext createSiteRenderingContext(Locale locale)
context.setProcessedContentOutput(processedDir);
}
}

context.setParserConfigurationRetriever(new ParserConfigurationRetrieverImpl(parserConfigurations));
return context;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.site.render;

import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.maven.doxia.parser.Parser;

/** Configuration for a Doxia {@link Parser} (bound to a specific markup source path pattern) **/
public class ParserConfiguration implements org.apache.maven.doxia.siterenderer.ParserConfiguration {

/**
* List of patterns in the format described at {@link FileSystem#getPathMatcher(String)}, i.e. {@code <syntax>:<pattern>}
* where {@code <syntax} is either {@code glob} or {@code regex}.
* If one of the patterns matches the file being parsed this configuration is applied.
* @see FileSystem#getPathMatcher(String)
* @see Pattern
*/
private final List<String> patterns;

/**
* List of {@link PathMatcher}s for all of the {@link #patterns}. Lazily populated via {@link FileSystem#getPathMatcher(String)}.
*/
private List<PathMatcher> matchers;

private boolean emitComments;

private boolean emitAnchorsForIndexableEntries;

public ParserConfiguration() {
patterns = new LinkedList<>();
matchers = null;
}

/**
* Switches the feature {@link Parser#setEmitComments(boolean)} either on or off.
* Default is off.
*
* @param emitComments {@code true} to switch it on, otherwise leave it off
* @see Parser#setEmitComments(boolean)
*/
public void setEmitComments(boolean emitComments) {
this.emitComments = emitComments;
}

/**
* Switches the feature {@link Parser#setEmitAnchorsForIndexableEntries(boolean)} either on or off.
* Default is on.
*
* @param emitAnchorsForIndexableEntries {@code true} to switch it on, otherwise leave it off
* @see Parser#setEmitAnchorsForIndexableEntries(boolean)
*/
public void setEmitAnchorsForIndexableEntries(boolean emitAnchorsForIndexableEntries) {
this.emitAnchorsForIndexableEntries = emitAnchorsForIndexableEntries;
}

/**
* A pattern in the format described at {@link FileSystem#getPathMatcher(String)}, i.e. {@code <syntax>:<pattern>}
* where {@code <syntax} is either {@code glob} or {@code regex}.
* If one of the patterns matches the file being parsed this configuration is applied.
* @see FileSystem#getPathMatcher(String)
* @see Pattern
*/
public void addPattern(String pattern) {
patterns.add(pattern);
}

/**
* Returns {@code true} the given file path matches one of the {@link #patterns} given via {@link #addPattern(String)}
* @param filePath the file path to check
* @return {@code true} if the given file path matches at least one of the patterns, {@code false} otherwise.
* @throws IllegalArgumentException
* If one of the patterns does not comply with the form: {@code syntax:pattern}
* @throws java.util.regex.PatternSyntaxException
* If one of the regex patterns is invalid
* @throws UnsupportedOperationException
* If one of the patterns syntax prefix is not known to the implementation
* @see FileSystem#getPathMatcher(String)
*/
public boolean matches(Path filePath) {
if (matchers == null) {
// lazily populate all matchers
matchers = patterns.stream()
.map(p -> filePath.getFileSystem().getPathMatcher(p))
.collect(Collectors.toList());
}
return matchers.stream().anyMatch(m -> m.matches(filePath));
}

@Override
public void accept(Parser parser) {
parser.setEmitComments(emitComments);
// parser.setEmitAnchorsForIndexableEntries(emitAnchorsForIndexableEntries);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.site.render;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;

import org.apache.maven.doxia.siterenderer.ParserConfigurationRetriever;

public class ParserConfigurationRetrieverImpl implements ParserConfigurationRetriever {

private final Collection<ParserConfiguration> parserConfigurations;

public ParserConfigurationRetrieverImpl(Collection<ParserConfiguration> parserConfigurations) {
this.parserConfigurations = parserConfigurations;
}

@Override
public Optional<ParserConfiguration> apply(Path filePath) {
return parserConfigurations.stream().filter(c -> c.matches(filePath)).findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public Sink createSink(File outputDirectory, String outputName) {
docRenderingContext.getBasedirRelativePath(),
document,
docRenderingContext.getParserId(),
docRenderingContext.getParserConfiguration(), // TODO: use another config?
docRenderingContext.getExtension(),
docRenderingContext.isEditable(),
docRenderingContext.getGenerator());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.site.render;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class ParserConfigurationRetrieverImplTest {

@Test
public void testEmptyConfigurations() {
ParserConfiguration config1 = new ParserConfiguration();
ParserConfiguration config2 = new ParserConfiguration();
assertEquals(
Optional.empty(),
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
}

@Test
public void testConfigurationWithInvalidPattern() {
ParserConfiguration config1 = new ParserConfiguration();
config1.addPattern("invalidprefix:*");
ParserConfigurationRetrieverImpl parserConfigurationRetrieverImpl =
new ParserConfigurationRetrieverImpl(Arrays.asList(config1));
assertThrows(RuntimeException.class, () -> {
parserConfigurationRetrieverImpl.apply(Paths.get("some", "file"));
});
}

@Test
public void testNonMatchingConfigurations() {
ParserConfiguration config1 = new ParserConfiguration();
config1.addPattern("glob:**/*.md");
ParserConfiguration config2 = new ParserConfiguration();
config2.addPattern("regex:.*\\.apt");
assertEquals(
Optional.empty(),
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
}

@Test
public void testNonOverlappingConfigurations() {
ParserConfiguration config1 = new ParserConfiguration();
config1.addPattern("regex:.*\\.apt");
ParserConfiguration config2 = new ParserConfiguration();
config2.addPattern("glob:**/*");
assertEquals(
Optional.of(config2),
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
}

@Test
public void testOverlappingConfigurations() {
ParserConfiguration config1 = new ParserConfiguration();
config1.addPattern("glob:**/*");
ParserConfiguration config2 = new ParserConfiguration();
config2.addPattern("regex:.*");
assertEquals(
Optional.of(config1),
new ParserConfigurationRetrieverImpl(Arrays.asList(config1, config2)).apply(Paths.get("some", "file")));
}
}

0 comments on commit 8a87442

Please sign in to comment.