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

Commit

Permalink
Merge pull request #28 from astubbs/code-includes
Browse files Browse the repository at this point in the history
Support document variables substitution in paths
  • Loading branch information
zteater authored Sep 4, 2020
2 parents 87970d8 + f32112e commit f5b5df7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 25 deletions.
22 changes: 15 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<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">
<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">

<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -67,16 +68,17 @@
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0-RC1</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -95,6 +97,12 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>

<distributionManagement>
Expand Down
63 changes: 47 additions & 16 deletions src/main/java/io/whelk/asciidoc/TemplateMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package io.whelk.asciidoc;

import lombok.SneakyThrows;
import lombok.Value;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -11,17 +21,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.Value;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import lombok.SneakyThrows;
import org.assertj.core.util.VisibleForTesting;
import static java.util.stream.Collectors.toMap;

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

// @VisibleForTesting
Map<String, String> vars = Map.of();

@SneakyThrows
public void execute() throws MojoExecutionException, MojoFailureException {
setDefaultConfiguration();

final var lines = this.readLines(templateDirectory, templateFile);
this.vars = loadVars(lines);
final var updatedLines = this.updateLines(lines);

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

// @VisibleForTesting
Map<String, String> loadVars(List<String> strings) {
String varRegex = "^:[\\w\\-]+:"; //includes dashes
Pattern compile = Pattern.compile(varRegex);
return strings.stream()
.map(x -> {
Matcher matcher = compile.matcher(x);
if (matcher.find()) {
int end = matcher.end();
String varName = matcher.group().trim();
String trimMarkers = varName.substring(1, varName.length() - 1).trim();
String value = x.substring(end).trim();
return List.of(trimMarkers, value);
} else {
return List.<String>of();
}
})
.filter(x -> x.size() == 2)
.collect(toMap(x -> x.get(0), x -> x.get(1)));
}

private List<String> updateLines(List<String> lines) {
return lines
.stream()
Expand All @@ -81,7 +106,7 @@ private boolean matchesIncludeLine(final String line) {
line.endsWith("]");
}

@VisibleForTesting
// @VisibleForTesting
List<String> updateIncludeLine(final String line) {
var pathAndOptions = extractPathAndOptions(line);
if (pathAndOptions.optionMap.containsKey(TAG)) {
Expand All @@ -98,12 +123,14 @@ private List<String> readTaggedLines(String templateDirectory, PathAndOptions pa
AtomicReference<Boolean> startHasBeenReached = new AtomicReference<>(false);
AtomicReference<Boolean> endHasBeenReached = new AtomicReference<>(false);
List<String> taggedLines = lines.stream().filter(x -> {
boolean foundStart = x.contains(TAG + "::" + tag);
boolean foundEnd = x.contains(TAG_END + "::" + tag);
boolean foundStart = false;
boolean foundEnd = false;
if (!startHasBeenReached.get()) {
foundStart = x.contains(TAG + "::" + tag);
startHasBeenReached.set(foundStart);
}
if (startHasBeenReached.get() && !endHasBeenReached.get()) {
foundEnd = x.contains(TAG_END + "::" + tag);
endHasBeenReached.set(foundEnd);
}
boolean thisIsATagLine = foundStart || foundEnd;
Expand All @@ -119,7 +146,7 @@ class PathAndOptions {
Map<String, String> optionMap;
}

@VisibleForTesting
// @VisibleForTesting
PathAndOptions extractPathAndOptions(String line) {
int pathStart = 9;
Pattern pattern = Pattern.compile("\\[.*\\]$");
Expand All @@ -129,9 +156,13 @@ PathAndOptions extractPathAndOptions(String line) {
Map<String, String> optionMap = Arrays.asList(allOptions).stream()
.filter(x -> x.trim().length() > 0)
.map(x -> x.split("="))
.collect(Collectors.toMap(x -> x[0], x -> x[1]));
.collect(toMap(x -> x[0], x -> x[1]));
int pathEnd = matcher.start();
String path = line.substring(pathStart, pathEnd);
for (var variable : this.vars.entrySet()) {
String needle = "\\{" + variable.getKey() + "\\}";
path = path.replaceAll(needle, variable.getValue());
}
return new PathAndOptions(path, optionMap);
}

Expand Down
38 changes: 36 additions & 2 deletions src/test/java/io/whelk/asciidoc/TemplateMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.whelk.asciidoc;

import io.whelk.asciidoc.TemplateMojo.PathAndOptions;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

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

@Test
void testFilePathExtraction() {
void filePathExtraction() {
TemplateMojo templateMojo = new TemplateMojo();

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

@Test
void loadVars() {
TemplateMojo templateMojo = new TemplateMojo();
Map<String, String> vars = templateMojo.loadVars(List.of(
"My Doc",
"::",
"",
":: 2",
":myVar: 4",
":noSpace:5",
":baseDir: relativeDirectory",
"some content",
":Here you can see a weirdly used colon",
":dash-var: dv"
));
Map<String, String> expected = Map.of(
"myVar", "4",
"noSpace", "5",
"baseDir", "relativeDirectory",
"dash-var", "dv");
assertThat(vars).containsExactlyInAnyOrderEntriesOf(expected);
}

@Test
void variableInPath() {
TemplateMojo templateMojo = new TemplateMojo();
Map<String, String> vars = templateMojo.loadVars(List.of(":rootDir: rootHere",
":another: hereAlso"));
templateMojo.vars = vars;

assertThat(templateMojo.extractPathAndOptions("include::{rootDir}/{another}/intoThis[]").getPath())
.isEqualTo("rootHere/hereAlso/intoThis");
}

}

0 comments on commit f5b5df7

Please sign in to comment.