Skip to content

Commit 63c6f38

Browse files
committed
fix: Order features by title
Fixes #21
1 parent 1290cf4 commit 63c6f38

File tree

1 file changed

+49
-19
lines changed

1 file changed

+49
-19
lines changed

.github/scripts/generate_navigation.java

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
1010
import java.nio.file.attribute.BasicFileAttributes;
11+
import java.util.Arrays;
1112
import java.util.Comparator;
1213
import java.util.LinkedHashMap;
1314
import java.util.LinkedHashSet;
1415
import java.util.Map;
1516
import java.util.Optional;
1617
import java.util.Set;
1718
import java.util.TreeMap;
19+
import java.util.TreeSet;
1820
import java.util.stream.Stream;
1921

2022
import static java.lang.System.lineSeparator;
@@ -34,8 +36,8 @@ public static void main(String... args) throws Exception {
3436
var versionsPath = Paths.get(pwd + "/docs/modules/versions").normalize().toAbsolutePath();
3537
var partialsPath = Paths.get(pwd + "/docs/modules/features/partials").normalize().toAbsolutePath();
3638

37-
var categoriesMap = new TreeMap<String, Map<String, Path>>();
38-
var versionsMap = new TreeMap<String, Map<String, Path>>();
39+
var categoriesMap = new TreeMap<String, Set<Feature>>();
40+
var versionsMap = new TreeMap<String, Set<Feature>>();
3941

4042
FeatureFinder featureFinder = new FeatureFinder();
4143
Files.walkFileTree(featuresPath, featureFinder);
@@ -54,21 +56,19 @@ public static void main(String... args) throws Exception {
5456
var features = new LinkedHashSet<Feature>();
5557
for (Map.Entry<String, Path> e : featureFinder.features.entrySet()) {
5658
var page = e.getKey();
57-
var feature = e.getValue();
59+
var f = e.getValue();
5860

5961
System.out.printf("➡️ Processing %s%n", page);
60-
var version = extractAttribute(feature, ":database-version:");
61-
var categories = extractAttribute(feature, ":database-category:").split(" ");
62+
var version = extractAttribute(f, ":database-version:");
63+
var categories = extractAttribute(f, ":database-category:").split(" ");
64+
var feature = new Feature(f, version, categories);
65+
features.add(feature);
6266

6367
for (String category : categories) {
64-
categoriesMap.computeIfAbsent(category, c -> new TreeMap<>())
65-
.put(page, feature);
68+
categoriesMap.computeIfAbsent(category, c -> new TreeSet<>()).add(feature);
6669
}
6770

68-
versionsMap.computeIfAbsent(version, v -> new TreeMap<>())
69-
.put(page, feature);
70-
71-
features.add(new Feature(feature, version, categories));
71+
versionsMap.computeIfAbsent(version, v -> new TreeSet<>()).add(feature);
7272
}
7373

7474
System.out.printf("🛠 Generating pages%n");
@@ -77,11 +77,11 @@ public static void main(String... args) throws Exception {
7777
copyFeatures(features, partialsPath);
7878
}
7979

80-
private static void generateNavigation(Path path, Map<String, Map<String, Path>> data) throws IOException {
80+
private static void generateNavigation(Path path, Map<String, Set<Feature>> data) throws IOException {
8181
StringBuilder b = new StringBuilder("* xref:index.adoc[]");
8282
b.append(lineSeparator());
8383

84-
for (Map.Entry<String, Map<String, Path>> e : data.entrySet()) {
84+
for (Map.Entry<String, Set<Feature>> e : data.entrySet()) {
8585
var k = e.getKey();
8686
var v = e.getValue();
8787

@@ -95,9 +95,8 @@ private static void generateNavigation(Path path, Map<String, Map<String, Path>>
9595

9696
b.append("** xref:" + k + "/index.adoc[]")
9797
.append(lineSeparator());
98-
for (Map.Entry<String, Path> g : v.entrySet()) {
99-
var f = g.getKey();
100-
var p = g.getValue();
98+
for (Feature feature: v) {
99+
var f = feature.path.getFileName().toString();
101100

102101
b.append("*** xref:" + k + "/" + f + "[]")
103102
.append(lineSeparator());
@@ -111,7 +110,7 @@ private static void generateNavigation(Path path, Map<String, Map<String, Path>>
111110
Files.write(path.resolve("nav.adoc"), b.toString().getBytes(UTF_8));
112111
}
113112

114-
private static void copyFeatures(LinkedHashSet<Feature> features, Path partials) throws IOException {
113+
private static void copyFeatures(Set<Feature> features, Path partials) throws IOException {
115114
deleteFiles(partials);
116115
Files.createDirectories(partials);
117116

@@ -135,6 +134,20 @@ private static String extractAttribute(Path file, String attributeName) throws I
135134
return line.get().substring(attributeName.length() + 1).trim();
136135
}
137136

137+
private static String extractTitle(Path file) throws IOException {
138+
Optional<String> line = Files.lines(file)
139+
.map(String::trim)
140+
.filter(s -> s.startsWith("= "))
141+
.findFirst();
142+
143+
if (line.isEmpty()) {
144+
System.err.printf("❌ Missing title in %s%n", file.toAbsolutePath());
145+
System.exit(1);
146+
}
147+
148+
return line.get().substring(2).trim();
149+
}
150+
138151
private static void deleteFiles(Path path) throws IOException {
139152
if (Files.exists(path)) {
140153
try (Stream<Path> stream = Files.walk(path)) {
@@ -182,15 +195,17 @@ public FileVisitResult visitFileFailed(Path file, IOException e) {
182195
}
183196
}
184197

185-
private static class Feature {
198+
private static class Feature implements Comparable<Feature> {
186199
private final Path path;
187200
private final String version;
188201
private final String[] categories;
202+
private final String title;
189203

190-
private Feature(Path path, String version, String[] categories) {
204+
private Feature(Path path, String version, String[] categories) throws IOException {
191205
this.path = path;
192206
this.version = version;
193207
this.categories = categories;
208+
this.title = extractTitle(path);
194209
}
195210

196211
private String asSummary() {
@@ -215,5 +230,20 @@ private String asSummary() {
215230

216231
return b.toString();
217232
}
233+
234+
@Override
235+
public int compareTo(Feature o) {
236+
return title.compareTo(o.title);
237+
}
238+
239+
@Override
240+
public String toString() {
241+
return "Feature{" +
242+
"path=" + path +
243+
", version='" + version + '\'' +
244+
", title='" + title + '\'' +
245+
", categories=" + Arrays.toString(categories) +
246+
'}';
247+
}
218248
}
219249
}

0 commit comments

Comments
 (0)