Skip to content

Commit

Permalink
Support new annotation in documentation generators.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky committed Jan 18, 2025
1 parent 4687117 commit 4a9d0a1
Showing 3 changed files with 46 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/main/java/ch/njol/skript/doc/Documentation.java
Original file line number Diff line number Diff line change
@@ -286,7 +286,12 @@ private static void insertSyntaxElement(final PrintWriter pw, final SyntaxElemen
Class<?> elementClass = info.getElementClass();
if (elementClass.getAnnotation(NoDoc.class) != null)
return;
if (elementClass.getAnnotation(Name.class) == null || elementClass.getAnnotation(Description.class) == null || elementClass.getAnnotation(Examples.class) == null || elementClass.getAnnotation(Since.class) == null) {
if (elementClass.getAnnotation(Name.class) == null
|| elementClass.getAnnotation(Description.class) == null
|| (!elementClass.isAnnotationPresent(Examples.class)
&& !elementClass.isAnnotationPresent(Example.class)
&& !elementClass.isAnnotationPresent(Example.Examples.class))
|| elementClass.getAnnotation(Since.class) == null) {
Skript.warning("" + elementClass.getSimpleName() + " is missing information");
return;
}
36 changes: 26 additions & 10 deletions src/main/java/ch/njol/skript/doc/HTMLGenerator.java
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryData;
import org.skriptlang.skript.lang.entry.EntryValidator;
@@ -29,12 +30,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@@ -438,10 +434,7 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));

// Examples
Examples examples = c.getAnnotation(Examples.class);
desc = desc.replace("${element.examples}", Joiner.on("<br>").join(getDefaultIfNullOrEmpty((examples != null ? Documentation.escapeHTML(examples.value()) : null), "Missing examples.")));
desc = desc.replace("${element.examples-safe}", Joiner.on("<br>").join(getDefaultIfNullOrEmpty((examples != null ? Documentation.escapeHTML(examples.value()) : null), "Missing examples."))
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));
desc = extractExamples(desc, c);

// Documentation ID
desc = desc.replace("${element.id}", DocumentationIdProvider.getId(info));
@@ -542,6 +535,29 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu
return desc;
}

private @NotNull String extractExamples(String desc, Class<?> syntax) {
if (syntax.isAnnotationPresent(Example.class)) {
Example examples = syntax.getAnnotation(Example.class);
return this.addExamples(desc, examples.value());
} else if (syntax.isAnnotationPresent(Example.Examples.class)) {
Example.Examples examples = syntax.getAnnotation(Example.Examples.class);
return this.addExamples(desc, Arrays.stream(examples.value())
.map(Example::value).toArray(String[]::new));
} else if (syntax.isAnnotationPresent(Examples.class)) {
Examples examples = syntax.getAnnotation(Examples.class);
return this.addExamples(desc, examples.value());
} else {
return this.addExamples(desc, (String[]) null);
}
}

private @NotNull String addExamples(String desc, String @Nullable ... examples) {
desc = desc.replace("${element.examples}", Joiner.on("<br>").join(getDefaultIfNullOrEmpty((examples != null ? Documentation.escapeHTML(examples) : null), "Missing examples.")));
desc = desc.replace("${element.examples-safe}", Joiner.on("<br>").join(getDefaultIfNullOrEmpty((examples != null ? Documentation.escapeHTML(examples) : null), "Missing examples."))
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));
return desc;
}

private String generateEvent(String descTemp, SkriptEventInfo<?> info, @Nullable String page) {
Class<?> c = info.getElementClass();
String desc;
17 changes: 14 additions & 3 deletions src/main/java/ch/njol/skript/doc/JSONGenerator.java
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.structure.Structure;
import org.skriptlang.skript.lang.structure.StructureInfo;
@@ -21,6 +22,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.stream.Stream;
@@ -39,7 +41,7 @@ public JSONGenerator(File templateDir, File outputDir) {
* @param strings the String array to convert
* @return the JsonArray containing the Strings
*/
private static @Nullable JsonArray convertToJsonArray(String @Nullable [] strings) {
private static @Nullable JsonArray convertToJsonArray(String @Nullable ... strings) {
if (strings == null)
return null;
JsonArray jsonArray = new JsonArray();
@@ -73,9 +75,18 @@ public JSONGenerator(File templateDir, File outputDir) {
syntaxJsonObject.add("description", new JsonArray());
}

Examples examplesAnnotation = syntaxClass.getAnnotation(Examples.class);
if (examplesAnnotation != null) {
if (syntaxClass.isAnnotationPresent(Examples.class)) {
@NotNull Examples examplesAnnotation = syntaxClass.getAnnotation(Examples.class);
syntaxJsonObject.add("examples", convertToJsonArray(examplesAnnotation.value()));
} else if (syntaxClass.isAnnotationPresent(Example.Examples.class)) {
// If there are multiple examples, they get containerised
@NotNull Example.Examples examplesAnnotation = syntaxClass.getAnnotation(Example.Examples.class);
syntaxJsonObject.add("examples", convertToJsonArray(Arrays.stream(examplesAnnotation.value())
.map(Example::value).toArray(String[]::new)));
} else if (syntaxClass.isAnnotationPresent(Example.class)) {
// If the user adds just one example, it isn't containerised
@NotNull Example example = syntaxClass.getAnnotation(Example.class);
syntaxJsonObject.add("examples", convertToJsonArray(example.value()));
} else {
syntaxJsonObject.add("examples", new JsonArray());
}

0 comments on commit 4a9d0a1

Please sign in to comment.