Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit feb940c

Browse files
committed
Support for including code files and segments
1 parent 7ee8e4f commit feb940c

File tree

3 files changed

+116
-9
lines changed

3 files changed

+116
-9
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@
6767
<version>1.18.12</version>
6868
<scope>provided</scope>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.junit.jupiter</groupId>
72+
<artifactId>junit-jupiter-api</artifactId>
73+
<version>5.7.0-RC1</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.assertj</groupId>
78+
<artifactId>assertj-core</artifactId>
79+
<version>3.16.1</version>
80+
</dependency>
7081
</dependencies>
7182

7283
<build>

src/main/java/io/whelk/asciidoc/TemplateMojo.java

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
import java.nio.file.Files;
44
import java.nio.file.Paths;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
57
import java.util.List;
8+
import java.util.Map;
9+
import java.util.concurrent.atomic.AtomicReference;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
612
import java.util.stream.Collectors;
713

14+
import lombok.Value;
815
import org.apache.maven.plugin.AbstractMojo;
916
import org.apache.maven.plugin.MojoExecutionException;
1017
import org.apache.maven.plugin.MojoFailureException;
@@ -14,10 +21,15 @@
1421
import org.apache.maven.project.MavenProject;
1522

1623
import lombok.SneakyThrows;
24+
import org.assertj.core.util.VisibleForTesting;
1725

1826
@Mojo(name = "build", defaultPhase = LifecyclePhase.PACKAGE)
1927
public class TemplateMojo extends AbstractMojo {
2028

29+
public static final String TAG = "tag";
30+
31+
public static final String TAG_END = "end";
32+
2133
@Parameter(property = "templateDirectory")
2234
String templateDirectory;
2335

@@ -45,10 +57,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
4557

4658
@SneakyThrows
4759
private List<String> readLines(String first, String... more) {
48-
return Files
49-
.readAllLines(Paths.get(first, more))
50-
.stream()
51-
.collect(Collectors.toList());
60+
return new ArrayList<>(Files
61+
.readAllLines(Paths.get(first, more)));
5262
}
5363

5464
private List<String> updateLines(List<String> lines) {
@@ -67,13 +77,62 @@ private List<String> updateLine(final String line) {
6777
}
6878

6979
private boolean matchesIncludeLine(final String line) {
70-
return line.startsWith("include::") &&
71-
line.endsWith(".adoc[]");
80+
return line.startsWith("include::") &&
81+
line.endsWith("]");
82+
}
83+
84+
@VisibleForTesting
85+
List<String> updateIncludeLine(final String line) {
86+
var pathAndOptions = extractPathAndOptions(line);
87+
if (pathAndOptions.optionMap.containsKey(TAG)) {
88+
return readTaggedLines(templateDirectory, pathAndOptions);
89+
}
90+
return this.readLines(templateDirectory, pathAndOptions.path);
91+
}
92+
93+
@SneakyThrows
94+
private List<String> readTaggedLines(String templateDirectory, PathAndOptions path) {
95+
ArrayList<String> lines = new ArrayList<>(Files
96+
.readAllLines(Paths.get(templateDirectory, path.path)));
97+
String tag = path.optionMap.get(TAG);
98+
AtomicReference<Boolean> startHasBeenReached = new AtomicReference<>(false);
99+
AtomicReference<Boolean> endHasBeenReached = new AtomicReference<>(false);
100+
List<String> taggedLines = lines.stream().filter(x -> {
101+
boolean foundStart = x.contains(TAG + "::" + tag);
102+
boolean foundEnd = x.contains(TAG_END + "::" + tag);
103+
if (!startHasBeenReached.get()) {
104+
startHasBeenReached.set(foundStart);
105+
}
106+
if (startHasBeenReached.get() && !endHasBeenReached.get()) {
107+
endHasBeenReached.set(foundEnd);
108+
}
109+
boolean thisIsATagLine = foundStart || foundEnd;
110+
return !thisIsATagLine && startHasBeenReached.get() && !endHasBeenReached.get();
111+
}).collect(Collectors.toList());
112+
return taggedLines;
113+
}
114+
115+
@Value
116+
static
117+
class PathAndOptions {
118+
String path;
119+
Map<String, String> optionMap;
72120
}
73121

74-
private List<String> updateIncludeLine(final String line) {
75-
var path = line.substring(9, line.length() - 2);
76-
return this.readLines(templateDirectory, path);
122+
@VisibleForTesting
123+
PathAndOptions extractPathAndOptions(String line) {
124+
int pathStart = 9;
125+
Pattern pattern = Pattern.compile("\\[.*\\]$");
126+
Matcher matcher = pattern.matcher(line);
127+
boolean found = matcher.find();
128+
String[] allOptions = matcher.group().replaceAll("[\\[\\]]", "").split(",");
129+
Map<String, String> optionMap = Arrays.asList(allOptions).stream()
130+
.filter(x -> x.trim().length() > 0)
131+
.map(x -> x.split("="))
132+
.collect(Collectors.toMap(x -> x[0], x -> x[1]));
133+
int pathEnd = matcher.start();
134+
String path = line.substring(pathStart, pathEnd);
135+
return new PathAndOptions(path, optionMap);
77136
}
78137

79138
private void setDefaultConfiguration() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.whelk.asciidoc;
2+
3+
import io.whelk.asciidoc.TemplateMojo.PathAndOptions;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Map;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
public class TemplateMojoTest {
12+
13+
// tag::exampleShort[]
14+
String test = "this is a small test";
15+
// end::exampleShort[]
16+
17+
@Test
18+
void testFilePathExtraction() {
19+
TemplateMojo templateMojo = new TemplateMojo();
20+
21+
assertThat(templateMojo.extractPathAndOptions("include::otherFile.adoc[]"))
22+
.isEqualTo(new PathAndOptions("otherFile.adoc", Map.of()));
23+
24+
assertThat(templateMojo.extractPathAndOptions("include::src/test/java/io/whelk/asciidoc/TemplateMojoTest.java[tag=exampleShort]"))
25+
.isEqualTo(new PathAndOptions("src/test/java/io/whelk/asciidoc/TemplateMojoTest.java", Map.of("tag", "exampleShort")));
26+
}
27+
28+
@Test
29+
void includeJavaCode() {
30+
TemplateMojo templateMojo = new TemplateMojo();
31+
templateMojo.templateDirectory = "./";
32+
33+
assertThat(templateMojo.updateIncludeLine("include::src/test/java/io/whelk/asciidoc/TemplateMojoTest.java[tag=exampleShort]"))
34+
.containsOnly(" String test = \"this is a small test\";");
35+
}
36+
37+
}

0 commit comments

Comments
 (0)