1
2
3
4
5
6
7 package com.varmateo.yawg.core;
8
9 import java.io.File;
10 import java.nio.file.Path;
11 import java.util.Optional;
12 import java.util.function.Function;
13
14 import io.vavr.control.Option;
15
16 import com.varmateo.yawg.api.YawgException;
17 import com.varmateo.yawg.spi.PageContext;
18 import com.varmateo.yawg.spi.PageContextBuilder;
19 import com.varmateo.yawg.spi.PageVars;
20 import com.varmateo.yawg.spi.PageVarsBuilder;
21 import com.varmateo.yawg.spi.Template;
22 import com.varmateo.yawg.spi.TemplateService;
23
24
25
26
27
28
29 final class DirPageContextBuilder {
30
31
32 private final Path _targetRootDir;
33 private final TemplateService _templateService;
34 private final String _bakeId;
35
36
37
38
39
40 DirPageContextBuilder(
41 final Path targetRootDir,
42 final TemplateService templateService,
43 final String bakeId) {
44
45 _targetRootDir = targetRootDir;
46 _templateService = templateService;
47 _bakeId = bakeId;
48 }
49
50
51
52
53
54 public PageContext buildPageContext(
55 final Path targetDir,
56 final DirBakeOptions dirBakeOptions,
57 final PageVars extensionVars) {
58
59 final Function<Path, Optional<Template>> templateFetcher =
60 buildTemplateFetcher(dirBakeOptions);
61 final String dirUrl = buildRelativeUrl(targetDir, _targetRootDir);
62 final String rootRelativeUrl = buildRelativeUrl(_targetRootDir, targetDir);
63 final PageVars allPageVars = PageVarsBuilder.create()
64 .addPageVars(dirBakeOptions.pageVars)
65 .addPageVars(dirBakeOptions.pageVarsHere)
66 .addPageVars(extensionVars)
67 .build();
68
69 return PageContextBuilder.create()
70 .dirUrl(dirUrl)
71 .rootRelativeUrl(rootRelativeUrl)
72 .templateFetcher(templateFetcher)
73 .pageVars(allPageVars)
74 .bakeId(_bakeId)
75 .build();
76 }
77
78
79
80
81
82
83 private Function<Path, Optional<Template>> buildTemplateFetcher(
84 final DirBakeOptions dirBakeOptions) {
85
86 final Option<String> templateName = dirBakeOptions.templateName;
87 final TemplateNameMatcher templateNameMatcher = dirBakeOptions.templatesHere;
88 final TemplateService templateService = _templateService;
89
90 return path -> prepareTemplate(
91 path,
92 templateNameMatcher,
93 templateName,
94 templateService)
95 .toJavaOptional();
96 }
97
98
99
100
101
102 private static Option<Template> prepareTemplate(
103 final Path path,
104 final TemplateNameMatcher templateNameMatcher,
105 final Option<String> templateName,
106 final TemplateService templateService) {
107
108 return prepareTemplateForPath(path, templateNameMatcher, templateService)
109 .orElse(() -> prepareDefaultTemplate(templateName, templateService));
110 }
111
112
113
114
115
116 private static Option<Template> prepareTemplateForPath(
117 final Path path,
118 final TemplateNameMatcher templateNameMatcher,
119 final TemplateService templateService) {
120
121 return templateNameMatcher.templateNameFor(path)
122 .flatMap(name -> Option.ofOptional(templateService.prepareTemplate(name)));
123 }
124
125
126
127
128
129 private static Option<Template> prepareDefaultTemplate(
130 final Option<String> templateName,
131 final TemplateService templateService) {
132
133 return templateName.flatMap(
134 name -> Option.ofOptional(templateService.prepareTemplate(name)));
135 }
136
137
138
139
140
141
142 private String buildRelativeUrl(
143 final Path dir,
144 final Path baseDir) {
145
146 final Path relDir = baseDir.relativize(dir);
147 final String relativeUrl = relDir.toString().replace(File.separatorChar, '/');
148
149 return relativeUrl.length() == 0
150 ? "."
151 : relativeUrl;
152 }
153
154
155 }