Anki template regex helper updates (#1934)

* Update regexReplace and regexMatch to support content arguments

* Update documentation
This commit is contained in:
toasted-nutbread 2021-09-05 22:59:46 -04:00 committed by GitHub
parent b988417031
commit c91831a817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 10 deletions

View File

@ -146,7 +146,8 @@ Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScr
<details> <details>
<summary>Syntax:</summary> <summary>Syntax:</summary>
<code>{{#regexReplace <i>regex</i> <i>replacement</i> <i>[flags]</i>}}<i>text-to-modify</i>{{/regexReplace}}</code> <code>{{#regexReplace <i>regex</i> <i>replacement</i> <i>[flags]</i>}}<i>text-to-modify</i>{{/regexReplace}}</code><br>
<code>{{#regexReplace <i>regex</i> <i>replacement</i> <i>[flags]</i> <i>[text-to-modify]...</i>}}{{/regexReplace}}</code><br>
* _`regex`_ <br> * _`regex`_ <br>
The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor. The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
@ -156,6 +157,7 @@ Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScr
Optional flags to pass to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor. Optional flags to pass to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
* _`text-to-modify`_ <br> * _`text-to-modify`_ <br>
The text that the regular expression is applied to. The text that the regular expression is applied to.
If multiple arguments are present, they are all concatenated.
</details> </details>
<details> <details>
<summary>Example:</summary> <summary>Example:</summary>
@ -178,7 +180,8 @@ Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScr
<details> <details>
<summary>Syntax:</summary> <summary>Syntax:</summary>
<code>{{#regexMatch <i>regex</i> <i>[flags]</i>}}<i>text-to-modify</i>{{/regexMatch}}</code> <code>{{#regexMatch <i>regex</i> <i>[flags]</i>}}<i>text-to-modify</i>{{/regexMatch}}</code><br>
<code>{{#regexMatch <i>regex</i> <i>[flags]</i> <i>[text-to-modify]...</i>}}{{/regexMatch}}</code><br>
* _`regex`_ <br> * _`regex`_ <br>
The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor. The raw string used to create the regular expression. This value is passed to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
@ -186,6 +189,7 @@ Uses a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScr
Optional flags to pass to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor. Optional flags to pass to the [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) constructor.
* _`text-to-modify`_ <br> * _`text-to-modify`_ <br>
The text that the regular expression is applied to. The text that the regular expression is applied to.
If multiple arguments are present, they are all concatenated.
</details> </details>
<details> <details>
<summary>Example:</summary> <summary>Example:</summary>

View File

@ -207,15 +207,20 @@ class AnkiTemplateRenderer {
_regexReplace(context, ...args) { _regexReplace(context, ...args) {
// Usage: // Usage:
// {{#regexReplace regex string [flags]}}content{{/regexReplace}} // {{#regexReplace regex string [flags] [content]...}}content{{/regexReplace}}
// regex: regular expression string // regex: regular expression string
// string: string to replace // string: string to replace
// flags: optional flags for regular expression // flags: optional flags for regular expression
// e.g. "i" for case-insensitive, "g" for replace all // e.g. "i" for case-insensitive, "g" for replace all
let value = args[args.length - 1].fn(context); const argCount = args.length - 1;
if (args.length >= 3) { const options = args[argCount];
let value = options.fn(context);
if (argCount > 3) {
value = `${args.slice(3).join('')}${value}`;
}
if (argCount > 1) {
try { try {
const flags = args.length > 3 ? args[2] : 'g'; const flags = argCount > 2 ? args[2] : 'g';
const regex = new RegExp(args[0], flags); const regex = new RegExp(args[0], flags);
value = value.replace(regex, args[1]); value = value.replace(regex, args[1]);
} catch (e) { } catch (e) {
@ -227,14 +232,19 @@ class AnkiTemplateRenderer {
_regexMatch(context, ...args) { _regexMatch(context, ...args) {
// Usage: // Usage:
// {{#regexMatch regex [flags]}}content{{/regexMatch}} // {{#regexMatch regex [flags] [content]...}}content{{/regexMatch}}
// regex: regular expression string // regex: regular expression string
// flags: optional flags for regular expression // flags: optional flags for regular expression
// e.g. "i" for case-insensitive, "g" for match all // e.g. "i" for case-insensitive, "g" for match all
let value = args[args.length - 1].fn(context); const argCount = args.length - 1;
if (args.length >= 2) { const options = args[argCount];
let value = options.fn(context);
if (argCount > 2) {
value = `${args.slice(2).join('')}${value}`;
}
if (argCount > 0) {
try { try {
const flags = args.length > 2 ? args[1] : ''; const flags = argCount > 1 ? args[1] : '';
const regex = new RegExp(args[0], flags); const regex = new RegExp(args[0], flags);
const parts = []; const parts = [];
value.replace(regex, (g0) => parts.push(g0)); value.replace(regex, (g0) => parts.push(g0));