Skip to content

Commit

Permalink
[minisite] enable to disable interpolation in template substitutor
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Jun 5, 2023
1 parent 4dfbf19 commit f018bcc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions _documentation/src/main/minisite/content/mojos.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ TIP: ensure to not use `1`, `2`, `3`, ... but rather `100`, `200`, ... to easily
* `minisite-index-icon` attribute enables to override font awesome icon (without `fa-` prefix).
* `minisite-index-description` attribute enables to override the text in the index tile for the page entry.

=== Escaping

When you write some documentation in a language close to the minisite interpolation one (mustache for ex), it can be hard to use.
To ease that, you can surround your snippet with `yupiik.minisite:no-interpolate:start` and `yupiik.minisite:no-interpolate:end`, included text will not support interpolations.

=== Synchronize Releases Example

[source,xml]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,26 @@ public String replace(final String source) {
if (source == null) {
return null;
}
final int idx = source.indexOf("yupiik.minisite:no-interpolate:start");
if (idx >= 0) {
final int endIdx = source.indexOf("yupiik.minisite:no-interpolate:end");
if (endIdx > 0) {
return source.substring(0, idx) +
source.substring(idx + "yupiik.minisite:no-interpolate:start".length(), endIdx) +
replace(source.substring(endIdx + "yupiik.minisite:no-interpolate:end".length()));
}
}
final StringBuilder builder = new StringBuilder(source);
if (substitute(builder, 0, source.length(), null) <= 0) {
return source;
return placeholders(source);
}
return replace(builder.toString());
return placeholders(replace(builder.toString()));
}

private String placeholders(final String content) {
return content
// enables to use "{{{" and dont break the output using "{$yupiik.minisite.openbracket$}{{" instead
.replace("{$yupiik.minisite.openbracket$}", "{");
}

private int substitute(final StringBuilder buf, final int offset, final int inLength,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@

class TemplateSubstitutorTest {
@Test
void ignore() {
void openBracket() {
assertEquals(
"code: `{{#json}}{{${.}}}{{/json}}` !",
"code: `{{#json}}{{{.}}}{{/json}}` !",
new TemplateSubstitutor(k -> fail("there should be no interpolation '" + k + "'"))
.replace("code: `{{#json}}{{${.}}}{{/json}}` !"));
.replace("code: `{{#json}}{$yupiik.minisite.openbracket$}{{.}}}{{/json}}` !"));
}

@Test
void noInterpolate() {
assertEquals(
"this is before\n\ncode: `{{#json}}{{{.}}}{{/json}}` !\n\nthis is after",
new TemplateSubstitutor(k -> fail("there should be no interpolation '" + k + "'"))
.replace("this is before\n" +
"yupiik.minisite:no-interpolate:start\n" +
"code: `{{#json}}{{{.}}}{{/json}}` !\n" +
"yupiik.minisite:no-interpolate:end\n" +
"this is after"));
}
}

0 comments on commit f018bcc

Please sign in to comment.