Skip to content

Instantly share code, notes, and snippets.

@Arlen22
Last active August 29, 2015 14:00
Show Gist options
  • Save Arlen22/11149813 to your computer and use it in GitHub Desktop.
Save Arlen22/11149813 to your computer and use it in GitHub Desktop.
macrodef - macro definition tiddler
/*\
title: $:/core/modules/parsers/wikiparser/rules/macrodef.js
type: application/javascript
module-type: wikirule
Wiki pragma rule for macro definitions
```
\define name(param:defaultvalue,param2:defaultvalue)
definition text, including $param$ markers
\end
\define {definition tiddler} name(param:defaultvalue,param2:defaultvalue)
\define name(param:defaultvalue,param2:defaultvalue)definition text, including $param$ markers
```
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "macrodef";
exports.types = {pragma: true};
/*
Instantiate parse rule
*/
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /^\\define\s+(?:{([^}]*)}\s+){0,1}([^(\s]+)\(\s*([^)]*)\)(\r?\n)?/mg;
};
/*
Parse the most recent match
*/
exports.parse = function() {
// Move past the macro name and parameters
this.parser.pos = this.matchRegExp.lastIndex;
// Parse the parameters
var paramString = this.match[3],
params = [];
if(paramString !== "") {
var reParam = /\s*([A-Za-z0-9\-_]+)(?:\s*:\s*(?:"([^"]*)"|'([^']*)'|\[\[([^\]]*)\]\]|([^"'\s]+)))?/mg,
paramMatch = reParam.exec(paramString);
while(paramMatch) {
// Save the parameter details
var paramInfo = {name: paramMatch[1]},
defaultValue = paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5];
if(defaultValue) {
paramInfo["default"] = defaultValue;
}
params.push(paramInfo);
// Look for the next parameter
paramMatch = reParam.exec(paramString);
}
}
// Is this a multiline definition?
var reEnd;
if(!this.match[1] && this.match[4]) {
// If so, the end of the body is marked with \end
reEnd = /(\r?\n\\end\r?\n)/mg;
} else {
// Otherwise, the end of the definition is marked by the end of the line
reEnd = /(\r?\n)/mg;
}
// Find the end of the definition
reEnd.lastIndex = this.parser.pos;
var text,
endMatch = reEnd.exec(this.parser.source);
if(endMatch && this.match[1]){
text = this.parser.wiki.getTiddler(this.match[1].trim()).fields.text;
this.parser.pos = endMatch.index + endMatch[0].length;
} else if(endMatch) {
text = this.parser.source.substring(this.parser.pos,endMatch.index);
this.parser.pos = endMatch.index + endMatch[0].length;
} else {
// We didn't find the end of the definition, so we'll make it blank
text = "";
}
// Save the macro definition
return [{
type: "macrodef",
name: this.match[2],
params: params,
text: text
}];
};
})();
@Arlen22
Copy link
Author

Arlen22 commented Apr 26, 2014

The changes to this gist are in the public domain. The starting copy is from https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/parsers/wikiparser/rules/macrodef.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment