♻️ Extract wrapper method for kotlinx.html
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Oct 2025 16:17:14 +0000 (18:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Oct 2025 16:47:07 +0000 (18:47 +0200)
src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java
src/main/java/net/pterodactylus/util/dom/KotlinXHtml.java [new file with mode: 0644]

index 52d6f1a..e266212 100644 (file)
@@ -29,10 +29,6 @@ import java.util.Map;
 import java.util.Set;
 
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.Consumer;
-import kotlin.Unit;
-import kotlin.jvm.functions.Function1;
-import kotlinx.html.Tag;
 import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.State;
 import net.pterodactylus.rhynodge.filters.EpisodeFilter;
@@ -55,6 +51,7 @@ import static kotlinx.html.Gen_tags_tKt.th;
 import static kotlinx.html.Gen_tags_tKt.thead;
 import static kotlinx.html.Gen_tags_tKt.tr;
 import static kotlinx.html.stream.StreamKt.createHTML;
+import static net.pterodactylus.util.dom.KotlinXHtml.wrapper;
 
 /**
  * {@link State} implementation that stores episodes of TV shows, parsed via
@@ -256,13 +253,6 @@ public class EpisodeState extends AbstractState implements Iterable<Episode> {
                return String.format("%s[episodes=%s]", getClass().getSimpleName(), episodes);
        }
 
-       private static <T extends Tag> Function1<T, Unit> wrapper(Consumer<T> tagConsumer) {
-               return t -> {
-                       tagConsumer.accept(t);
-                       return null;
-               };
-       }
-
        /**
         * Stores attributes for an episode.
         *
diff --git a/src/main/java/net/pterodactylus/util/dom/KotlinXHtml.java b/src/main/java/net/pterodactylus/util/dom/KotlinXHtml.java
new file mode 100644 (file)
index 0000000..31f5f18
--- /dev/null
@@ -0,0 +1,41 @@
+package net.pterodactylus.util.dom;
+
+import java.util.function.Consumer;
+import kotlin.Unit;
+import kotlin.jvm.functions.Function1;
+import kotlinx.html.Tag;
+
+/**
+ * Helper class that collects methods for use of {@link kotlinx.html} from
+ * Java.
+ */
+public class KotlinXHtml {
+
+       /**
+        * Can be used to wrap the block body during HTML construction. It
+        * removes the need to construct lambdas that require a
+        * {@code return null;} at the end to satisfy {@link Function1}’s
+        * second type parameter, {@link Unit}.
+        *
+        * <h2>Usage</h2>
+        * <pre>
+        * html(output, null, wrapper(html -> {
+        *     body(html, null, wrapper(body -> {
+        *         // add more HTML
+        *     }));
+        * }));
+        * </pre>
+        *
+        * @param tagConsumer The body to wrap
+        * @param <T> The type of tag
+        * @return A Kotlin {@link Function1} wrapper around the given
+        *        {@code tagConsumer}
+        */
+       public static <T extends Tag> Function1<T, Unit> wrapper(Consumer<T> tagConsumer) {
+               return t -> {
+                       tagConsumer.accept(t);
+                       return null;
+               };
+       }
+
+}