Output files are named as the templates with the ending stripped.

This commit is contained in:
Johan Maasing 2025-04-13 14:11:03 +02:00
parent b86568a6d0
commit 6f1b026dd5
6 changed files with 30 additions and 34 deletions

View file

@ -29,10 +29,12 @@ import java.util.Objects;
import java.util.stream.Stream;
public class Generator {
private final Path templatesDir ;
private final Path templatesDir;
private final DocumentNode data;
private final Path outputDir ;
private final Path outputDir;
private final Configuration cfg;
private final String TEMPLATE_EXTENSION = ".ftl";
private final int TEMPLATE_EXTENSION_LENGTH = TEMPLATE_EXTENSION.length();
public Generator(Path templatesDir, DocumentNode data, Path outputDir) throws IOException {
this.templatesDir = Objects.requireNonNull(templatesDir);
@ -49,23 +51,26 @@ public class Generator {
public List<Path> generate() throws IOException, TemplateException {
try (Stream<Path> files = Files.list(templatesDir)) {
List<Path> templates = files.filter(p -> p.toString().endsWith(".ftl")).toList() ;
List<String> templates = files
.map(Path::getFileName)
.map(Path::toString)
.filter(p -> p.length() > TEMPLATE_EXTENSION_LENGTH && p.endsWith(TEMPLATE_EXTENSION)
)
.toList();
ArrayList<Path> out = new ArrayList<>();
for (Path template : templates) {
String configEnding = this.data.config().get("ending") ;
String ending = configEnding != null ? configEnding.trim() : "";
Path outpath = outputDir.resolve(outputFilenameFromTemplate(template.getFileName(), ending));
Template ftl = this.cfg.getTemplate(template.getFileName().toString()) ;
for (String template : templates) {
Path outpath = outputDir.resolve(outputFilenameFromTemplate(template));
Template ftl = this.cfg.getTemplate(template);
try (var outw = Files.newBufferedWriter(outpath, StandardCharsets.UTF_8)) {
ftl.process(this.data, outw);
out.add(outpath);
}
}
return out ;
return out;
}
}
private String outputFilenameFromTemplate(Path template, String ending) {
return template.getFileName().toString().replace(".ftl", ending);
private String outputFilenameFromTemplate(String template) {
return template.substring(0, template.length() - TEMPLATE_EXTENSION_LENGTH);
}
}