Skip to content

Commit

Permalink
feat(diffplug#2183): flexmark configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
l3r8yJ committed Sep 27, 2024
1 parent 94ab966 commit 6ff2c63
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 24 deletions.
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
82 changes: 67 additions & 15 deletions lib/src/main/java/com/diffplug/spotless/markdown/FlexmarkStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 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);
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,38 @@

import javax.inject.Inject;

import com.vladsch.flexmark.util.data.MutableDataSet;

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);
Expand All @@ -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
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions testlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 6ff2c63

Please sign in to comment.