From 52592a4dbcc6a4990442d8755607a12dbc6a0d26 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Wed, 23 Nov 2022 16:30:20 +0000 Subject: [PATCH] Add support for named end markers for procedures, functions and widgets --- .../parsers/wikiparser/rules/fnprocdef.js | 2 +- .../tiddlers/tests/data/procedures/Nested.tid | 20 +++++++++++++++++++ .../tiddlers/pragmas/Pragma_ _function.tid | 4 +++- .../tiddlers/pragmas/Pragma_ _procedure.tid | 18 ++++++++++++++++- .../tiddlers/pragmas/Pragma_ _widget.tid | 4 +++- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 editions/test/tiddlers/tests/data/procedures/Nested.tid diff --git a/core/modules/parsers/wikiparser/rules/fnprocdef.js b/core/modules/parsers/wikiparser/rules/fnprocdef.js index 12ac33cac..5d0a8878b 100644 --- a/core/modules/parsers/wikiparser/rules/fnprocdef.js +++ b/core/modules/parsers/wikiparser/rules/fnprocdef.js @@ -53,7 +53,7 @@ exports.parse = function() { var reEnd; if(this.match[5]) { // If so, the end of the body is marked with \end - reEnd = /(\r?\n\\end[^\S\n\r]*(?:$|\r?\n))/mg; + reEnd = new RegExp("(\\r?\\n\\\\end[^\\S\\n\\r]*(?:" + $tw.utils.escapeRegExp(this.match[2]) + ")?(?:$|\\r?\\n))","mg"); } else { // Otherwise, the end of the definition is marked by the end of the line reEnd = /($|\r?\n)/mg; diff --git a/editions/test/tiddlers/tests/data/procedures/Nested.tid b/editions/test/tiddlers/tests/data/procedures/Nested.tid new file mode 100644 index 000000000..f63c634af --- /dev/null +++ b/editions/test/tiddlers/tests/data/procedures/Nested.tid @@ -0,0 +1,20 @@ +title: Procedures/Nested +description: Nested Procedures +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\whitespace trim +\procedure alpha(x) +\procedure beta(y) +<$text text=<>/> +\end beta +<$transclude $variable="beta" y={{{ [addprefix] }}}/> +\end alpha + +<> ++ +title: ExpectedResult + +

ElephantElephant

\ No newline at end of file diff --git a/editions/tw5.com/tiddlers/pragmas/Pragma_ _function.tid b/editions/tw5.com/tiddlers/pragmas/Pragma_ _function.tid index 24c17d9ea..ffcd76b43 100644 --- a/editions/tw5.com/tiddlers/pragmas/Pragma_ _function.tid +++ b/editions/tw5.com/tiddlers/pragmas/Pragma_ _function.tid @@ -11,9 +11,11 @@ The usual form allows custom functions to span multiple lines: ``` \function ([:],[:]...) -\end +\end [] ``` +Note that the `\end` marker can optionally specify the name of the function to which it relates, enabling function definitions to be nested inside procedures, macros or widget definitions. + There is also a single line form for shorter functions: ``` diff --git a/editions/tw5.com/tiddlers/pragmas/Pragma_ _procedure.tid b/editions/tw5.com/tiddlers/pragmas/Pragma_ _procedure.tid index 948a863dd..becec39f9 100644 --- a/editions/tw5.com/tiddlers/pragmas/Pragma_ _procedure.tid +++ b/editions/tw5.com/tiddlers/pragmas/Pragma_ _procedure.tid @@ -11,9 +11,11 @@ The usual form allows procedures to span multiple lines: ``` \procedure ([:],[:]...) -\end +\end [] ``` +Note that the `\end` marker can optionally specify the name of the procedure to which it relates which allows procedure definitions to be nested. + There is also a single line form for shorter procedures: ``` @@ -37,3 +39,17 @@ Alternatively, the entire definition can be presented on a single line without a ``` \procedure sayhi(name:"Bugs Bunny") Hi, I'm $name$. ``` + +Procedure definitions can be nested by specifying the name of the procedure in the `\end` marker. For example: + +< +\end actions +<$button actions=<>> +$caption$ + +\end special-button + +<> +""">> \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/pragmas/Pragma_ _widget.tid b/editions/tw5.com/tiddlers/pragmas/Pragma_ _widget.tid index bf9c228e6..e7b505c92 100644 --- a/editions/tw5.com/tiddlers/pragmas/Pragma_ _widget.tid +++ b/editions/tw5.com/tiddlers/pragmas/Pragma_ _widget.tid @@ -11,9 +11,11 @@ The usual form allows custom widgets to span multiple lines: ``` \widget ([:],[:]...) -\end +\end [] ``` +Note that the `\end` marker can optionally specify the name of the widget to which it relates which allows widget definitions to be nested. + There is also a single line form for shorter widgets: ```