Skip to content

Commit 7f2cfa0

Browse files
committed
update doctlet to support static fields
1 parent b1b6ace commit 7f2cfa0

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

docs/templates/class.md.hbs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ hide: true
4040
{{/each}}
4141
{{/if}}
4242

43+
{{#if fields}}
44+
## Static Fields
45+
46+
| Name | Type | Description |
47+
|------|------|-------------|
48+
{{#each fields}}
49+
| `{{ name }}` | {{ type }} | {{ description }} |
50+
{{/each}}
51+
{{/if}}
52+

src/main/java/org/openpatch/scratch/KeyCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* <p>The constants in this class are intended to be used for keyboard event handling and key
3232
* mapping operations.
33+
*
3334
*/
3435
public final class KeyCode {
3536
/* Virtual key codes. */

src/tools/java/doclets/Scratch4JDoclet.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private void generatePackageIndex(String packageName, List<TypeElement> classes,
225225
}
226226
}
227227
}
228-
json.put("description", convertToMarkdown(description, packageName).trim());
228+
json.put("description", convertToMarkdown(description, packageName).replace("\n", " ").replace("\r", " ").trim());
229229
if (indexInDocs != null) {
230230
json.put("index", indexInDocs);
231231
}
@@ -320,7 +320,7 @@ private void generateClassIndex(TypeElement classElement, List<ExecutableElement
320320
}
321321

322322
json.put("scratchblock", customTags.getOrDefault("scratchblock", ""));
323-
json.put("description", convertToMarkdown(description, currentPackage).trim());
323+
json.put("description", convertToMarkdown(description, currentPackage).replace("\n", " ").replace("\r", " ").trim());
324324
if (indexInDocs != null) {
325325
json.put("index", indexInDocs);
326326
}
@@ -335,6 +335,31 @@ private void generateClassIndex(TypeElement classElement, List<ExecutableElement
335335
// Examples - support multiple examples
336336
json.put("examples", buildExamples(customTags));
337337

338+
// --- Add public static fields under "fields" ---
339+
List<Map<String, Object>> fields = new ArrayList<>();
340+
for (Element member : classElement.getEnclosedElements()) {
341+
if (member.getKind() == ElementKind.FIELD) {
342+
Set<Modifier> modifiers = member.getModifiers();
343+
if (modifiers.contains(Modifier.PUBLIC) && modifiers.contains(Modifier.STATIC)) {
344+
Map<String, Object> fieldJson = new LinkedHashMap<>();
345+
fieldJson.put("name", member.getSimpleName().toString());
346+
fieldJson.put("type", simplifyType(member.asType().toString()));
347+
// Add field Javadoc if present
348+
DocCommentTree fieldDoc = env.getDocTrees().getDocCommentTree(member);
349+
String fieldDesc = "";
350+
if (fieldDoc != null) {
351+
fieldDesc = convertToMarkdown(extractDescription(fieldDoc, currentPackage), currentPackage)
352+
.replace("\n", " ").replace("\r", " ").trim();
353+
}
354+
fieldJson.put("description", fieldDesc);
355+
fields.add(fieldJson);
356+
}
357+
}
358+
}
359+
if (!fields.isEmpty()) {
360+
json.put("fields", fields);
361+
}
362+
338363
writeJsonToFile(json, classDir.resolve("index.md.json"));
339364
}
340365

@@ -412,7 +437,7 @@ private void generateMethodJson(List<ExecutableElement> methods, String classNam
412437
}
413438
json.put("scratchblock", customTags.getOrDefault("scratchblock", ""));
414439
json.put("class", className);
415-
json.put("description", finalDescription.trim());
440+
json.put("description", finalDescription.replace("\n", " ").replace("\r", " ").trim());
416441
if (indexInDocs != null) {
417442
json.put("index", indexInDocs);
418443
}
@@ -584,7 +609,7 @@ private String extractDescriptionRaw(DocCommentTree docComment) {
584609
*/
585610
private String convertToMarkdown(String html, String currentPackage) {
586611
// Convert <pre>{@code ...}</pre> to markdown code blocks
587-
// Pattern matches: <pre>\s*{@code\s* ... }\s*</pre>
612+
// Pattern matches: <pre>\s*\\{@code\s* ... }\s*</pre>
588613
String result = html.replaceAll(
589614
"(?s)<pre>\\s*\\{@code\\s*(.*?)\\s*\\}\\s*</pre>",
590615
"\n```java\n$1\n```\n");

0 commit comments

Comments
 (0)