@@ -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+
6373void 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
199204String replaceTokens (String template , Map <String , String > replacements ) {
0 commit comments