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

Commit f5b5df7

Browse files
authored
Merge pull request #28 from astubbs/code-includes
Support document variables substitution in paths
2 parents 87970d8 + f32112e commit f5b5df7

File tree

3 files changed

+98
-25
lines changed

3 files changed

+98
-25
lines changed

pom.xml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
34

45
<modelVersion>4.0.0</modelVersion>
56

@@ -67,16 +68,17 @@
6768
<version>1.18.12</version>
6869
<scope>provided</scope>
6970
</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>
7671
<dependency>
7772
<groupId>org.assertj</groupId>
7873
<artifactId>assertj-core</artifactId>
7974
<version>3.16.1</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.junit.jupiter</groupId>
79+
<artifactId>junit-jupiter-engine</artifactId>
80+
<version>5.7.0-RC1</version>
81+
<scope>test</scope>
8082
</dependency>
8183
</dependencies>
8284

@@ -95,6 +97,12 @@
9597
</plugin>
9698
</plugins>
9799
</pluginManagement>
100+
<plugins>
101+
<plugin>
102+
<artifactId>maven-surefire-plugin</artifactId>
103+
<version>3.0.0-M5</version>
104+
</plugin>
105+
</plugins>
98106
</build>
99107

100108
<distributionManagement>

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

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package io.whelk.asciidoc;
22

3+
import lombok.SneakyThrows;
4+
import lombok.Value;
5+
import org.apache.maven.plugin.AbstractMojo;
6+
import org.apache.maven.plugin.MojoExecutionException;
7+
import org.apache.maven.plugin.MojoFailureException;
8+
import org.apache.maven.plugins.annotations.LifecyclePhase;
9+
import org.apache.maven.plugins.annotations.Mojo;
10+
import org.apache.maven.plugins.annotations.Parameter;
11+
import org.apache.maven.project.MavenProject;
12+
313
import java.nio.file.Files;
414
import java.nio.file.Paths;
515
import java.util.ArrayList;
@@ -11,17 +21,7 @@
1121
import java.util.regex.Pattern;
1222
import java.util.stream.Collectors;
1323

14-
import lombok.Value;
15-
import org.apache.maven.plugin.AbstractMojo;
16-
import org.apache.maven.plugin.MojoExecutionException;
17-
import org.apache.maven.plugin.MojoFailureException;
18-
import org.apache.maven.plugins.annotations.LifecyclePhase;
19-
import org.apache.maven.plugins.annotations.Mojo;
20-
import org.apache.maven.plugins.annotations.Parameter;
21-
import org.apache.maven.project.MavenProject;
22-
23-
import lombok.SneakyThrows;
24-
import org.assertj.core.util.VisibleForTesting;
24+
import static java.util.stream.Collectors.toMap;
2525

