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

Support for including code files and segments #25

Merged
merged 3 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.whelk.asciidoc</groupId>
<artifactId>asciidoc-template-maven-plugin</artifactId>
<version>1.0.7-RELEASE</version>
<version>1.0.8-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Asciidoc Template Plugin</name>
Expand Down Expand Up @@ -67,6 +67,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>
</dependency>
</dependencies>

<build>
Expand Down
77 changes: 68 additions & 9 deletions src/main/java/io/whelk/asciidoc/TemplateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
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;
Expand All @@ -14,10 +21,15 @@
import org.apache.maven.project.MavenProject;

import lombok.SneakyThrows;
import org.assertj.core.util.VisibleForTesting;

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

public static final String TAG = "tag";

public static final String TAG_END = "end";

@Parameter(property = "templateDirectory")
String templateDirectory;

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

@SneakyThrows
private List<String> readLines(String first, String... more) {
return Files
.readAllLines(Paths.get(first, more))
.stream()
.collect(Collectors.toList());
return new ArrayList<>(Files
.readAllLines(Paths.get(first, more)));
}

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

private boolean matchesIncludeLine(final String line) {
return line.startsWith("include::") &&
line.endsWith(".adoc[]");
return line.startsWith("include::") &&
line.endsWith("]");
}

@VisibleForTesting
List<String> updateIncludeLine(final String line) {
var pathAndOptions = extractPathAndOptions(line);
if (pathAndOptions.optionMap.containsKey(TAG)) {
return readTaggedLines(templateDirectory, pathAndOptions);
}
return this.readLines(templateDirectory, pathAndOptions.path);
}

@SneakyThrows
private List<String> readTaggedLines(String templateDirectory, PathAndOptions path) {
ArrayList<String> lines = new ArrayList<>(Files
.readAllLines(Paths.get(templateDirectory, path.path)));
String tag = path.optionMap.get(TAG);
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);
if (!startHasBeenReached.get()) {
startHasBeenReached.set(foundStart);
}
if (startHasBeenReached.get() && !endHasBeenReached.get()) {
endHasBeenReached.set(foundEnd);
}
boolean thisIsATagLine = foundStart || foundEnd;
return !thisIsATagLine && startHasBeenReached.get() && !endHasBeenReached.get();
}).collect(Collectors.toList());
return taggedLines;
}

@Value
static
class PathAndOptions {
String path;
Map<String, String> optionMap;
}

private List<String> updateIncludeLine(final String line) {
var path = line.substring(9, line.length() - 2);
return this.readLines(templateDirectory, path);
@VisibleForTesting
PathAndOptions extractPathAndOptions(String line) {
int pathStart = 9;
Pattern pattern = Pattern.compile("\\[.*\\]$");
Matcher matcher = pattern.matcher(line);
boolean found = matcher.find();
String[] allOptions = matcher.group().replaceAll("[\\[\\]]", "").split(",");
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]));
int pathEnd = matcher.start();
String path = line.substring(pathStart, pathEnd);
return new PathAndOptions(path, optionMap);
}

private void setDefaultConfiguration() {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/io/whelk/asciidoc/TemplateMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.whelk.asciidoc;

import io.whelk.asciidoc.TemplateMojo.PathAndOptions;

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class TemplateMojoTest {

// tag::exampleShort[]
String test = "this is a small test";
// end::exampleShort[]

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

assertThat(templateMojo.extractPathAndOptions("include::otherFile.adoc[]"))
.isEqualTo(new PathAndOptions("otherFile.adoc", Map.of()));

assertThat(templateMojo.extractPathAndOptions("include::src/test/java/io/whelk/asciidoc/TemplateMojoTest.java[tag=exampleShort]"))
.isEqualTo(new PathAndOptions("src/test/java/io/whelk/asciidoc/TemplateMojoTest.java", Map.of("tag", "exampleShort")));
}

@Test
void includeJavaCode() {
TemplateMojo templateMojo = new TemplateMojo();
templateMojo.templateDirectory = "./";

assertThat(templateMojo.updateIncludeLine("include::src/test/java/io/whelk/asciidoc/TemplateMojoTest.java[tag=exampleShort]"))
.containsOnly(" String test = \"this is a small test\";");
}

}