Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VitePress generation option, set as default #140

Merged
merged 18 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,15 @@ enum Format {
/**
* A box which is fully expanded at all times.
*/
STANDARD("!!!"),
STANDARD,
/**
* A box which can be toggled between only rendering the title and the full text. Defaults to the collapsed form (only the title visible).
*/
COLLAPSED("???"),
COLLAPSED,
/**
* A box which can be toggled between only rendering the title and the full text. Defaults to the expanded form (both title and text visible).
*/
EXPANDED("???+");

private final String type;

Format(String type) {
this.type = type;
}

public String toString() {
return type;
}
EXPANDED;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cleanroommc.groovyscript.documentation;

import com.cleanroommc.groovyscript.api.documentation.annotations.Admonition;
import com.cleanroommc.groovyscript.documentation.format.IFormat;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
Expand All @@ -10,11 +11,11 @@
public class AdmonitionBuilder {

private final List<String> note = new ArrayList<>();
private Admonition.Type type = Admonition.Type.NOTE;
private Admonition.Type admonitionType = Admonition.Type.NOTE;
private String title = "";
private boolean hasTitle;
private int indentation;
private Admonition.Format format = Admonition.Format.EXPANDED;
private Admonition.Format admonitionFormat = Admonition.Format.EXPANDED;

public AdmonitionBuilder note(String note) {
this.note.add(note);
Expand All @@ -32,7 +33,7 @@ public AdmonitionBuilder note(List<String> note) {
}

public AdmonitionBuilder type(Admonition.Type type) {
this.type = type;
this.admonitionType = type;
return this;
}

Expand All @@ -52,25 +53,32 @@ public AdmonitionBuilder indentation(int indentation) {
}

public AdmonitionBuilder format(Admonition.Format format) {
this.format = format;
this.admonitionFormat = format;
return this;
}

public String generate() {
return generate(Documentation.DEFAULT_FORMAT);
}

public String generate(IFormat format) {
StringBuilder out = new StringBuilder();
String indent = StringUtils.repeat(" ", indentation);

out.append(indent).append(format);
out.append(" ").append(type);
if (hasTitle) out.append(" \"").append(title).append("\"");
out.append(format.admonitionStart(admonitionFormat, admonitionType, indentation, hasTitle ? title : ""));
out.append("\n");

for (int i = 0; i < note.size(); i++) {
String line = note.get(i);
if (!line.trim().isEmpty()) out.append(indent).append(" ").append(line);
if (!line.trim().isEmpty()) {
if (format.allowsIndentation()) out.append(indent).append(" ");
out.append(line);
}
if (i < note.size() - 1) out.append("\n");
}

out.append(indent).append(format.admonitionEnd(admonitionFormat, indentation));

return out.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,19 @@ public class Builder {
private final Map<String, FieldDocumentation> fields;
private final Map<String, List<RecipeBuilderMethod>> methods;
private final List<Method> registrationMethods;
private final Collection<String> missingLangKeys;

public Builder(GroovyContainer<? extends ModPropertyContainer> mod, Method builderMethod, String reference, String baseTranslationKey, Collection<String> missingLangKeys) {
public Builder(GroovyContainer<? extends ModPropertyContainer> mod, Method builderMethod, String reference, String baseTranslationKey) {
this.mod = mod;
this.builderMethod = builderMethod;
this.reference = reference;
this.annotation = builderMethod.getAnnotation(RecipeBuilderDescription.class);
this.missingLangKeys = missingLangKeys;
Class<?> builderClass = builderMethod.getReturnType();
this.fields = gatherFields(builderClass, annotation, baseTranslationKey, missingLangKeys);
this.fields = gatherFields(builderClass, annotation, baseTranslationKey);
this.methods = gatherMethods(builderClass, fields);
this.registrationMethods = gatherRegistrationMethods(builderClass);
}

public String lang(String key) {
if (!I18n.hasKey(key)) {
this.missingLangKeys.add(key);
return key;
}
return I18n.format(key);
}

private static Map<String, FieldDocumentation> gatherFields(Class<?> builderClass, RecipeBuilderDescription annotation, String langLocation, Collection<String> missingLangKeys) {
private static Map<String, FieldDocumentation> gatherFields(Class<?> builderClass, RecipeBuilderDescription annotation, String langLocation) {
Map<String, FieldDocumentation> fields = new HashMap<>();
List<Field> allFields = getAllFields(builderClass);
for (Field field : allFields) {
Expand All @@ -78,7 +68,7 @@ private static Map<String, FieldDocumentation> gatherFields(Class<?> builderClas
.collect(Collectors.toList());

if (!annotations.isEmpty()) {
fields.putIfAbsent(field.getName(), new FieldDocumentation(field, annotations, langLocation, missingLangKeys));
fields.putIfAbsent(field.getName(), new FieldDocumentation(field, annotations, langLocation));
}
}

Expand Down Expand Up @@ -203,11 +193,29 @@ else if (!req.isEmpty() && current == req.get(req.size() - 1)) {
return parts;
}

@NotNull
private static List<String> getOutputs(List<String> parts) {
List<String> output = new ArrayList<>();
for (int i = 0; i < parts.size(); i++) {
String part = parts.get(i);
if (!part.isEmpty()) {
int indent = 4;
if (!output.isEmpty()) {
int lastIndex = output.get(i - 1).indexOf(part.charAt(0));
if (lastIndex != -1) indent = lastIndex;
}
output.add(StringUtils.repeat(" ", indent) + part.trim() + "\n");
}
}
return output;
}

public String builderAdmonition() {
if (annotation.example().length == 0) return "";
return new AdmonitionBuilder()
.note(new CodeBlockBuilder().line(builder().split("\n")).annotation(annotations()).generate())
.type(Admonition.Type.EXAMPLE)
.indentation(1)
.generate();
}

Expand All @@ -228,7 +236,7 @@ public String builder() {
}

public List<String> annotations() {
return Arrays.stream(annotation.example()).flatMap(example -> Arrays.stream(example.annotations())).map(this::lang).collect(Collectors.toList());
return Arrays.stream(annotation.example()).flatMap(example -> Arrays.stream(example.annotations())).map(Documentation::translate).collect(Collectors.toList());
}

public String documentMethods() {
Expand All @@ -246,7 +254,7 @@ public String documentMethods() {
}

if (fieldDocumentation.hasRequirement()) {
out.append(" ").append(I18n.format("groovyscript.wiki.requires", lang(fieldDocumentation.getRequirement())));
out.append(" ").append(I18n.format("groovyscript.wiki.requires", fieldDocumentation.getRequirement()));
}

if (fieldDocumentation.hasDefaultValue()) {
Expand Down Expand Up @@ -291,46 +299,18 @@ private String createBuilder(Example example) {
return out.toString();
}

@NotNull
private static List<String> getOutputs(List<String> parts) {
List<String> output = new ArrayList<>();
for (int i = 0; i < parts.size(); i++) {
String part = parts.get(i);
if (!part.isEmpty()) {
int indent = 4;
if (!output.isEmpty()) {
int lastIndex = output.get(i - 1).indexOf(part.charAt(0));
if (lastIndex != -1) indent = lastIndex;
}
output.add(StringUtils.repeat(" ", indent) + part.trim() + "\n");
}
}
return output;
}


private static class FieldDocumentation implements Comparable<FieldDocumentation> {

private final Field field;
private final String langLocation;
private final List<Property> annotations;
private final Property firstAnnotation;
private final Collection<String> missingLangKeys;

public FieldDocumentation(Field field, List<Property> annotations, String langLocation, Collection<String> missingLangKeys) {
public FieldDocumentation(Field field, List<Property> annotations, String langLocation) {
this.field = field;
this.langLocation = langLocation;
this.annotations = annotations;
this.firstAnnotation = annotations.get(0);
this.missingLangKeys = missingLangKeys;
}

public String lang(String key, Object... args) {
if (!I18n.hasKey(key)) {
this.missingLangKeys.add(key);
return key;
}
return I18n.format(key, args);
}

public Field getField() {
Expand Down Expand Up @@ -378,7 +358,7 @@ public String getComparison() {
if (!comparison.isPresent()) return "";
return Arrays.stream(comparison.get())
.sorted((left, right) -> ComparisonChain.start().compare(left.type(), right.type()).result())
.map(x -> lang(x.type().getKey(), x.value()))
.map(x -> Documentation.translate(x.type().getKey(), x.value()))
.collect(Collectors.joining(String.format(" %s ", I18n.format("groovyscript.wiki.and"))));
}

Expand All @@ -391,7 +371,7 @@ public String getRequirement() {
.map(Property::requirement)
.filter(x -> !x.isEmpty())
.findFirst()
.map(this::lang)
.map(Documentation::translate)
.orElse("");
}

Expand All @@ -404,11 +384,12 @@ public boolean isUsed() {
}

private String getFieldTypeInlineCode() {
return "`#!groovy " + Exporter.simpleSignature(getField().getAnnotatedType().getType().getTypeName()) + "`. ";
//return "`#!groovy " + Exporter.simpleSignature(getField().getAnnotatedType().getType().getTypeName()) + "`. ";
return "`" + Exporter.simpleSignature(getField().getAnnotatedType().getType().getTypeName()) + "`. ";
}

public String getDescription() {
return "- " + getFieldTypeInlineCode() + lang(getLangKey()) + ".";
return "- " + getFieldTypeInlineCode() + Documentation.translate(getLangKey()) + ".";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.groovyscript.documentation;

import com.cleanroommc.groovyscript.documentation.format.IFormat;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
Expand All @@ -12,6 +13,7 @@ public class CodeBlockBuilder {
private final List<String> lines = new ArrayList<>();
private final List<String> annotations = new ArrayList<>();
private final List<String> highlight = new ArrayList<>();
private final List<Integer> focus = new ArrayList<>();
private String lang = "groovy";
private int indentation;

Expand Down Expand Up @@ -55,36 +57,59 @@ public CodeBlockBuilder highlight(String highlight) {
return this;
}

public CodeBlockBuilder focus(Integer focus) {
this.focus.add(focus);
return this;
}

public CodeBlockBuilder indentation(int indentation) {
this.indentation = indentation;
return this;
}

public List<String> generate() {
return generate(Documentation.DEFAULT_FORMAT);
}

public List<String> generate(IFormat format) {
List<String> out = new ArrayList<>();
String indent = StringUtils.repeat(" ", indentation);

String hl_lines = highlight.isEmpty() ? "" : (" hl_lines=\"" + String.join(" ", highlight) + "\"");

out.add(indent + "```" + lang + hl_lines);
out.add(indent + "```" + lang + format.codeBlockHighlights(highlight));
for (String line : lines) out.add(indent + line);
out.add(indent + "```");

if (!annotations.isEmpty()) out.add("");

int i = 0;
for (int x = 0; x < out.size(); x++) {
Matcher matcher = Documentation.ANNOTATION_COMMENT_LOCATION.matcher(out.get(x));
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, String.format("/*(%d)$1*/", 1 + i));
out.add(String.format("%s%d. %s", indent, 1 + i, annotations.get(i)));
i++;
if (Documentation.DEFAULT_FORMAT.usesFocusInCodeBlocks()) {
int i = 0;
for (int x = 0; x < out.size(); x++) {
Matcher matcher = Documentation.ANNOTATION_COMMENT_LOCATION.matcher(out.get(x));
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, String.format("/* %s */", annotations.get(i)));
i++;
}
matcher.appendTail(sb);
if (focus.contains(x)) out.set(x, sb + " // [!code focus]");
else out.set(x, sb.toString());
}
} else {
int i = 0;
for (int x = 0; x < out.size(); x++) {
Matcher matcher = Documentation.ANNOTATION_COMMENT_LOCATION.matcher(out.get(x));
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, String.format("/*(%d)$1*/", 1 + i));
out.add(String.format("%s%d. %s", indent, 1 + i, annotations.get(i)));
i++;
}
matcher.appendTail(sb);
out.set(x, sb.toString());
}
matcher.appendTail(sb);
out.set(x, sb.toString());
}


out.add("\n");

return out;
Expand Down
Loading