Skip to content

Commit 9d76248

Browse files
committed
Code cleanup
1 parent a99a891 commit 9d76248

File tree

7 files changed

+47
-57
lines changed

7 files changed

+47
-57
lines changed

heylogs-api/src/main/java/internal/heylogs/FlexmarkIO.java

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.vladsch.flexmark.formatter.Formatter;
44
import com.vladsch.flexmark.parser.Parser;
5+
import com.vladsch.flexmark.util.ast.Document;
6+
import nbbrd.io.text.TextFormatter;
7+
import nbbrd.io.text.TextParser;
58

69
public final class FlexmarkIO {
710

@@ -13,9 +16,17 @@ public static Parser newParser() {
1316
return Parser.builder().build();
1417
}
1518

19+
public static TextParser<Document> newTextParser() {
20+
return TextParser.onParsingReader(newParser()::parseReader);
21+
}
22+
1623
public static Formatter newFormatter() {
1724
Formatter.Builder result = Formatter.builder();
1825
result.set(Formatter.MAX_TRAILING_BLANK_LINES, 0);
1926
return result.build();
2027
}
28+
29+
public static TextFormatter<Document> newTextFormatter() {
30+
return TextFormatter.onFormattingWriter(newFormatter()::render);
31+
}
2132
}

heylogs-api/src/test/java/nbbrd/heylogs/FilterTest.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.time.LocalDate;
77
import java.util.function.Function;
88

