|
| 1 | +package org.infinispan.tutorial.docs; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import org.asciidoctor.Asciidoctor; |
| 16 | +import org.asciidoctor.Options; |
| 17 | +import org.asciidoctor.SafeMode; |
| 18 | +import org.asciidoctor.ast.Document; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 23 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
| 24 | + |
| 25 | +public class GuideMetadataGenerator { |
| 26 | + |
| 27 | + private static final Set<String> SKIP_DIRS = Set.of( |
| 28 | + "target", "docs", "documentation", "docs-maven-plugin", "non-java-clients", ".git", ".github"); |
| 29 | + |
| 30 | + private final Path srcDir; |
| 31 | + private final Path outputDir; |
| 32 | + |
| 33 | + public GuideMetadataGenerator(Path srcDir, Path outputDir) { |
| 34 | + this.srcDir = srcDir; |
| 35 | + this.outputDir = outputDir; |
| 36 | + } |
| 37 | + |
| 38 | + public static void main(String[] args) throws Exception { |
| 39 | + if (args.length < 2) { |
| 40 | + System.err.println("Usage: GuideMetadataGenerator <srcDir> <outputDir>"); |
| 41 | + System.exit(1); |
| 42 | + } |
| 43 | + Path src = Path.of(args[0]); |
| 44 | + Path out = Path.of(args[1]); |
| 45 | + System.out.println("[INFO] Scanning for guides in: " + src); |
| 46 | + GuideMetadataGenerator generator = new GuideMetadataGenerator(src, out); |
| 47 | + generator.generate(); |
| 48 | + System.out.println("[INFO] Guide metadata written to: " + out); |
| 49 | + } |
| 50 | + |
| 51 | + public void generate() throws IOException { |
| 52 | + List<Map<String, Object>> guides = new ArrayList<>(); |
| 53 | + Path guidesOutputDir = outputDir.resolve("guides"); |
| 54 | + Files.createDirectories(guidesOutputDir); |
| 55 | + |
| 56 | + Options options = Options.builder() |
| 57 | + .safe(SafeMode.UNSAFE) |
| 58 | + .build(); |
| 59 | + |
| 60 | + try (Asciidoctor asciidoctor = Asciidoctor.Factory.create()) { |
| 61 | + collectGuides(srcDir, "", asciidoctor, options, guides, guidesOutputDir); |
| 62 | + } |
| 63 | + |
| 64 | + guides.sort(Comparator.comparing(g -> (String) g.get("id"))); |
| 65 | + |
| 66 | + Map<String, Object> index = new LinkedHashMap<>(); |
| 67 | + index.put("guides", guides); |
| 68 | + |
| 69 | + ObjectMapper om = new ObjectMapper( |
| 70 | + new YAMLFactory() |
| 71 | + .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) |
| 72 | + .disable(YAMLGenerator.Feature.SPLIT_LINES) |
| 73 | + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)); |
| 74 | + om.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); |
| 75 | + |
| 76 | + Files.createDirectories(outputDir); |
| 77 | + om.writeValue(outputDir.resolve("index.yaml").toFile(), index); |
| 78 | + } |
| 79 | + |
| 80 | + private void collectGuides(Path dir, String prefix, Asciidoctor asciidoctor, |
| 81 | + Options options, List<Map<String, Object>> guides, Path guidesOutputDir) |
| 82 | + throws IOException { |
| 83 | + try (Stream<Path> entries = Files.list(dir)) { |
| 84 | + List<Path> sorted = entries.sorted().toList(); |
| 85 | + for (Path entry : sorted) { |
| 86 | + if (!Files.isDirectory(entry)) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + String name = entry.getFileName().toString(); |
| 90 | + if (name.startsWith(".") || SKIP_DIRS.contains(name)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + Path guideFile = entry.resolve("guide.adoc"); |
| 94 | + String dirName = stripPrefix(entry.getFileName().toString()); |
| 95 | + if (Files.exists(guideFile)) { |
| 96 | + String id = prefix.isEmpty() ? dirName : prefix + "-" + dirName; |
| 97 | + processGuide(guideFile, id, asciidoctor, options, guides, guidesOutputDir); |
| 98 | + } |
| 99 | + String subPrefix = prefix.isEmpty() ? dirName : prefix + "-" + dirName; |
| 100 | + collectGuides(entry, subPrefix, asciidoctor, options, guides, guidesOutputDir); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private String stripPrefix(String dirName) { |
| 106 | + if (dirName.startsWith("infinispan-")) { |
| 107 | + return dirName.substring("infinispan-".length()); |
| 108 | + } |
| 109 | + return dirName; |
| 110 | + } |
| 111 | + |
| 112 | + private void processGuide(Path guideFile, String id, Asciidoctor asciidoctor, |
| 113 | + Options options, List<Map<String, Object>> guides, Path guidesOutputDir) |
| 114 | + throws IOException { |
| 115 | + String content = Files.readString(guideFile); |
| 116 | + Document doc = asciidoctor.load(content, options); |
| 117 | + |
| 118 | + Map<String, Object> guide = new LinkedHashMap<>(); |
| 119 | + guide.put("id", id); |
| 120 | + guide.put("title", doc.getDoctitle()); |
| 121 | + guide.put("summary", attrString(doc, "summary")); |
| 122 | + guide.put("mode", attrString(doc, "mode")); |
| 123 | + guide.put("topics", splitComma(attrString(doc, "topics"))); |
| 124 | + guide.put("keywords", splitComma(attrString(doc, "keywords"))); |
| 125 | + guide.put("source-dir", attrString(doc, "source-dir")); |
| 126 | + guide.put("file", id + ".adoc"); |
| 127 | + |
| 128 | + String duration = attrString(doc, "duration"); |
| 129 | + if (duration != null) { |
| 130 | + try { |
| 131 | + guide.put("duration", Integer.parseInt(duration)); |
| 132 | + } catch (NumberFormatException e) { |
| 133 | + guide.put("duration", duration); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + guides.add(guide); |
| 138 | + Files.copy(guideFile, guidesOutputDir.resolve(id + ".adoc")); |
| 139 | + } |
| 140 | + |
| 141 | + private String attrString(Document doc, String name) { |
| 142 | + Object val = doc.getAttribute(name); |
| 143 | + return val != null ? val.toString() : null; |
| 144 | + } |
| 145 | + |
| 146 | + private List<String> splitComma(String value) { |
| 147 | + if (value == null || value.isBlank()) { |
| 148 | + return List.of(); |
| 149 | + } |
| 150 | + return Arrays.stream(value.split(",")) |
| 151 | + .map(String::trim) |
| 152 | + .filter(s -> !s.isEmpty()) |
| 153 | + .toList(); |
| 154 | + } |
| 155 | +} |
0 commit comments