-
Notifications
You must be signed in to change notification settings - Fork 47
[MSITE-1000] Introduce parser configuration parameter #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kwin
wants to merge
2
commits into
master
Choose a base branch
from
feature/configure-parser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/org/apache/maven/plugins/site/render/ParserConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/apache/maven/plugins/site/render/ParserConfigurationRetrieverImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/org/apache/maven/plugins/site/render/ParserConfigurationRetrieverImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"))); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.