2626
@Mojo(name = "build", defaultPhase = LifecyclePhase.PACKAGE)
2727
public class TemplateMojo extends AbstractMojo {
@@ -45,11 +45,15 @@ public class TemplateMojo extends AbstractMojo {
4545
@Parameter(defaultValue = "${project}", required = true, readonly = true)
4646
MavenProject project;
4747

48+
// @VisibleForTesting
49+
Map<String, String> vars = Map.of();
50+
4851
@SneakyThrows
4952
public void execute() throws MojoExecutionException, MojoFailureException {
5053
setDefaultConfiguration();
5154

5255
final var lines = this.readLines(templateDirectory, templateFile);
56+
this.vars = loadVars(lines);
5357
final var updatedLines = this.updateLines(lines);
5458

5559
Files.write(Paths.get(outputDirectory, outputFile), updatedLines);
@@ -61,6 +65,27 @@ private List<String> readLines(String first, String... more) {
6165
.readAllLines(Paths.get(first, more)));
6266
}
6367

68+
// @VisibleForTesting
69+
Map<String, String> loadVars(List<String> strings) {
70+
String varRegex = "^:[\\w\\-]+:"; //includes dashes
71+
Pattern compile = Pattern.compile(varRegex);
72+
return strings.stream()
73+
.map(x -> {
74+
Matcher matcher = compile.matcher(x);
75+
if (matcher.find()) {
76+
int end = matcher.end();
77+
String varName = matcher.group().trim();
78+
String trimMarkers = varName.substring(1, varName.length() - 1).trim();
79+
String value = x.substring(end).trim();
80+
return List.of(trimMarkers, value);
81+
} else {
82+
return List.<String>of();
83+
}
84+
})
85+
.filter(x -> x.size() == 2)
86+
.collect(toMap(x -> x.get(0), x -> x.get(1)));
87+
}
88+
6489
private List<String> updateLines(List<String> lines) {
6590
return lines
6691
.stream()
@@ -81,7 +106,7 @@ private boolean matchesIncludeLine(final String line) {
81106
line.endsWith("]");
82107
}
83108

84-
@VisibleForTesting
109+
// @VisibleForTesting
85110
List<String> updateIncludeLine(final String line) {
86111
var pathAndOptions = extractPathAndOptions(line);
87112
if (pathAndOptions.optionMap.containsKey(TAG)) {
@@ -98,12 +123,14 @@ private List<String> readTaggedLines(String templateDirectory, PathAndOptions pa
98123
AtomicReference<Boolean> startHasBeenReached = new AtomicReference<>(false);
99124
AtomicReference<Boolean> endHasBeenReached = new AtomicReference<>(false);
100125
List<String> taggedLines = lines.stream().filter(x -> {
101-
boolean foundStart = x.contains(TAG + "::" + tag);
102-
boolean foundEnd = x.contains(TAG_END + "::" + tag);
126+
boolean foundStart = false;
127+
boolean foundEnd = false;
103128
if (!startHasBeenReached.get()) {
129+
foundStart = x.contains(TAG + "::" + tag);
104130
startHasBeenReached.set(foundStart);
105131
}
106132
if (startHasBeenReached.get() && !endHasBeenReached.get()) {
133+
foundEnd = x.contains(TAG_END + "::" + tag);
107134
endHasBeenReached.set(foundEnd);
108135
}
109136
boolean thisIsATagLine = foundStart || foundEnd;
@@ -119,7 +146,7 @@ class PathAndOptions {
119146
Map<String, String> optionMap;
120147
}
121148

122-
@VisibleForTesting
149+
// @VisibleForTesting
123150
PathAndOptions extractPathAndOptions(String line) {
124151
int pathStart = 9;
125152
Pattern pattern = Pattern.compile("\\[.*\\]$");
@@ -129,9 +156,13 @@ PathAndOptions extractPathAndOptions(String line) {
129156
Map<String, String> optionMap = Arrays.asList(allOptions).stream()
130157
.filter(x -> x.trim().length() > 0)
131158
.map(x -> x.split("="))
132-
.collect(Collectors.toMap(x -> x[0], x -> x[1]));
159+
.collect(toMap(x -> x[0], x -> x[1]));
133160
int pathEnd = matcher.start();
134161
String path = line.substring(pathStart, pathEnd);
162+
for (var variable : this.vars.entrySet()) {
163+
String needle = "\\{" + variable.getKey() + "\\}";
164+
path = path.replaceAll(needle, variable.getValue());
165+
}
135166
return new PathAndOptions(path, optionMap);
136167
}
137168

src/test/java/io/whelk/asciidoc/TemplateMojoTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.whelk.asciidoc;
22

33
import io.whelk.asciidoc.TemplateMojo.PathAndOptions;
4-
54
import org.junit.jupiter.api.Test;
65

6+
import java.util.List;
77
import java.util.Map;
88

99
import static org.assertj.core.api.Assertions.assertThat;
@@ -15,7 +15,7 @@ public class TemplateMojoTest {
1515
// end::exampleShort[]
1616

1717
@Test
18-
void testFilePathExtraction() {
18+
void filePathExtraction() {
1919
TemplateMojo templateMojo = new TemplateMojo();
2020

2121
assertThat(templateMojo.extractPathAndOptions("include::otherFile.adoc[]"))
@@ -34,4 +34,38 @@ void includeJavaCode() {
3434
.containsOnly(" String test = \"this is a small test\";");
3535
}
3636

37+
@Test
38+
void loadVars() {
39+
TemplateMojo templateMojo = new TemplateMojo();
40+
Map<String, String> vars = templateMojo.loadVars(List.of(
41+
"My Doc",
42+
"::",
43+
"",
44+
":: 2",
45+
":myVar: 4",
46+
":noSpace:5",
47+
":baseDir: relativeDirectory",
48+
"some content",
49+
":Here you can see a weirdly used colon",
50+
":dash-var: dv"
51+
));
52+
Map<String, String> expected = Map.of(
53+
"myVar", "4",
54+
"noSpace", "5",
55+
"baseDir", "relativeDirectory",
56+
"dash-var", "dv");
57+
assertThat(vars).containsExactlyInAnyOrderEntriesOf(expected);
58+
}
59+
60+
@Test
61+
void variableInPath() {
62+
TemplateMojo templateMojo = new TemplateMojo();
63+
Map<String, String> vars = templateMojo.loadVars(List.of(":rootDir: rootHere",
64+
":another: hereAlso"));
65+
templateMojo.vars = vars;
66+
67+
assertThat(templateMojo.extractPathAndOptions("include::{rootDir}/{another}/intoThis[]").getPath())
68+
.isEqualTo("rootHere/hereAlso/intoThis");
69+
}
70+
3771
}

0 commit comments

Comments
 (0)