mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-30 23:23:02 +00:00 
			
		
		
		
	Support nested macro definitions (#7004)
* First commit * Switched to \end <name> instead of all those repeated backslashes Thanks @kookma. See https://github.com/Jermolene/TiddlyWiki5/pull/7004#issuecomment-1286429236 * Docs update
This commit is contained in:
		| @@ -58,7 +58,7 @@ exports.parse = function() { | |||||||
| 	var reEnd; | 	var reEnd; | ||||||
| 	if(this.match[3]) { | 	if(this.match[3]) { | ||||||
| 		// If so, the end of the body is marked with \end | 		// 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[1]) + ")?(?:$|\\r?\\n))","mg"); | ||||||
| 	} else { | 	} else { | ||||||
| 		// Otherwise, the end of the definition is marked by the end of the line | 		// Otherwise, the end of the definition is marked by the end of the line | ||||||
| 		reEnd = /($|\r?\n)/mg; | 		reEnd = /($|\r?\n)/mg; | ||||||
|   | |||||||
							
								
								
									
										36
									
								
								editions/test/tiddlers/tests/data/macros/NestedMacros.tid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								editions/test/tiddlers/tests/data/macros/NestedMacros.tid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | |||||||
|  | title: Macros/NestedMacros | ||||||
|  | description: Nested Macros | ||||||
|  | type: text/vnd.tiddlywiki-multiple | ||||||
|  | tags: [[$:/tags/wiki-test-spec]] | ||||||
|  |  | ||||||
|  | title: Output | ||||||
|  |  | ||||||
|  | \whitespace trim | ||||||
|  |  | ||||||
|  | \define outer() | ||||||
|  | \whitespace trim | ||||||
|  |  | ||||||
|  | \define middle() | ||||||
|  | \whitespace trim | ||||||
|  |  | ||||||
|  | \define inner() | ||||||
|  | \whitespace trim | ||||||
|  |  | ||||||
|  | Jaguar | ||||||
|  |  | ||||||
|  | \end inner | ||||||
|  |  | ||||||
|  | <<inner>> | ||||||
|  |  | ||||||
|  | \end middle | ||||||
|  |  | ||||||
|  | <<middle>> | ||||||
|  |  | ||||||
|  | \end outer | ||||||
|  |  | ||||||
|  | <<outer>> | ||||||
|  |  | ||||||
|  | + | ||||||
|  | title: ExpectedResult | ||||||
|  |  | ||||||
|  | <p>Jaguar</p> | ||||||
| @@ -1,13 +1,15 @@ | |||||||
| caption: Macro Definitions | caption: Macro Definitions | ||||||
| created: 20150220181617000 | created: 20150220181617000 | ||||||
| modified: 20180820165115455 | modified: 20221022135909352 | ||||||
| tags: WikiText | tags: WikiText | ||||||
| title: Macro Definitions in WikiText | title: Macro Definitions in WikiText | ||||||
| type: text/vnd.tiddlywiki | type: text/vnd.tiddlywiki | ||||||
|  |  | ||||||
| A [[macro|Macros]] is defined using a `\define` [[pragma|Pragma]]. Like any pragma, this can only appear at the start of a tiddler. | A [[macro|Macros]] is defined using a `\define` [[pragma|Pragma]]. Like any pragma, this can only appear at the start of a tiddler. | ||||||
|  |  | ||||||
| The first line of the definition specifies the macro name and any parameters. Each parameter has a name and, optionally, a default value that is used if no value is supplied on a particular call to the macro. The lines that follow contain the text of the macro text (i.e. the snippet represented by the macro name), until `\end` appears on a line by itself: | The first line of the definition specifies the macro name and any parameters. Each parameter has a name and, optionally, a default value that is used if no value is supplied on a particular call to the macro. | ||||||
|  |  | ||||||
|  | The lines that follow contain the text of the macro text (i.e. the snippet represented by the macro name), until `\end` appears on a line by itself: | ||||||
|  |  | ||||||
| <$codeblock code={{$:/editions/tw5.com/macro-examples/say-hi}}/> | <$codeblock code={{$:/editions/tw5.com/macro-examples/say-hi}}/> | ||||||
|  |  | ||||||
| @@ -17,6 +19,20 @@ Alternatively, the entire definition can be presented on a single line without a | |||||||
| \define sayhi(name:"Bugs Bunny") Hi, I'm $name$. | \define sayhi(name:"Bugs Bunny") Hi, I'm $name$. | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  | Macro definitions can be nested by specifying the name of the macro in the `\end` marker. For example: | ||||||
|  |  | ||||||
|  | <<wikitext-example-without-html src:"""\define special-button(caption:"Click me") | ||||||
|  | \define actions() | ||||||
|  | <$action-sendmessage $message="tm-notify" $param="HelloThere"/> | ||||||
|  | \end actions | ||||||
|  | <$button actions=<<actions>>> | ||||||
|  | $caption$ | ||||||
|  | </$button> | ||||||
|  | \end special-button | ||||||
|  |  | ||||||
|  | <<special-button>> | ||||||
|  | """>> | ||||||
|  |  | ||||||
| A more formal [[presentation|Macro Definition Syntax]] of this syntax is also available. | A more formal [[presentation|Macro Definition Syntax]] of this syntax is also available. | ||||||
|  |  | ||||||
| !! Accessing variables and parameters | !! Accessing variables and parameters | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jeremy Ruston
					Jeremy Ruston