Skip to content

Commit 4124e64

Browse files
brunoborgesCopilot
andcommitted
Use Templates record instead of String[] for template storage
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2f8f450 commit 4124e64

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

Generate.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,18 @@ List<String> related() {
6060
}
6161
}
6262

63+
record Templates(String page, String whyCard, String relatedCard, String socialShare) {
64+
static Templates load() throws IOException {
65+
return new Templates(
66+
Files.readString(Path.of("templates/slug-template.html")),
67+
Files.readString(Path.of("templates/why-card.html")),
68+
Files.readString(Path.of("templates/related-card.html")),
69+
Files.readString(Path.of("templates/social-share.html")));
70+
}
71+
}
72+
6373
void main() throws IOException {
64-
var templates = new String[] {
65-
Files.readString(Path.of("templates/slug-template.html")),
66-
Files.readString(Path.of("templates/why-card.html")),
67-
Files.readString(Path.of("templates/related-card.html")),
68-
Files.readString(Path.of("templates/social-share.html"))
69-
};
74+
var templates = Templates.load();
7075
var allSnippets = loadAllSnippets();
7176
IO.println("Loaded %d snippets".formatted(allSnippets.size()));
7277

@@ -176,8 +181,8 @@ String renderSocialShare(String tpl, String slug, String title) {
176181
return replaceTokens(tpl, Map.of("encodedUrl", encodedUrl, "encodedText", encodedText));
177182
}
178183

179-
String generateHtml(String[] tpl, Snippet s, Map<String, Snippet> all) throws IOException {
180-
return replaceTokens(tpl[0], Map.ofEntries(
184+
String generateHtml(Templates tpl, Snippet s, Map<String, Snippet> all) throws IOException {
185+
return replaceTokens(tpl.page(), Map.ofEntries(
181186
Map.entry("title", escape(s.title())), Map.entry("summary", escape(s.summary())),
182187
Map.entry("slug", s.slug()), Map.entry("category", s.category()),
183188
Map.entry("categoryDisplay", s.catDisplay()), Map.entry("difficulty", s.difficulty()),
@@ -191,9 +196,9 @@ String generateHtml(String[] tpl, Snippet s, Map<String, Snippet> all) throws IO
191196
Map.entry("titleJson", jsonEscape(s.title())), Map.entry("summaryJson", jsonEscape(s.summary())),
192197
Map.entry("categoryDisplayJson", jsonEscape(s.catDisplay())),
193198
Map.entry("navArrows", renderNavArrows(s)),
194-
Map.entry("whyCards", renderWhyCards(tpl[1], s.whyModernWins())),
195-
Map.entry("relatedCards", renderRelatedSection(tpl[2], s, all)),
196-
Map.entry("socialShare", renderSocialShare(tpl[3], s.slug(), s.title()))));
199+
Map.entry("whyCards", renderWhyCards(tpl.whyCard(), s.whyModernWins())),
200+
Map.entry("relatedCards", renderRelatedSection(tpl.relatedCard(), s, all)),
201+
Map.entry("socialShare", renderSocialShare(tpl.socialShare(), s.slug(), s.title()))));
197202
}
198203

199204
String replaceTokens(String template, Map<String, String> replacements) {

0 commit comments

Comments
 (0)