From 99ae8e0f3b876055212926bfe612b550220a56d5 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sat, 30 Dec 2023 12:47:52 +0100 Subject: [PATCH] [MSITE-1000] Introduce parser configuration parameter --- pom.xml | 13 ++- .../render/AbstractSiteRenderingMojo.java | 20 ++++- .../site/render/ParserConfiguration.java | 88 +++++++++++++++++++ .../ParserConfigurationRetrieverImpl.java | 39 ++++++++ .../site/render/ReportDocumentRenderer.java | 1 + 5 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/apache/maven/plugins/site/render/ParserConfiguration.java create mode 100644 src/main/java/org/apache/maven/plugins/site/render/ParserConfigurationRetrieverImpl.java diff --git a/pom.xml b/pom.xml index b9d095c4..66f40a1a 100644 --- a/pom.xml +++ b/pom.xml @@ -199,7 +199,7 @@ under the License. 9.4.53.v20231009 2.0.0-M8 - 2.0.0-M16 + 2.0.0-M17-SNAPSHOT 3.5.3 1.7.36 @@ -513,6 +513,17 @@ under the License. + + org.apache.maven.plugins + maven-plugin-plugin + + + https://maven.apache.org/doxia/doxia/doxia-core/apidocs/ + https://maven.apache.org/doxia/doxia-sitetools/doxia-site-renderer/apidocs/ + https://docs.oracle.com/javase/8/docs/api/ + + + org.apache.maven.plugins maven-plugin-report-plugin diff --git a/src/main/java/org/apache/maven/plugins/site/render/AbstractSiteRenderingMojo.java b/src/main/java/org/apache/maven/plugins/site/render/AbstractSiteRenderingMojo.java index 8906cbc8..151ee182 100644 --- a/src/main/java/org/apache/maven/plugins/site/render/AbstractSiteRenderingMojo.java +++ b/src/main/java/org/apache/maven/plugins/site/render/AbstractSiteRenderingMojo.java @@ -94,6 +94,24 @@ public abstract class AbstractSiteRenderingMojo extends AbstractSiteDescriptorMo @Parameter private Map attributes; + /** + * Parser configurations (per Doxia markup source file paths). + * Each entry has the following format + *

+ *

+     *   <patterns>
+     *     <pattern>glob:**/*.md</pattern><!-- is either glob or regex syntax -->
+     *   </patterns>
+     *   <emitComments>true</emitComments><!-- false by default -->
+     *   <emitAnchorsForIndexableEntries>false</emitAnchorsForIndexableEntries><!-- true by default -->
+     * 
+ * + * @since 4.0.0 + * @see java.nio.file.FileSystem#getPathMatcher(String) FileSystem.getPathMatcher(String) for the supported patterns + */ + @Parameter + private List parserConfigurations; + /** * Site renderer. */ @@ -329,7 +347,7 @@ protected SiteRenderingContext createSiteRenderingContext(Locale locale) context.setProcessedContentOutput(processedDir); } } - + context.setParserConfigurationRetriever(new ParserConfigurationRetrieverImpl(parserConfigurations)); return context; } diff --git a/src/main/java/org/apache/maven/plugins/site/render/ParserConfiguration.java b/src/main/java/org/apache/maven/plugins/site/render/ParserConfiguration.java new file mode 100644 index 00000000..f275b2b3 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/site/render/ParserConfiguration.java @@ -0,0 +1,88 @@ +/* + * 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.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Pattern; + +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 { + + /** + * Map of patterns and {@link PathMatcher}s. + * The key contains the pattern in the format described at {@link FileSystem#getPathMatcher(String)}, i.e. {@code :} + * where {@code patterns; + + /** + * @see {@link Parser#setEmitComments(boolean)} + */ + private boolean emitComments; + + /** + * @see {@link Parser#setEmitAnchorsForIndexableEntries(boolean)} + */ + private boolean emitAnchorsForIndexableEntries; + + public ParserConfiguration() { + patterns = new HashMap<>(); + } + + public void setEmitComments(boolean emitComments) { + this.emitComments = emitComments; + } + + public void setEmitAnchorsForIndexableEntries(boolean emitAnchorsForIndexableEntries) { + this.emitAnchorsForIndexableEntries = emitAnchorsForIndexableEntries; + } + + public void addPattern(String pattern) { + patterns.put(pattern, null); + } + + public boolean matches(Path filePath) { + for (Entry patternWithMatcher : patterns.entrySet()) { + if (patternWithMatcher.getValue() == null) { + patternWithMatcher.setValue(filePath.getFileSystem().getPathMatcher(patternWithMatcher.getKey())); + } + if (patternWithMatcher.getValue().matches(filePath)) { + return true; + } + } + return false; + } + + @Override + public void accept(Parser parser) { + parser.setEmitComments(emitComments); + // parser.setEmitAnchorsForIndexableEntries(emitAnchorsForIndexableEntries); + } +} diff --git a/src/main/java/org/apache/maven/plugins/site/render/ParserConfigurationRetrieverImpl.java b/src/main/java/org/apache/maven/plugins/site/render/ParserConfigurationRetrieverImpl.java new file mode 100644 index 00000000..a6ce7686 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/site/render/ParserConfigurationRetrieverImpl.java @@ -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 parserConfigurations; + + public ParserConfigurationRetrieverImpl(Collection parserConfigurations) { + this.parserConfigurations = parserConfigurations; + } + + @Override + public Optional apply(Path filePath) { + return parserConfigurations.stream().filter(c -> c.matches(filePath)).findFirst(); + } +} diff --git a/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java b/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java index c359269d..e30862e1 100644 --- a/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java +++ b/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java @@ -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());