Skip to content

Commit 89f9333

Browse files
committed
[adoc] enable table last cell to be multiple in inline rows + drop legacy passthorugh symbols in macro (51188) as well as macro quoting surronding (__) since it is not in the theme for now
1 parent ba55df3 commit 89f9333

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

asciidoc-java/src/main/java/io/yupiik/asciidoc/parser/Parser.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,26 @@ private Table parseTable(final Path enclosingDocument, final Reader reader,
500500
while (!Objects.equals(token, next = reader.skipCommentsAndEmptyLines()) && next != null) {
501501
next = next.strip();
502502
final var cells = new ArrayList<Element>();
503+
if (!next.startsWith("|") && !rows.isEmpty() && !rows.get(rows.size() - 1).isEmpty()) {
504+
final var lastRow = rows.get(rows.size() - 1);
505+
final var line = cellParser.size() > lastRow.size() - 1
506+
? cellParser.get(lastRow.size() - 1).apply(List.of(next))
507+
: new Text(List.of(), next, Map.of());
508+
final var last = lastRow.get(lastRow.size() - 1);
509+
final Element replacement;
510+
if (last instanceof Text lt && line instanceof Text le) {
511+
replacement = mergeTexts(List.of(lt, le));
512+
} else if (last instanceof Paragraph lt && line instanceof Paragraph le) {
513+
replacement = new Paragraph(Stream.concat(lt.children().stream(), le.children().stream()).toList(), lt.options());
514+
} else if (last instanceof Paragraph lp) {
515+
replacement = new Paragraph(Stream.concat(lp.children().stream(), Stream.of(line)).toList(), lp.options());
516+
} else {
517+
replacement = unwrapElementIfPossible(new Paragraph(List.of(last, line), Map.of("nowrap", "true")));
518+
}
519+
lastRow.set(lastRow.size() - 1, replacement);
520+
continue;
521+
}
522+
503523
if (next.indexOf("|", 2) > 0) { // single line row
504524
int cellIdx = 0;
505525
int last = 1; // line starts with '|'
@@ -854,13 +874,19 @@ private List<Element> parseLine(final Path enclosingDocument, final Reader reade
854874
backward = -1;
855875
}
856876

877+
var offset = 0;
857878
if (backward >= 0 && backward < i) { // start by assuming it a link then fallback on a macro
858-
final var optionsPrefix = line.substring(backward, i);
879+
var optionsPrefix = line.substring(backward, i);
859880
var options = parseOptions(line.substring(i + 1, end).strip());
860881
if (start < backward) {
861882
flushText(elements, line.substring(start, backward));
862883
}
863884

885+
if (optionsPrefix.startsWith("__") && line.substring(end).startsWith("]__")) {
886+
optionsPrefix = optionsPrefix.substring(2);
887+
offset = 2;
888+
}
889+
864890
final int macroMarker = optionsPrefix.indexOf(":");
865891
if (macroMarker > 0 && !isLink(optionsPrefix)) {
866892
final boolean inlined = optionsPrefix.length() <= macroMarker + 1 || optionsPrefix.charAt(macroMarker + 1) != ':';
@@ -931,8 +957,8 @@ enclosingDocument, new Reader(List.of(label)),
931957
linkLabel,
932958
removeEmptyKey(options)));
933959
}
934-
i = end;
935-
start = end + 1;
960+
i = end + offset;
961+
start = i + 1;
936962
continue;
937963
}
938964

@@ -1485,17 +1511,25 @@ private void flushOption(final String defaultKey,
14851511
final var keyValue = key.toString();
14861512
if (value.isEmpty()) {
14871513
if (keyValue.startsWith(".")) {
1488-
collector.put("role", keyValue.substring(1));
1514+
collector.put("role", keyValue.substring(1).replace('.', ' '));
14891515
} else if (keyValue.startsWith("#")) {
14901516
collector.put("id", keyValue.substring(1));
14911517
} else {
1492-
collector.put(defaultKey, keyValue);
1518+
collector.put(defaultKey, dropLegacyPassthroughMarkers(keyValue));
14931519
}
14941520
} else {
1495-
collector.put(keyValue, value.toString());
1521+
collector.put(
1522+
keyValue,
1523+
dropLegacyPassthroughMarkers(value.toString()));
14961524
}
14971525
}
14981526

1527+
private static String dropLegacyPassthroughMarkers(final String v) {
1528+
return v.length() > 3 && v.startsWith("$$") && v.endsWith("$$") ?
1529+
v.substring(2, v.length() - 2) :
1530+
v;
1531+
}
1532+
14991533
private Element parseSection(final Path enclosingDocument, final Reader reader, final Map<String, String> options,
15001534
final ContentResolver resolver, final Map<String, String> currentAttributes) {
15011535
final var title = reader.skipCommentsAndEmptyLines();

asciidoc-java/src/test/java/io/yupiik/asciidoc/parser/ParserTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,51 @@
6060
import static java.util.Map.entry;
6161
import static java.util.stream.Collectors.toMap;
6262
import static org.junit.jupiter.api.Assertions.assertEquals;
63+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
6364

6465
class ParserTest {
66+
@Test
67+
public void doubleDollarAndQuotingForMacro() {
68+
final var body = new Parser()
69+
.parseBody("xref:foo[$$bar$$]\n__link:https://foo.bar[$$dummy$$]__", new Parser.ParserContext(null));
70+
assertEquals(
71+
List.of(
72+
new Paragraph(
73+
List.of(
74+
new Macro("xref", "foo", Map.of("", "bar"), true),
75+
new Link("https://foo.bar", new Text(List.of(), "dummy", Map.of("nowrap", "true", "", "dummy")), Map.of("", "dummy", "nowrap", "true"))
76+
), Map.of())),
77+
body.children());
78+
}
79+
80+
// crd-ref-docs uses this kind of formatting
81+
@Test
82+
public void tableWithContinuation() {
83+
var body = new Parser().parseBody(
84+
"""
85+
[cols="20a,50a,15a,15a", options="header"]
86+
|===
87+
| Field | Description | Default | Validation
88+
| *`foo`* string | Foo. | | MaxLength: 10
89+
MinLength: 3
90+
Pattern: `^[A-Z]$`
91+
92+
| *`bar`* string | Bar. | | MaxLength: 10
93+
MinLength: 3
94+
Pattern: `^[A-Z]$`
95+
96+
|===
97+
""",
98+
new Parser.ParserContext(null)
99+
);
100+
assertEquals(1, body.children().size());
101+
final var table = assertInstanceOf(Table.class, body.children().get(0));
102+
assertEquals(3, table.elements().size());
103+
for (final var it : table.elements()) {
104+
assertEquals(4, it.size());
105+
}
106+
}
107+
65108
@Test
66109
void definitionList() {
67110
final var body = new Parser().parseBody(new Reader(List.of("""

0 commit comments

Comments
 (0)