Skip to content

Commit

Permalink
[asciidoc] enable links even when no option are set
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Mar 10, 2024
1 parent ac7520a commit ce8db78
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
44 changes: 37 additions & 7 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class Parser {
private static final Pattern UNORDERED_LIST_PREFIX = Pattern.compile("^(?<wildcard>\\*+) .+");
private static final Pattern ATTRIBUTE_DEFINITION = Pattern.compile("^:(?<name>[^\\n\\t:]+):( +(?<value>.+))? *$");
private static final Pattern ATTRIBUTE_VALUE = Pattern.compile("\\{(?<name>[^ }]+)}");
private static final List<String> LINK_PREFIXES = List.of("http://", "https://", "ftp://", "ftps://", "irc://", "file://", "mailto:");

private final Map<String, String> globalAttributes;

Expand Down Expand Up @@ -1435,11 +1436,7 @@ private Element unwrapElementIfPossible(final Paragraph element) {
}

private boolean isLink(final String link) {
return link.startsWith("http://") || link.startsWith("https://") ||
link.startsWith("ftp://") || link.startsWith("ftps://") ||
link.startsWith("irc://") ||
link.startsWith("file://") ||
link.startsWith("mailto:");
return LINK_PREFIXES.stream().anyMatch(link::startsWith);
}

private Map<String, String> doParseOptions(final String options, final String defaultKey, final boolean nestedOptsSupport) {
Expand Down Expand Up @@ -1514,9 +1511,42 @@ private Map<String, String> removeEmptyKey(final Map<String, String> options) {
}

private void flushText(final Collection<Element> elements, final String content) {
if (!content.isEmpty()) {
elements.add(newText(List.of(), content, Map.of()));
if (content.isEmpty()) {
return;
}
int start = 0;
while (start < content.length()) {
final int next = findNextLink(content, start);
if (next < 0) {
elements.add(newText(List.of(), start == 0 ? content : content.substring(start), Map.of()));
break;
}

final int end = Stream.of(" ", "\t")
.mapToInt(s -> content.indexOf(s, next))
.filter(i -> i > next)
.min()
.orElseGet(content::length);

if (start != next) {
elements.add(newText(List.of(), content.substring(start, next), Map.of()));
}

final var link = content.substring(next, end);
elements.add(new Link(link, link, Map.of()));
if (end == content.length()) {
break;
}
start = end;
}
}

private int findNextLink(final String line, final int from) {
return LINK_PREFIXES.stream()
.mapToInt(p -> line.indexOf(p, from))
.filter(i -> i >= from)
.min()
.orElse(-1);
}

private List<Element> flattenTexts(final List<Element> elements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,12 @@ public void visitLink(final Link element) {
builder.append(element.label());
} else {
var label = element.label();
if (label.contains("://") && attr("hide-uri-scheme", element.options()) != null) {
label = label.substring(label.indexOf("://") + "://".length());
if (attr("hide-uri-scheme", element.options()) != null) {
if (label.contains("://")) {
label = label.substring(label.indexOf("://") + "://".length());
} else if (label.contains(":")) { // mailto for ex
label = label.substring(label.indexOf(":") + 1);
}
}
builder.append(escape(label));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@ void links() {
body.children());
}

@Test
void linkNoOpt() {
assertEquals(
List.of(new Link("https://yupiik.io", "https://yupiik.io", Map.of())),
new Parser().parseBody(new Reader(List.of("https://yupiik.io")), null).children());
assertEquals(
List.of(new Paragraph(
List.of(
new Text(List.of(), "in a sentence ", Map.of()),
new Link("https://yupiik.io", "https://yupiik.io", Map.of()),
new Text(List.of(), " and multiple ", Map.of()),
new Link("https://www.yupiik.io", "https://www.yupiik.io", Map.of()),
new Text(List.of(), " links.", Map.of())),
Map.of())),
new Parser().parseBody(new Reader(List.of("in a sentence https://yupiik.io and multiple https://www.yupiik.io links.")), null).children());
}

@Test
void linkInCode() {
final var body = new Parser().parseBody(new Reader(List.of("`https://yupiik.io[Yupiik OSS]`")), null);
Expand Down

0 comments on commit ce8db78

Please sign in to comment.