-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
808d59c
commit e8ed220
Showing
5 changed files
with
233 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
asciidoc-java/src/main/java/io/yupiik/asciidoc/renderer/html/IdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.asciidoc.renderer.html; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import static java.util.Locale.ROOT; | ||
|
||
public final class IdGenerator { | ||
private static final Pattern TAGS = Pattern.compile("<[^>]+>"); | ||
private static final Pattern FORBIDDEN_CHARS = Pattern.compile("[^\\w]+"); | ||
|
||
private IdGenerator() { | ||
// no-op | ||
} | ||
|
||
public static String forTitle(final String title) { | ||
return "_" + FORBIDDEN_CHARS.matcher(TAGS.matcher(title).replaceAll("").toLowerCase(ROOT) | ||
.replace(" ", "_") | ||
.replace("\n", "")) | ||
.replaceAll(""); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
asciidoc-java/src/main/java/io/yupiik/asciidoc/renderer/html/TocVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.asciidoc.renderer.html; | ||
|
||
import io.yupiik.asciidoc.model.Body; | ||
import io.yupiik.asciidoc.model.Element; | ||
import io.yupiik.asciidoc.model.Section; | ||
import io.yupiik.asciidoc.model.Text; | ||
import io.yupiik.asciidoc.renderer.Visitor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import static java.util.Locale.ROOT; | ||
import static java.util.Optional.ofNullable; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class TocVisitor implements Visitor<StringBuilder> { | ||
private final int maxLevel; | ||
private final int currentLevel; | ||
private final Collection<Section> sections = new ArrayList<>(); | ||
|
||
public TocVisitor(final int toclevels, final int currentLevel) { | ||
this.maxLevel = toclevels; | ||
this.currentLevel = currentLevel; | ||
} | ||
|
||
@Override | ||
public void visitSection(final Section element) { | ||
if (element.level() == currentLevel) { | ||
sections.add(element); | ||
} | ||
} | ||
|
||
@Override | ||
public StringBuilder result() { | ||
final var builder = new StringBuilder(); | ||
if (sections.isEmpty()) { | ||
return builder; | ||
} | ||
|
||
builder.append(" <ul class=\"sectlevel").append(currentLevel).append("\">\n"); | ||
if (currentLevel == maxLevel) { | ||
builder.append(sections.stream() | ||
.map(it -> { | ||
final var title = title(it.title()); | ||
return " <li><a href=\"#" + id(it, title) + "\">" + title + "</a></li>"; | ||
}) | ||
.collect(joining("\n", "", "\n"))); | ||
} else { | ||
builder.append(sections.stream() | ||
.map(it -> { | ||
final var tocVisitor = new TocVisitor(maxLevel, currentLevel + 1); | ||
tocVisitor.visitBody(new Body(it.children())); | ||
final var children = tocVisitor.result().toString(); | ||
final var title = title(it.title()); | ||
return " <li><a href=\"#" + id(it, title) + "\">" + title + "</a>\n" + children + " </li>"; | ||
}) | ||
.collect(joining("\n", "", "\n"))); | ||
} | ||
builder.append(" </ul>\n"); | ||
return builder; | ||
} | ||
|
||
private String id(final Section section, final String title) { | ||
return ofNullable(section.options().get("id")) | ||
// todo: better sanitization | ||
.orElseGet(() -> IdGenerator.forTitle(title)); | ||
} | ||
|
||
private String title(final Element title) { | ||
final var titleRenderer = new AsciidoctorLikeHtmlRenderer(new AsciidoctorLikeHtmlRenderer.Configuration()); | ||
titleRenderer.visitElement(title instanceof Text t && t.options().isEmpty() && t.style().isEmpty() ? | ||
new Text(t.style(), t.value(), Map.of("nowrap", "")) : | ||
title); | ||
return titleRenderer.result(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.