Makes it possible to put templates in subdirectories

This commit is contained in:
Johan Maasing 2025-05-02 08:44:44 +02:00
parent c1c062b6cf
commit 68dc70c176
Signed by: johan
GPG key ID: FFD31BABEE2DEED2
4 changed files with 14 additions and 10 deletions

View file

@ -21,6 +21,7 @@ import nu.zoom.dsl.ast.DocumentNode;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
@ -50,17 +51,20 @@ public class Generator {
} }
public List<Path> generate() throws IOException, TemplateException { public List<Path> generate() throws IOException, TemplateException {
try (Stream<Path> files = Files.list(templatesDir)) { try (Stream<Path> files = Files.walk(templatesDir)) {
List<String> templates = files List<Path> templates = files
.map(Path::getFileName) .filter(p -> {
.map(Path::toString) var fname = p.getFileName().toString();
.filter(p -> p.length() > TEMPLATE_EXTENSION_LENGTH && p.endsWith(TEMPLATE_EXTENSION) return fname.length() > TEMPLATE_EXTENSION_LENGTH && fname.endsWith(TEMPLATE_EXTENSION) ;
) }
.toList(); )
.map(p -> templatesDir.relativize(p))
.toList();
ArrayList<Path> out = new ArrayList<>(); ArrayList<Path> out = new ArrayList<>();
for (String template : templates) { for (Path template : templates) {
Path outpath = outputDir.resolve(outputFilenameFromTemplate(template)); Path outpath = outputDir.resolve(outputFilenameFromTemplate(template.toString()));
Template ftl = this.cfg.getTemplate(template); Files.createDirectories(outpath.getParent());
Template ftl = this.cfg.getTemplate(template.toString());
try (var outw = Files.newBufferedWriter(outpath, StandardCharsets.UTF_8)) { try (var outw = Files.newBufferedWriter(outpath, StandardCharsets.UTF_8)) {
ftl.process(this.data, outw); ftl.process(this.data, outw);
out.add(outpath); out.add(outpath);