, bold, italic, strikethrough, underline, blink, italic}
- *
- * @see Ansi
- * @see Ansi.Color
- * @see Ansi.Attribute
- * @since 1-alpha0
- */
-@SuppressWarnings({ "unused", "JavadocDeclaration", "JavadocBlankLines" })
-public class ShortcodeConverter {
- /**
- * Contains the class instance.
- *
- * @since 1-alpha0
- *
- * -- GETTER --
- * Returns the class instance.
- *
- * @return class instance unless {@link Engine} is uninitialized
- * @since 1-alpha0
- */
- @Getter
- private static ShortcodeConverter instance;
+import java.util.ArrayList;
+import java.util.List;
+/**
+ * The base skeleton for implementing a shortcode parser.
+ *
+ * The following shortcodes are available and can be used:
+ *
+ * - reset
+ * - fg:[black,red,green,yellow,blue,magenta,cyan,white]
+ * - bg:[black,red,green,yellow,blue,magenta,cyan,white]
+ * - bold
+ * - italic
+ * - strikethrough
+ * - underline
+ * - blink
+ * - negative
+ *
+ *
+ * @since 1-alpha1
+ */
+public abstract class ShortcodeParserSkeleton {
/**
* Logger instance.
*
* @see LoggerInstance
- * @since 1-alpha0
+ * @since 1-alpha1
*/
- private final LoggerInstance logger = new LoggerInstance(new LogIssuer(getClass(), CodePart.ENGINE));
+ protected final LoggerInstance logger = new LoggerInstance(new LogIssuer(getClass(), CodePart.ENGINE));
+
+ /**
+ * A list of components the parsed string is made out of.
+ *
+ * @since 1-alpha1
+ */
+ @NotNull
+ @Getter
+ protected final List components;
/**
* Constructs this class.
*
- * @since 1-alpha0
+ * @param string string to parse
+ * @since 1-alpha1
*/
- public ShortcodeConverter() {
- // Only allow one instance
- if (instance == null)
- instance = this;
- else {
- Logger.crash(new LogIssuer(getClass(), CodePart.ENGINE), "Tried reinitializing " + getClass().getName() + " twice");
- }
+ public ShortcodeParserSkeleton(@NotNull String string) {
+ components = parse(string);
}
/**
- * Converts shortcodes into ANSI escape sequences.
+ * Parses an input string and spits out all of it's components.
*
- * @param text text to process
- * @param noErrors prevents printing tag/shortcode errors, overrides the engine configuration setting
- * @return {@link Ansi} sequence
+ * @param string string to parse
+ * @return list of components
* @see EngineConfiguration#errorShortcodeConverter
- * @since 1-alpha0
+ * @since 1-alpha1
*/
@NotNull
- public Ansi process(@NotNull String text, boolean noErrors) {
- Ansi ansi = Ansi.ansi();
+ private List<@NotNull String> parse(@NotNull String string) {
+ List components = new ArrayList<>(); // List of components
boolean tagActive = false; // Indicates that a tag is being parsed
String part = ""; // Current part. May be a tag, may be regular text
// Iterate through every character
- for (char character : text.toCharArray()) {
+ for (char character : string.toCharArray()) {
if (tagActive) {
// A tag is being parsed
if (character == '>') {
@@ -104,15 +102,12 @@ public class ShortcodeConverter {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
try {
- System.out.println("SCC textHash=" + text.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Color.valueOf(part.substring(3).toUpperCase()));
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Color.valueOf(part.substring(3).toUpperCase()));
} catch (IllegalArgumentException ignored) {
- System.out.println("SCC textHash=" + text.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase() + " enum=");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=fg data=" + part.substring(3).toUpperCase() + " enum=");
}
- // Convert text to 'Color' value and insert it on the foreground
- try {
- ansi.fg(Ansi.Color.valueOf(part.substring(3).toUpperCase()));
- } catch (IllegalArgumentException ignored) {}
+ components.add("COLOR:FOREGROUND:" + part.substring(3).toUpperCase());
}
// bg:*
@@ -120,101 +115,96 @@ public class ShortcodeConverter {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
try {
- System.out.println("SCC textHash=" + text.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Color.valueOf(part.substring(3).toUpperCase()));
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Color.valueOf(part.substring(3).toUpperCase()));
} catch (IllegalArgumentException ignored) {
- System.out.println("SCC textHash=" + text.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase() + " enum=");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=bg data=" + part.substring(3).toUpperCase() + " enum=");
}
- // Convert text to 'Color' value and insert it on the background
- try {
- ansi.bg(Ansi.Color.valueOf(part.substring(3).toUpperCase()));
- } catch (IllegalArgumentException ignored) {}
- }
-
- // attr:*
- else if (part.startsWith("attr:")) {
- // Print debug message
- if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- try {
- System.out.println("SCC textHash=" + text.hashCode() + " tag=attr data=" + part.substring(3).toUpperCase() + " enum=" + Ansi.Attribute.valueOf(part.substring(3).toUpperCase()));
- } catch (IllegalArgumentException ignored) {
- System.out.println("SCC textHash=" + text.hashCode() + " tag=attr data=" + part.substring(3).toUpperCase() + " enum=");
- }
-
- // Convert text into 'Attribute' value and insert it
- try {
- ansi.a(Ansi.Attribute.valueOf(part.substring(3).toUpperCase()));
- } catch (IllegalArgumentException ignored) {}
+ components.add("COLOR:BACKGROUND:" + part.substring(3).toUpperCase());
}
// bold
else if (part.equals("bold")) {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- System.out.println("SCC textHash=" + text.hashCode() + " tag=bold");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=bold");
// Insert attribute
- ansi.a(Ansi.Attribute.INTENSITY_BOLD);
+ components.add("ATTRIBUTE:BOLD");
}
// italic
else if (part.equals("italic")) {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- System.out.println("SCC textHash=" + text.hashCode() + " tag=italic");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=italic");
// Insert attribute
- ansi.a(Ansi.Attribute.ITALIC);
+ components.add("ATTRIBUTE:ITALIC");
}
// strikethrough
else if (part.equals("strikethrough")) {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- System.out.println("SCC textHash=" + text.hashCode() + " tag=strikethrough");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=strikethrough");
// Insert attribute
- ansi.a(Ansi.Attribute.STRIKETHROUGH_ON);
+ components.add("ATTRIBUTE:STRIKETHROUGH");
}
// underline
else if (part.equals("underline")) {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- System.out.println("SCC textHash=" + text.hashCode() + " tag=underline");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=underline");
// Insert attribute
- ansi.a(Ansi.Attribute.UNDERLINE);
+ components.add("ATTRIBUTE:UNDERLINE");
}
- // blink
+ // underline
else if (part.equals("blink")) {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- System.out.println("SCC textHash=" + text.hashCode() + " tag=blink");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=blink");
// Insert attribute
- ansi.a(Ansi.Attribute.BLINK_SLOW);
+ components.add("ATTRIBUTE:BLINK");
+ }
+
+ // underline
+ else if (part.equals("negative")) {
+ // Print debug message
+ if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=negative");
+
+ // Insert attribute
+ components.add("ATTRIBUTE:NEGATIVE");
}
// reset
else if (part.equals("reset")) {
// Print debug message
if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
- System.out.println("SCC textHash=" + text.hashCode() + " tag=reset");
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " tag=reset");
// Insert reset
- ansi.reset();
+ components.add("RESET");
}
// error case
else {
+ // Print debug message
+ if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " invalidtag=" + part);
+
// Complain about invalid shortcode
if (EngineConfiguration.getInstance().isErrorShortcodeConverter())
logger.sarn("Invalid shortcode: " + part);
// Convert tag regular text
- ansi.a("<" + part + ">");
+ components.add("TEXT:" + "<" + part + ">");
}
// Empty 'part'
@@ -226,10 +216,15 @@ public class ShortcodeConverter {
} else {
// Regular text is being parsed
if (character == '<') {
- // Tag is starting, insert previous text
- ansi.a(part);
+ if (!part.isEmpty()) {
+ // Tag is starting, insert previous text
+ if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " text=" + part);
+
+ components.add("TEXT:" + part);
+ part = "";
+ }
- part = ""; // Empty 'part'
tagActive = true; // Enable tag processing
} else
// Regular text, add character to 'part'
@@ -239,20 +234,13 @@ public class ShortcodeConverter {
}
// Processing ended, insert leftover text
- ansi.a(part);
+ if (!part.isEmpty()) {
+ if (EngineConfiguration.getInstance().isDebugShortcodeConverter())
+ System.out.println(getClass().getName() + "#" + string.hashCode() + " endtext=" + part);
- return ansi;
- }
+ components.add("TEXT:" + part);
+ }
- /**
- * Converts shortcodes into ANSI escape sequences.
- *
- * @param text text to process
- * @return {@link Ansi} sequence
- * @since 1-alpha0
- */
- @NotNull
- public Ansi process(@NotNull String text) {
- return process(text, false);
+ return components;
}
}
diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/converter/AnsiShortcodeConverter.java b/base/src/main/java/de/staropensource/sosengine/base/utility/converter/AnsiShortcodeConverter.java
new file mode 100644
index 00000000..5373bca1
--- /dev/null
+++ b/base/src/main/java/de/staropensource/sosengine/base/utility/converter/AnsiShortcodeConverter.java
@@ -0,0 +1,80 @@
+/*
+ * STAROPENSOURCE ENGINE SOURCE FILE
+ * Copyright (c) 2024 The StarOpenSource Engine Contributors
+ * Licensed under the GNU Affero General Public License v3
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+package de.staropensource.sosengine.base.utility.converter;
+
+import de.staropensource.sosengine.base.types.ShortcodeParserSkeleton;
+import org.fusesource.jansi.Ansi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Converts shortcodes such as {@code } or {@code