♻️ Use kotlinx.html to create HTML for comic state main
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Oct 2025 16:17:51 +0000 (18:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Oct 2025 16:47:08 +0000 (18:47 +0200)
src/main/java/net/pterodactylus/rhynodge/states/ComicState.java

index 117f5b6..ad4874a 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.rhynodge.states;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -24,17 +25,20 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
-
+import kotlinx.html.FlowContent;
 import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.states.ComicState.Comic;
-
-import com.fasterxml.jackson.annotation.JsonProperty;
-import org.apache.commons.lang3.StringEscapeUtils;
-import org.apache.commons.lang3.StringUtils;
 import org.jspecify.annotations.NonNull;
 import org.jspecify.annotations.Nullable;
 
 import static java.lang.String.format;
+import static kotlinx.html.Gen_consumer_tagsKt.html;
+import static kotlinx.html.Gen_tag_groupsKt.div;
+import static kotlinx.html.Gen_tag_unionsKt.h1;
+import static kotlinx.html.Gen_tag_unionsKt.img;
+import static kotlinx.html.Gen_tags_hKt.body;
+import static kotlinx.html.stream.StreamKt.createHTML;
+import static net.pterodactylus.util.dom.KotlinXHtml.wrapper;
 
 /**
  * {@link net.pterodactylus.rhynodge.State} that can store an arbitrary amout of
@@ -103,7 +107,7 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
                        text.append("Comic Found: ").append(newComic.title()).append("\n\n");
                        for (Strip strip : newComic) {
                                text.append("Image: ").append(strip.imageUrl()).append("\n");
-                               if (!StringUtils.isBlank(strip.comment())) {
+                               if (!strip.comment().isBlank()) {
                                        text.append("Comment: ").append(strip.comment()).append("\n");
                                }
                        }
@@ -116,40 +120,34 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
        @Nullable
        @Override
        protected String htmlText() {
-               StringBuilder html = new StringBuilder();
-               html.append("<body>");
-
                List<Comic> latestComics = new ArrayList<>(comics());
                Collections.reverse(latestComics);
 
-               for (Comic newComic : latestComics.stream().filter(newComics::contains).toList()) {
-                       generateComicHtml(html, newComic);
-               }
-
-               int comicCount = 0;
-               for (Comic comic : latestComics) {
-                       if (newComics.contains(comic)) {
-                               continue;
-                       }
-                       generateComicHtml(html, comic);
-                       if (++comicCount == 7) {
-                               break;
-                       }
-               }
-
-               return html.append("</body>").toString();
+               var tagConsumer = createHTML(false, true);
+               return html(tagConsumer, null, wrapper(html -> {
+                       body(html, null, wrapper(body -> {
+                               for (Comic newComic : latestComics.stream().filter(newComics::contains).toList()) {
+                                       generateComicHtml(body, newComic);
+                               }
+                               for (Comic comic : latestComics.stream().filter(comic -> !newComics.contains(comic)).limit(7).toList()) {
+                                       generateComicHtml(body, comic);
+                               }
+                       }));
+               }));
        }
 
-       private void generateComicHtml(StringBuilder html, Comic comic) {
-               html.append("<div>").append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
-               for (Strip strip : comic) {
-                       html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
-                       html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
-                       html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
-                       html.append("\" /></div>\n");
-                       html.append("<div>").append(StringEscapeUtils.escapeHtml4(strip.comment())).append("</div>\n");
-               }
-               html.append("</div>");
+       private void generateComicHtml(FlowContent flowContent, Comic comic) {
+               div(flowContent, null, wrapper(div -> {
+                       h1(div, null, wrapper(h1 -> h1.text(comic.title())));
+                       for (Strip strip : comic) {
+                               div(div, null, wrapper(imageDiv -> {
+                                       img(imageDiv, strip.comment(), strip.imageUrl(), null, null, wrapper(img ->
+                                                       img.getAttributes().put("title", strip.comment())
+                                       ));
+                               }));
+                               div(div, null, wrapper(commentDiv -> commentDiv.text(strip.comment())));
+                       }
+               }));
        }
 
        /**