9-
import static nbbrd.heylogs.Filter.builder;
109
import static nbbrd.heylogs.Filter.parseLocalDate;
1110
import static nbbrd.heylogs.Version.HYPHEN;
1211
import static org.assertj.core.api.Assertions.*;
@@ -15,43 +14,43 @@ public class FilterTest {
1514

1615
@Test
1716
public void testRef() {
18-
assertThat(builder().build())
17+
assertThat(Filter.builder().build())
1918
.describedAs("Empty reference")
2019
.is(containing(unreleased))
2120
.is(containing(v1_1_0))
2221
.is(containing(v1_0_0));
2322

24-
assertThat(builder().ref("Unreleased").build())
23+
assertThat(Filter.builder().ref("Unreleased").build())
2524
.describedAs("Full reference")
2625
.is(containing(unreleased))
2726
.isNot(containing(v1_1_0))
2827
.isNot(containing(v1_0_0));
2928

30-
assertThat(builder().ref("1.1.0").build())
29+
assertThat(Filter.builder().ref("1.1.0").build())
3130
.describedAs("Full reference")
3231
.isNot(containing(unreleased))
3332
.is(containing(v1_1_0))
3433
.isNot(containing(v1_0_0));
3534

36-
assertThat(builder().ref("rel").build())
35+
assertThat(Filter.builder().ref("rel").build())
3736
.describedAs("Partial reference")
3837
.is(containing(unreleased))
3938
.isNot(containing(v1_1_0))
4039
.isNot(containing(v1_0_0));
4140

42-
assertThat(builder().ref("1.").build())
41+
assertThat(Filter.builder().ref("1.").build())
4342
.describedAs("Partial reference")
4443
.isNot(containing(unreleased))
4544
.is(containing(v1_1_0))
4645
.is(containing(v1_0_0));
4746

48-
assertThat(builder().ref("other").build())
47+
assertThat(Filter.builder().ref("other").build())
4948
.describedAs("Unknown reference")
5049
.isNot(containing(unreleased))
5150
.isNot(containing(v1_1_0))
5251
.isNot(containing(v1_0_0));
5352

54-
assertThat(builder().ref("other-SNAPSHOT").build())
53+
assertThat(Filter.builder().ref("other-SNAPSHOT").build())
5554
.describedAs("Matching unreleased pattern reference")
5655
.is(containing(unreleased))
5756
.isNot(containing(v1_1_0))
@@ -60,7 +59,7 @@ public void testRef() {
6059

6160
@Test
6261
public void testTimeRange() {
63-
Function<TimeRange, Filter> onTimeRange = o -> builder().timeRange(o).build();
62+
Function<TimeRange, Filter> onTimeRange = o -> Filter.builder().timeRange(o).build();
6463

6564
assertThat(TimeRange.ALL)
6665
.extracting(onTimeRange)

heylogs-api/src/test/java/nbbrd/heylogs/HeylogsTest.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
import static internal.heylogs.URLExtractor.urlOf;
2323
import static java.util.Collections.singletonList;
24-
import static nbbrd.heylogs.Filter.builder;
2524
import static nbbrd.heylogs.Heylogs.FIRST_FORMAT_AVAILABLE;
2625
import static nbbrd.heylogs.spi.RuleSeverity.ERROR;
26+
import static nbbrd.io.function.IOFunction.unchecked;
2727
import static org.assertj.core.api.Assertions.*;
2828
import static org.assertj.core.api.InstanceOfAssertFactories.list;
2929
import static tests.heylogs.api.Sample.using;
@@ -62,13 +62,9 @@ public void testCheckFormat() {
6262
public void testExtractVersions() {
6363
Heylogs x = Heylogs.ofServiceLoader();
6464

65-
Function<Filter, String> usingMain = extractor -> {
66-
Document doc = using("/Main.md");
67-
x.extractVersions(doc, extractor);
68-
return FlexmarkIO.newFormatter().render(doc);
69-
};
65+
Function<Filter, String> usingMain = extractor -> extractVersionsToString(x, using("/Main.md"), extractor);
7066

71-
assertThat(builder().ref("1.1.0").build())
67+
assertThat(Filter.builder().ref("1.1.0").build())
7268
.extracting(usingMain, STRING)
7369
.isEqualTo(
7470
"## [1.1.0] - 2019-02-15\n" +
@@ -86,14 +82,14 @@ public void testExtractVersions() {
8682
"\n" +
8783
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");
8884

89-
assertThat(builder().ref("1.1.0").ignoreContent(true).build())
85+
assertThat(Filter.builder().ref("1.1.0").ignoreContent(true).build())
9086
.extracting(usingMain, STRING)
9187
.isEqualTo(
9288
"## [1.1.0] - 2019-02-15\n" +
9389
"\n" +
9490
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");
9591

96-
assertThat(builder().ref("zzz").build())
92+
assertThat(Filter.builder().ref("zzz").build())
9793
.extracting(usingMain, STRING)
9894
.isEmpty();
9995
}
@@ -260,9 +256,14 @@ public void testFormatStatus() throws IOException {
260256
);
261257
}
262258

259+
private static String extractVersionsToString(Heylogs heylogs, Document doc, Filter extractor) {
260+
heylogs.extractVersions(doc, extractor);
261+
return unchecked(FlexmarkIO.newTextFormatter()::formatToString).apply(doc);
262+
}
263+
263264
private static String releaseChangesToString(Heylogs heylogs, Document doc, Version version) {
264265
heylogs.releaseChanges(doc, version, "v");
265-
return FlexmarkIO.newFormatter().render(doc);
266+
return unchecked(FlexmarkIO.newTextFormatter()::formatToString).apply(doc);
266267
}
267268

268269
private static final class MockedRule implements Rule {

heylogs-api/src/test/java/tests/heylogs/api/Sample.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,32 @@
99
import nbbrd.io.function.IOConsumer;
1010
import org.assertj.core.util.URLs;
1111

12-
import java.io.*;
13-
import java.nio.charset.StandardCharsets;
1412
import java.time.LocalDate;
1513
import java.util.Objects;
1614

15+
import static java.nio.charset.StandardCharsets.UTF_8;
1716
import static nbbrd.heylogs.spi.RuleSeverity.ERROR;
17+
import static nbbrd.io.function.IOFunction.unchecked;
1818

1919
public class Sample {
2020

2121
public static Document using(String name) {
22-
try (InputStream stream = Sample.class.getResourceAsStream(name)) {
23-
if (stream == null) {
24-
throw new IllegalArgumentException("Missing resource '" + name + "'");
25-
}
26-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
27-
return FlexmarkIO.newParser().parseReader(reader);
28-
}
29-
} catch (IOException ex) {
30-
throw new UncheckedIOException(ex);
31-
}
22+
return unchecked((String x) -> FlexmarkIO.newTextParser().parseResource(Sample.class, x, UTF_8))
23+
.apply(name);
3224
}
3325

3426
public static Heading asHeading(String text) {
35-
return (Heading) FlexmarkIO.newParser().parse(text).getChildOfType(Heading.class);
27+
return unchecked(FlexmarkIO.newTextParser()::parseChars)
28+
.andThen(doc -> (Heading) doc.getChildOfType(Heading.class))
29+
.apply(text);
3630
}
3731

3832
public static String asText(Heading heading) {
3933
Document doc = new Document(null, BasedSequence.NULL);
4034
doc.appendChild(heading);
41-
return FlexmarkIO.newFormatter().render(doc).trim();
35+
return unchecked(FlexmarkIO.newTextFormatter()::formatToString)
36+
.andThen(String::trim)
37+
.apply(doc);
4238
}
4339

4440
public static final Problem PROBLEM1 = Problem.builder().id("rule1").severity(ERROR).issue(RuleIssue.builder().message("boom").line(5).column(18).build()).build();
@@ -72,6 +68,6 @@ public static String writing(IOConsumer<? super Appendable> content) {
7268
}
7369

7470
public static String contentOf(Class<?> anchor, String resourceName) {
75-
return URLs.contentOf(Objects.requireNonNull(anchor.getResource(resourceName)), StandardCharsets.UTF_8);
71+
return URLs.contentOf(Objects.requireNonNull(anchor.getResource(resourceName)), UTF_8);
7672
}
7773
}

heylogs-cli/src/main/java/internal/heylogs/cli/MarkdownInputSupport.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package internal.heylogs.cli;
22

3-
import com.vladsch.flexmark.parser.Parser;
43
import com.vladsch.flexmark.util.ast.Document;
54
import internal.heylogs.FlexmarkIO;
65
import lombok.AccessLevel;
@@ -10,7 +9,6 @@
109
import nbbrd.design.StaticFactoryMethod;
1110

1211
import java.io.IOException;
13-
import java.io.Reader;
1412
import java.nio.file.DirectoryStream;
1513
import java.nio.file.Path;
1614
import java.util.Locale;
@@ -26,12 +24,8 @@ public class MarkdownInputSupport extends TextInputSupport implements DirectoryS
2624
return CommandSupporter.create(MarkdownInputSupport::new, supporters);
2725
}
2826

29-
private @NonNull Parser parser = FlexmarkIO.newParser();
30-
3127
public Document readDocument(Path file) throws IOException {
32-
try (Reader reader = newBufferedReader(file)) {
33-
return parser.parseReader(reader);
34-
}
28+
return FlexmarkIO.newTextParser().parseReader(() -> newBufferedReader(file));
3529
}
3630

3731
public String getName(Path file) {

heylogs-cli/src/main/java/internal/heylogs/cli/MarkdownOutputSupport.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package internal.heylogs.cli;
22

3-
import com.vladsch.flexmark.formatter.Formatter;
43
import com.vladsch.flexmark.util.ast.Document;
54
import internal.heylogs.FlexmarkIO;
65
import lombok.AccessLevel;
@@ -10,7 +9,6 @@
109
import nbbrd.design.StaticFactoryMethod;
1110

1211
import java.io.IOException;
13-
import java.io.Writer;
1412
import java.nio.file.Path;
1513

1614
@lombok.Getter
@@ -24,11 +22,7 @@ public class MarkdownOutputSupport extends TextOutputSupport {
2422
return CommandSupporter.create(MarkdownOutputSupport::new, supporters);
2523
}
2624

27-
private @NonNull Formatter formatter = FlexmarkIO.newFormatter();
28-
2925
public void writeDocument(Path file, Document document) throws IOException {
30-
try (Writer writer = newBufferedWriter(file)) {
31-
formatter.render(document, writer);
32-
}
26+
FlexmarkIO.newTextFormatter().formatWriter(document, () -> newBufferedWriter(file));
3327
}
3428
}

heylogs-maven-plugin/src/main/java/nbbrd/heylogs/maven/plugin/HeylogsMojo.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package nbbrd.heylogs.maven.plugin;
22

3-
import com.vladsch.flexmark.formatter.Formatter;
4-
import com.vladsch.flexmark.parser.Parser;
53
import com.vladsch.flexmark.util.ast.Document;
64
import internal.heylogs.FlexmarkIO;
75
import nbbrd.design.MightBePromoted;
@@ -16,6 +14,7 @@
1614
import java.util.function.Consumer;
1715

1816
import static internal.heylogs.maven.plugin.HeylogsParameters.isMojoLogFile;
17+
import static java.nio.charset.StandardCharsets.UTF_8;
1918
import static nbbrd.console.picocli.text.TextOutputSupport.newTextOutputSupport;
2019

2120
abstract class HeylogsMojo extends AbstractMojo {
@@ -44,22 +43,18 @@ protected void notifyMissingChangelog() {
4443

4544
protected Document readChangelog(File inputFile) throws MojoExecutionException {
4645
getLog().info("Reading changelog " + inputFile);
47-
Parser parser = FlexmarkIO.newParser();
48-
try (Reader reader = Files.newBufferedReader(inputFile.toPath())) {
49-
return parser.parseReader(reader);
46+
try {
47+
return FlexmarkIO.newTextParser().parseFile(inputFile, UTF_8);
5048
} catch (IOException ex) {
5149
throw new MojoExecutionException("Failed to read changelog", ex);
5250
}
5351
}
5452

5553
protected void writeChangelog(Document document, File outputFile) throws MojoExecutionException {
5654
getLog().info("Writing changelog " + outputFile);
57-
Formatter formatter = FlexmarkIO.newFormatter();
5855
try {
5956
Files.createDirectories(outputFile.getParentFile().toPath());
60-
try (Writer writer = Files.newBufferedWriter(outputFile.toPath())) {
61-
formatter.render(document, writer);
62-
}
57+
FlexmarkIO.newTextFormatter().formatFile(document, outputFile, UTF_8);
6358
} catch (IOException ex) {
6459
throw new MojoExecutionException("Failed to write changelog", ex);
6560
}

0 commit comments

Comments
 (0)