Skip to content

Commit

Permalink
[asciidoc] workaround for legacy anchors - only inline in beginning o…
Browse files Browse the repository at this point in the history
…r end of line support for now
  • Loading branch information
rmannibucau committed Feb 14, 2024
1 parent 7b66691 commit d10d46e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
26 changes: 24 additions & 2 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ private <T extends Element> T parseList(final Reader reader, final String option
final var buffer = new StringBuilder();
Matcher matcher;
final int currentLevel = prefix.length() - 1 /*ending space*/;
while ((next = reader.nextLine()) != null && (matcher = regex.matcher((nextStripped=next.strip()))).matches() && !next.isBlank()) {
while ((next = reader.nextLine()) != null && (matcher = regex.matcher((nextStripped = next.strip()))).matches() && !next.isBlank()) {
final var level = matcher.group(captureName).length();
if (level < currentLevel) { // go back to parent
break;
Expand Down Expand Up @@ -1211,7 +1211,29 @@ private void addTextElements(final String line, final int i, final int end,
}

private Element newText(final List<Text.Style> styles, final String value, final Map<String, String> options) {
return new Text(styles, value, options); // todo: check nested links, email - without escaping
// cheap handling of legacy syntax for anchors - for backward compat but only when at the beginning or end, not yet in the middle
String id = null;
String text = value;
if (value.startsWith("[[")) {
final int end = value.indexOf("]]");
if (end > 0) {
id = value.substring("[[".length(), end);
text = text.substring(end + "]]".length()).strip();
}
} else if (value.endsWith("]]")) {
final int start = value.lastIndexOf("[[");
if (start > 0) {
id = value.substring(start + "[[".length(), value.length() - "]]".length());
text = text.substring(0, start).strip();
}
}

if (id != null && !id.isBlank() && !options.containsKey("id")) {
final var opts = new HashMap<>(options);
opts.putIfAbsent("id", id);
return new Text(styles, text, opts);
}
return new Text(styles, text, options); // todo: check nested links, email - without escaping
}

private Map<String, String> parseOptions(final String options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,22 @@ void anchor() {
body.children());
}

@Test
void titleId() {
final var body = new Parser().parseBody(new Reader(List.of("""
== Create a configuration model [[configuration_model]]
A configuration model is a record marked with RootConfiguration.
""".split("\n"))), null);
assertEquals(
List.of(new Section(
2,
new Text(List.of(), "Create a configuration model", Map.of("id", "configuration_model")),
List.of(new Text(List.of(), "A configuration model is a record marked with RootConfiguration.", Map.of())),
Map.of())),
body.children());
}

@Test
void include() {
final var body = new Parser().parseBody(
Expand Down

0 comments on commit d10d46e

Please sign in to comment.