|
7 | 7 |
|
8 | 8 | import static io.jooby.SneakyThrows.throwingConsumer; |
9 | 9 | import static io.jooby.SneakyThrows.throwingFunction; |
| 10 | +import static java.util.function.Predicate.not; |
10 | 11 |
|
11 | 12 | import java.io.File; |
12 | 13 | import java.io.IOException; |
|
23 | 24 | import java.util.List; |
24 | 25 | import java.util.UUID; |
25 | 26 | import java.util.concurrent.atomic.AtomicInteger; |
26 | | -import java.util.stream.Collectors; |
27 | 27 | import java.util.stream.Stream; |
28 | 28 |
|
29 | 29 | import org.apache.commons.io.FileUtils; |
|
37 | 37 | import org.jsoup.nodes.Document; |
38 | 38 | import org.jsoup.nodes.Element; |
39 | 39 |
|
40 | | -import me.tongfei.progressbar.ProgressBar; |
41 | 40 | import me.tongfei.progressbar.ProgressBarBuilder; |
42 | 41 | import me.tongfei.progressbar.ProgressBarStyle; |
43 | 42 |
|
@@ -324,7 +323,7 @@ private static String toJavaName(String tagName) { |
324 | 323 | private static String document(Path index) { |
325 | 324 | try { |
326 | 325 | Document doc = Jsoup.parse(index.toFile(), "UTF-8"); |
327 | | - tocItems(doc); |
| 326 | + headerIds(doc); |
328 | 327 | languageTab(doc); |
329 | 328 | clipboard(doc); |
330 | 329 | externalLink(doc); |
@@ -381,45 +380,43 @@ private static void languageTab(Document doc) { |
381 | 380 | primaryContent.appendTo(primary); |
382 | 381 | secondaryContent.appendTo(primary); |
383 | 382 | secondaryContent.addClass("hidden").addClass("option-2"); |
384 | | - ; |
385 | 383 | } |
386 | 384 | } |
387 | 385 |
|
388 | | - private static void tocItems(Document doc) { |
389 | | - tocItems(doc, 2); |
390 | | - tocItems(doc, 3); |
391 | | - tocItems(doc, 4); |
| 386 | + private static void headerIds(Document doc) { |
| 387 | + headerIds(doc, 2); |
| 388 | + headerIds(doc, 3); |
| 389 | + headerIds(doc, 4); |
| 390 | + headerIds(doc, 5); |
392 | 391 | } |
393 | 392 |
|
394 | | - private static void tocItems(Document doc, int level) { |
395 | | - doc.select("h" + level) |
396 | | - .forEach( |
397 | | - h -> { |
398 | | - if (!h.hasClass("discrete")) { |
399 | | - String id = h.attr("id"); |
400 | | - LinkedHashSet<String> name = new LinkedHashSet<>(); |
401 | | - int parent = level - 1; |
402 | | - Element p = h.parents().select("h" + parent).first(); |
403 | | - if (p != null && !p.hasClass("discrete")) { |
404 | | - String parentId = p.attr("id"); |
405 | | - if (parentId != null && parentId.length() > 0) { |
406 | | - name.add(parentId); |
407 | | - } |
408 | | - } |
409 | | - name.add(id.replaceAll("([a-zA-Z0-9-]+)-\\d+$", "$1")); |
410 | | - String newId = name.stream().collect(Collectors.joining("-")); |
411 | | - if (!id.equals(newId)) { |
412 | | - h.attr("id", newId); |
413 | | - doc.select("a") |
414 | | - .forEach( |
415 | | - a -> { |
416 | | - if (a.attr("href").equals("#" + id) && a.attr("class").length() > 0) { |
417 | | - a.attr("href", "#" + newId); |
418 | | - } |
419 | | - }); |
420 | | - } |
| 393 | + private static void headerIds(Document doc, int level) { |
| 394 | + doc.select("h" + level).stream() |
| 395 | + .filter(not(DocGenerator::isDiscrete)) |
| 396 | + .forEach(h -> { |
| 397 | + String id = h.attr("id"); |
| 398 | + LinkedHashSet<String> name = new LinkedHashSet<>(); |
| 399 | + int parent = level - 1; |
| 400 | + Element p = h.parents().select("h" + parent).first(); |
| 401 | + if (p != null && !isDiscrete(p)) { |
| 402 | + String parentId = p.attr("id"); |
| 403 | + if (!parentId.isEmpty()) { |
| 404 | + name.add(parentId); |
421 | 405 | } |
422 | | - }); |
| 406 | + } |
| 407 | + name.add(id.replaceAll("([a-zA-Z0-9-]+)-\\d+$", "$1")); |
| 408 | + String newId = String.join("-", name); |
| 409 | + if (!id.equals(newId)) { |
| 410 | + h.attr("id", newId); |
| 411 | + h.select("a").stream() |
| 412 | + .filter(a -> a.attr("href").equals("#" + id) && !a.attr("class").isEmpty()) |
| 413 | + .forEach(a -> a.attr("href", "#" + newId)); |
| 414 | + } |
| 415 | + }); |
| 416 | + } |
| 417 | + |
| 418 | + private static boolean isDiscrete(Element p) { |
| 419 | + return p.hasClass("discrete"); |
423 | 420 | } |
424 | 421 |
|
425 | 422 | private static void clipboard(Document doc) { |
@@ -449,8 +446,7 @@ public static Path basedir() { |
449 | 446 | public static String version() { |
450 | 447 | try { |
451 | 448 | Document doc = Jsoup.parse(basedir().getParent().resolve("pom.xml").toFile(), "utf-8"); |
452 | | - String version = doc.selectFirst("version").text().trim(); |
453 | | - return version; |
| 449 | + return doc.selectFirst("version").text().trim(); |
454 | 450 | } catch (IOException x) { |
455 | 451 | throw new IllegalStateException(x); |
456 | 452 | } |
|
0 commit comments