Skip to content

Commit

Permalink
[asciidoc] simple support of subs=attributes for code/passthrough blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed May 30, 2024
1 parent 5218d3e commit df3e82e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
20 changes: 16 additions & 4 deletions asciidoc-java/src/main/java/io/yupiik/asciidoc/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private PassthroughBlock parsePassthrough(final Reader reader, final Map<String,
final var text = content.toString();
final var actualOpts = options == null ? Map.<String, String>of() : options;
if (!text.contains("include::")) {
return new PassthroughBlock(text, actualOpts);
return new PassthroughBlock(subs(text, actualOpts), actualOpts);
}

final var filtered = Stream.of(text.split("\n"))
Expand All @@ -319,7 +319,19 @@ private PassthroughBlock parsePassthrough(final Reader reader, final Map<String,
}
})
.collect(joining("\n"));
return new PassthroughBlock(filtered, actualOpts);
return new PassthroughBlock(subs(filtered, actualOpts), actualOpts);
}

private String subs(final String value, final Map<String, String> opts) {
final var subs = opts.get("subs");
var out = value;
if (subs == null) {
return out;
}
if (subs.contains("attributes") && !subs.contains("-attributes")) {
out = earlyAttributeReplacement(out, opts);
}
return out;
}

private OpenBlock parseOpenBlock(final Reader reader, final Map<String, String> options,
Expand Down Expand Up @@ -462,7 +474,7 @@ private Code parseCodeBlock(final Reader reader, final Map<String, String> optio

final var contentWithCallouts = parseWithCallouts(code);
if (contentWithCallouts.callOutReferences().isEmpty()) {
return new Code(code, List.of(), codeOptions, false);
return new Code(subs(code, codeOptions), List.of(), codeOptions, false);
}

final var callOuts = new ArrayList<CallOut>(contentWithCallouts.callOutReferences().size());
Expand Down Expand Up @@ -495,7 +507,7 @@ private Code parseCodeBlock(final Reader reader, final Map<String, String> optio
throw new IllegalArgumentException("Invalid callout references (code markers don't match post-code callouts) in snippet:\n" + snippet);
}

return new Code(contentWithCallouts.content(), callOuts, codeOptions, false);
return new Code(subs(contentWithCallouts.content(), codeOptions), callOuts, codeOptions, false);
}

private ContentWithCalloutIndices parseWithCallouts(final String snippet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,32 @@ public record Foo() {
body.children());
}

@Test
void passthroughAttributeSubs() {
final var body = new Parser(Map.of("foo-version", "1")).parseBody(new Reader(List.of("""
[subs=attributes]
++++
<script defer src="/js/test.js?v={foo-version}"></script>
++++
""".split("\n"))), null);
assertEquals(
List.of(new PassthroughBlock("<script defer src=\"/js/test.js?v=1\"></script>", Map.of("subs", "attributes"))),
body.children());
}

@Test
void codeAttributeSubs() {
final var body = new Parser(Map.of("foo-version", "1")).parseBody(new Reader(List.of("""
[subs=attributes]
----
<script defer src="/js/test.js?v={foo-version}"></script>
----
""".split("\n"))), null);
assertEquals(
List.of(new Code("<script defer src=\"/js/test.js?v=1\"></script>\n", List.of(), Map.of("subs", "attributes"), false)),
body.children());
}

@Test
void codeAfterListContinuation() {
final var body = new Parser().parseBody(new Reader(List.of("""
Expand Down

0 comments on commit df3e82e

Please sign in to comment.