diff --git a/lib/build.gradle b/lib/build.gradle index 930510995e..d8135d67c5 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -89,6 +89,7 @@ dependencies { compatDiktat2Dot0Dot0CompileOnly "com.saveourtool.diktat:diktat-runner:2.0.0" // flexmark flexmarkCompileOnly 'com.vladsch.flexmark:flexmark-all:0.64.8' + implementation 'com.vladsch.flexmark:flexmark-util-data:0.64.8' // gherkin gherkinCompileOnly 'io.cucumber:gherkin-utils:9.0.0' gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.16' diff --git a/lib/src/flexmark/java/com/diffplug/spotless/glue/markdown/FlexmarkFormatterFunc.java b/lib/src/flexmark/java/com/diffplug/spotless/glue/markdown/FlexmarkFormatterFunc.java index dccb4fee7c..10c7be260a 100644 --- a/lib/src/flexmark/java/com/diffplug/spotless/glue/markdown/FlexmarkFormatterFunc.java +++ b/lib/src/flexmark/java/com/diffplug/spotless/glue/markdown/FlexmarkFormatterFunc.java @@ -40,7 +40,7 @@ public class FlexmarkFormatterFunc implements FormatterFunc { private final Parser parser; private final Formatter formatter; - public FlexmarkFormatterFunc() { + public FlexmarkFormatterFunc(final MutableDataHolder customFormatterOptions) { // 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 @@ -52,7 +52,7 @@ public FlexmarkFormatterFunc() { final MutableDataHolder formatterOptions = createFormatterOptions(parserOptions, emulationProfile); parser = Parser.builder(parserOptions).build(); - formatter = Formatter.builder(formatterOptions).build(); + formatter = Formatter.builder(MutableDataSet.merge(formatterOptions, customFormatterOptions)).build(); } /** @@ -84,7 +84,7 @@ private static MutableDataHolder createFormatterOptions(MutableDataHolder parser } @Override - public String apply(String input) throws Exception { + public String apply(String input) { final Document parsedMarkdown = parser.parse(input); return formatter.render(parsedMarkdown); } diff --git a/lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java b/lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java index 359e5b5de0..5e0193bf39 100644 --- a/lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java +++ b/lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java @@ -15,10 +15,17 @@ */ package com.diffplug.spotless.markdown; +import static com.diffplug.spotless.JarState.Promised; +import static com.diffplug.spotless.JarState.from; +import static com.diffplug.spotless.JarState.promise; + import java.io.Serializable; import java.lang.reflect.Constructor; import java.util.Objects; +import com.vladsch.flexmark.util.data.MutableDataHolder; +import com.vladsch.flexmark.util.data.MutableDataSet; + import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.JarState; @@ -31,33 +38,75 @@ public class FlexmarkStep implements Serializable { private static final String MAVEN_COORDINATE = "com.vladsch.flexmark:flexmark-all:"; public static final String NAME = "flexmark-java"; - private final JarState.Promised jarState; + private final Promised jarState; + private final MutableDataSet options; - private FlexmarkStep(JarState.Promised jarState) { + private FlexmarkStep( + final Promised jarState, + final MutableDataSet options + ) { this.jarState = jarState; + this.options = options; + } + + /** Creates a formatter step for the custom version. */ + public static FormatterStep create( + final String version, + final Provisioner provisioner + ) { + return FlexmarkStep.create( + version, + provisioner, + new MutableDataSet() + ); } /** Creates a formatter step for the default version. */ - public static FormatterStep create(Provisioner provisioner) { - return create(defaultVersion(), provisioner); + public static FormatterStep create(final Provisioner provisioner) { + return FlexmarkStep.create( + FlexmarkStep.defaultVersion(), + provisioner, + new MutableDataSet() + ); + } + + /** Creates a formatter step for the default version with custom options. */ + public static FormatterStep create( + final Provisioner provisioner, + final MutableDataSet options + ) { + return FlexmarkStep.create( + FlexmarkStep.defaultVersion(), + provisioner, + options + ); } /** Creates a formatter step for the given version. */ - public static FormatterStep create(String version, Provisioner provisioner) { + public static FormatterStep create( + final String version, + final Provisioner provisioner, + final MutableDataSet options + ) { Objects.requireNonNull(version, "version"); Objects.requireNonNull(provisioner, "provisioner"); - return FormatterStep.create(NAME, - new FlexmarkStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner))), - FlexmarkStep::equalityState, - State::createFormat); + return FormatterStep.create( + FlexmarkStep.NAME, + new FlexmarkStep( + promise(() -> from(FlexmarkStep.MAVEN_COORDINATE + version, provisioner)), + options + ), + FlexmarkStep::equalityState, + State::createFormat + ); } public static String defaultVersion() { - return DEFAULT_VERSION; + return FlexmarkStep.DEFAULT_VERSION; } private State equalityState() { - return new State(jarState.get()); + return new State(this.jarState.get(), this.options); } private static class State implements Serializable { @@ -65,15 +114,18 @@ private static class State implements Serializable { private final JarState jarState; - State(JarState jarState) { + private final MutableDataSet options; + + State(final JarState jarState, final MutableDataSet options) { this.jarState = jarState; + this.options = options; } FormatterFunc createFormat() throws Exception { - final ClassLoader classLoader = jarState.getClassLoader(); + final ClassLoader classLoader = this.jarState.getClassLoader(); final Class formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.markdown.FlexmarkFormatterFunc"); - final Constructor constructor = formatterFunc.getConstructor(); - return (FormatterFunc) constructor.newInstance(); + final Constructor constructor = formatterFunc.getConstructor(MutableDataHolder.class); + return (FormatterFunc) constructor.newInstance(this.options); } } } diff --git a/plugin-gradle/build.gradle b/plugin-gradle/build.gradle index 4f82f713bf..062d7b348d 100644 --- a/plugin-gradle/build.gradle +++ b/plugin-gradle/build.gradle @@ -20,6 +20,8 @@ dependencies { implementation "com.diffplug.durian:durian-io:${VER_DURIAN}" implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}" implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}" + implementation 'com.vladsch.flexmark:flexmark-all:0.64.8' + implementation 'com.vladsch.flexmark:flexmark-util-data:0.64.8' testImplementation projects.testlib testImplementation "org.junit.jupiter:junit-jupiter:${VER_JUNIT}" diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java index d1f941613b..1d44530b79 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FlexmarkExtension.java @@ -19,6 +19,8 @@ import javax.inject.Inject; +import com.vladsch.flexmark.util.data.MutableDataSet; + import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.markdown.FlexmarkStep; @@ -26,7 +28,7 @@ public class FlexmarkExtension extends FormatExtension { static final String NAME = "flexmark"; @Inject - public FlexmarkExtension(SpotlessExtension spotless) { + public FlexmarkExtension(final SpotlessExtension spotless) { super(spotless); } @@ -34,14 +36,21 @@ public FlexmarkFormatterConfig flexmark() { return flexmark(FlexmarkStep.defaultVersion()); } - public FlexmarkFormatterConfig flexmark(String version) { + public FlexmarkFormatterConfig flexmark(final String version) { return new FlexmarkFormatterConfig(version); } + public FlexmarkFormatterConfig flexmark( + final String version, + final MutableDataSet options + ) { + return new FlexmarkFormatterConfig(version, options); + } + @Override - protected void setupTask(SpotlessTask task) { + protected void setupTask(final SpotlessTask task) { // defaults to all markdown files - if (target == null) { + if (this.target == null) { throw noDefaultTargetException(); } super.setupTask(task); @@ -50,14 +59,29 @@ protected void setupTask(SpotlessTask task) { public class FlexmarkFormatterConfig { private final String version; + private final MutableDataSet options; + + FlexmarkFormatterConfig(final String version) { + this.version = Objects.requireNonNull(version); + this.options = new MutableDataSet(); + addStep(createStep()); + } - FlexmarkFormatterConfig(String version) { + FlexmarkFormatterConfig( + final String version, + final MutableDataSet options + ) { this.version = Objects.requireNonNull(version); + this.options = options; addStep(createStep()); } private FormatterStep createStep() { - return FlexmarkStep.create(this.version, provisioner()); + return FlexmarkStep.create( + this.version, + provisioner(), + this.options + ); } } diff --git a/testlib/build.gradle b/testlib/build.gradle index 08e735bced..26e049dd69 100644 --- a/testlib/build.gradle +++ b/testlib/build.gradle @@ -18,6 +18,8 @@ dependencies { implementation "com.diffplug.durian:durian-io:${VER_DURIAN}" implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}" implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}" + implementation 'com.vladsch.flexmark:flexmark-all:0.64.8' + implementation 'com.vladsch.flexmark:flexmark-util-data:0.64.8' implementation gradleTestKit() }