-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1011 from korthout/flexmark-java
Add native markdown formatting for maven
- Loading branch information
Showing
19 changed files
with
482 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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
91 changes: 91 additions & 0 deletions
91
lib/src/flexmark/java/com/diffplug/spotless/glue/markdown/FlexmarkFormatterFunc.java
This file contains 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,91 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed 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 com.diffplug.spotless.glue.markdown; | ||
|
||
import com.vladsch.flexmark.formatter.Formatter; | ||
import com.vladsch.flexmark.parser.Parser; | ||
import com.vladsch.flexmark.parser.ParserEmulationProfile; | ||
import com.vladsch.flexmark.parser.PegdownExtensions; | ||
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter; | ||
import com.vladsch.flexmark.util.ast.Document; | ||
import com.vladsch.flexmark.util.data.MutableDataHolder; | ||
import com.vladsch.flexmark.util.data.MutableDataSet; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
/** | ||
* The formatter function for <a href="https://github.com/vsch/flexmark-java">flexmark-java</a>. | ||
*/ | ||
public class FlexmarkFormatterFunc implements FormatterFunc { | ||
|
||
/** | ||
* The emulation profile is used by both the parser and the formatter and generally determines the markdown flavor. | ||
* COMMONMARK is the default defined by flexmark-java. | ||
*/ | ||
private static final String DEFAULT_EMULATION_PROFILE = "COMMONMARK"; | ||
|
||
private final Parser parser; | ||
private final Formatter formatter; | ||
|
||
public FlexmarkFormatterFunc() { | ||
// flexmark-java has a separate parser and renderer (formatter) | ||
// this is build from the example in https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter | ||
|
||
// The emulation profile generally determines the markdown flavor. We use the same one for both the parser and | ||
// the formatter, to make sure this formatter func is idempotent. | ||
final ParserEmulationProfile emulationProfile = ParserEmulationProfile.valueOf(DEFAULT_EMULATION_PROFILE); | ||
|
||
final MutableDataHolder parserOptions = createParserOptions(emulationProfile); | ||
final MutableDataHolder formatterOptions = createFormatterOptions(parserOptions, emulationProfile); | ||
|
||
parser = Parser.builder(parserOptions).build(); | ||
formatter = Formatter.builder(formatterOptions).build(); | ||
} | ||
|
||
/** | ||
* Creates the parser options. | ||
* See: https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter#options | ||
* | ||
* @param emulationProfile the emulation profile (or flavor of markdown) the parser should use | ||
* @return the created parser options | ||
*/ | ||
private static MutableDataHolder createParserOptions(ParserEmulationProfile emulationProfile) { | ||
final MutableDataHolder parserOptions = PegdownOptionsAdapter.flexmarkOptions(PegdownExtensions.ALL).toMutable(); | ||
parserOptions.set(Parser.PARSER_EMULATION_PROFILE, emulationProfile); | ||
return parserOptions; | ||
} | ||
|
||
/** | ||
* Creates the formatter options, copies the parser extensions and changes defaults that make sense for a formatter. | ||
* See: https://github.com/vsch/flexmark-java/wiki/Markdown-Formatter#options | ||
* | ||
* @param parserOptions the options used for the parser | ||
* @param emulationProfile the emulation profile (or flavor of markdown) the formatter should use | ||
* @return the created formatter options | ||
*/ | ||
private static MutableDataHolder createFormatterOptions(MutableDataHolder parserOptions, ParserEmulationProfile emulationProfile) { | ||
final MutableDataHolder formatterOptions = new MutableDataSet(); | ||
formatterOptions.set(Parser.EXTENSIONS, Parser.EXTENSIONS.get(parserOptions)); | ||
formatterOptions.set(Formatter.FORMATTER_EMULATION_PROFILE, emulationProfile); | ||
return formatterOptions; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
final Document parsedMarkdown = parser.parse(input); | ||
return formatter.render(parsedMarkdown); | ||
} | ||
} |
This file contains 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 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 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
72 changes: 72 additions & 0 deletions
72
lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java
This file contains 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,72 @@ | ||
/* | ||
* Copyright 2016-2021 DiffPlug | ||
* | ||
* Licensed 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 com.diffplug.spotless.markdown; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
/** A step for <a href="https://github.com/vsch/flexmark-java">flexmark-java</a>. */ | ||
public class FlexmarkStep { | ||
// prevent direct instantiation | ||
private FlexmarkStep() {} | ||
|
||
private static final String DEFAULT_VERSION = "0.62.2"; | ||
private static final String NAME = "flexmark-java"; | ||
private static final String MAVEN_COORDINATE = "com.vladsch.flexmark:flexmark-all:"; | ||
|
||
/** Creates a formatter step for the default version. */ | ||
public static FormatterStep create(Provisioner provisioner) { | ||
return create(defaultVersion(), provisioner); | ||
} | ||
|
||
/** Creates a formatter step for the given version. */ | ||
public static FormatterStep create(String version, Provisioner provisioner) { | ||
Objects.requireNonNull(version, "version"); | ||
Objects.requireNonNull(provisioner, "provisioner"); | ||
return FormatterStep.createLazy(NAME, | ||
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner)), | ||
State::createFormat); | ||
} | ||
|
||
public static String defaultVersion() { | ||
return DEFAULT_VERSION; | ||
} | ||
|
||
private static class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** The jar that contains the formatter. */ | ||
final JarState jarState; | ||
|
||
State(JarState jarState) { | ||
this.jarState = jarState; | ||
} | ||
|
||
FormatterFunc createFormat() throws Exception { | ||
final ClassLoader classLoader = jarState.getClassLoader(); | ||
final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.markdown.FlexmarkFormatterFunc"); | ||
final Constructor<?> constructor = formatterFunc.getConstructor(); | ||
return (FormatterFunc) constructor.newInstance(); | ||
} | ||
|
||
} | ||
} |
This file contains 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 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 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 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
35 changes: 35 additions & 0 deletions
35
plugin-maven/src/main/java/com/diffplug/spotless/maven/markdown/Flexmark.java
This file contains 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,35 @@ | ||
/* | ||
* Copyright 2021 DiffPlug | ||
* | ||
* Licensed 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 com.diffplug.spotless.maven.markdown; | ||
|
||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.markdown.FlexmarkStep; | ||
import com.diffplug.spotless.maven.FormatterStepConfig; | ||
import com.diffplug.spotless.maven.FormatterStepFactory; | ||
|
||
public class Flexmark implements FormatterStepFactory { | ||
|
||
@Parameter | ||
private String version; | ||
|
||
@Override | ||
public FormatterStep newFormatterStep(FormatterStepConfig config) { | ||
String version = this.version != null ? this.version : FlexmarkStep.defaultVersion(); | ||
return FlexmarkStep.create(version, config.getProvisioner()); | ||
} | ||
} |
Oops, something went wrong.