Skip to content

Commit ecc0002

Browse files
Fix Docs Example Formatting (#7992)
1 parent 23c7650 commit ecc0002

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/main/java/ch/njol/skript/doc/HTMLGenerator.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,23 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu
550550
return this.addExamples(desc, examples.value());
551551
} else if (syntax.isAnnotationPresent(Example.Examples.class)) {
552552
Example.Examples examples = syntax.getAnnotation(Example.Examples.class);
553-
return this.addExamples(desc, Arrays.stream(examples.value())
554-
.map(Example::value).toArray(String[]::new));
553+
Example[] examplesArray = examples.value();
554+
// we map the examples into a new array
555+
// [example1, "", example2, "", example3]
556+
// blank strings are a hacky way to insert spacing between the examples
557+
String[] mappedExamples = new String[examplesArray.length * 2 - 1];
558+
for (int i = 0; i < mappedExamples.length; i++) {
559+
if (i % 2 == 0) {
560+
String example = examplesArray[i / 2].value();
561+
if (example.endsWith("\n")) {
562+
example = example.substring(0, example.length() - "\n".length());
563+
}
564+
mappedExamples[i] = example;
565+
} else {
566+
mappedExamples[i] = "";
567+
}
568+
}
569+
return this.addExamples(desc, mappedExamples);
555570
} else if (syntax.isAnnotationPresent(Examples.class)) {
556571
Examples examples = syntax.getAnnotation(Examples.class);
557572
return this.addExamples(desc, examples.value());
@@ -561,9 +576,19 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu
561576
}
562577

563578
private @NotNull String addExamples(String desc, String @Nullable ... examples) {
564-
desc = desc.replace("${element.examples}", Joiner.on("<br>").join(getDefaultIfNullOrEmpty((examples != null ? Documentation.escapeHTML(examples) : null), "Missing examples.")));
565-
desc = desc.replace("${element.examples-safe}", Joiner.on("<br>").join(getDefaultIfNullOrEmpty((examples != null ? Documentation.escapeHTML(examples) : null), "Missing examples."))
566-
.replace("\\", "\\\\").replace("\"", "\\\"").replace("\t", " "));
579+
if (examples != null) {
580+
// sanitize
581+
Documentation.escapeHTML(examples);
582+
// replace newlines
583+
for (int i = 0; i < examples.length; i++) {
584+
examples[i] = examples[i].replace("\n", "<br>");
585+
}
586+
}
587+
588+
String mergedExamples = Joiner.on("<br>").join(getDefaultIfNullOrEmpty(examples, "Missing examples."));
589+
desc = desc.replace("${element.examples}", mergedExamples)
590+
.replace("${element.examples-safe}", mergedExamples);
591+
567592
return desc;
568593
}
569594

0 commit comments

Comments
 (0)