-
Notifications
You must be signed in to change notification settings - Fork 486
feat(#2183): flexmark configuration support #2280
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ public class FlexmarkFormatterFunc implements FormatterFunc { | |
private final Parser parser; | ||
private final Formatter formatter; | ||
|
||
public FlexmarkFormatterFunc() { | ||
public FlexmarkFormatterFunc(final MutableDataHolder customFormatterOptions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We need to pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be hard, since Flexmark reinvent the wheels... |
||
// 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); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to load the classes at runtime. |
||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
|
@@ -31,49 +38,94 @@ 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 transient 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 { | ||
private static final long serialVersionUID = 1L; | ||
|
||
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,29 +19,38 @@ | |
|
||
import javax.inject.Inject; | ||
|
||
import com.vladsch.flexmark.util.data.MutableDataSet; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No dependencies. |
||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.markdown.FlexmarkStep; | ||
|
||
public class FlexmarkExtension extends FormatExtension { | ||
static final String NAME = "flexmark"; | ||
|
||
@Inject | ||
public FlexmarkExtension(SpotlessExtension spotless) { | ||
public FlexmarkExtension(final SpotlessExtension spotless) { | ||
super(spotless); | ||
} | ||
|
||
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 | ||
); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These can't be here either. |
||
implementation gradleTestKit() | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has to be
flexmarkCompileOnly
, notimplementation
. Spotless has way too many dependencies to deal with all the transitive conflicts. You can only use flexmark classes inside ofFlexmarkFormatterFunc
, which gets called with a special classloader after Gradle/Maven has done the dependency resolution.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nedtwigg okay, got it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nedtwigg I'm a bit confused. If we 're using only
flexmarkCompileOnly
it means that I can't get access toflexmark-util-data
API inside of FlexmarkStep, which makes it impossible to run with custom configuration or am I wrong?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, @l3r8yJ. It's been almost a year and I don't know if you still have the interest to finish this PR.
Anyway, you can find an example of the usage of classloader in JsonSimpleStep. You need to use
com.diffplug.spotless.JarState
and load the class at runtime.