1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-01 01:33:16 +00:00
TiddlyWiki5/core/modules/parsers/wikiparser/rules/macrocallblock.js
Cameron Fischer 45355a7fcf
Wikirules now use better macrocall parser (#5451)
* wikirules now use better macrocall parser

Before, wikirules would use a deficient macrocall parser which couldn't
handle certain types of arguments. Now it uses the same one that the
widget parser uses. Less code!

* style changes and removing weird switch statement

That switch statement made more sense in an earlier iteration.

* comment improvements

* oops, wikirule macrocalls could do ONE thing better

* '=' wasn't allowed for widget macros, but why?

Now they're allowed for both widget macros and macrocall macros.
2021-01-29 13:26:31 +00:00

57 lines
1.2 KiB
JavaScript

/*\
title: $:/core/modules/parsers/wikiparser/rules/macrocallblock.js
type: application/javascript
module-type: wikirule
Wiki rule for block macro calls
```
<<name value value2>>
```
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "macrocallblock";
exports.types = {block: true};
exports.init = function(parser) {
this.parser = parser;
};
exports.findNextMatch = function(startPos) {
var nextStart = startPos;
// Try parsing at all possible macrocall openers until we match
while((nextStart = this.parser.source.indexOf("<<",nextStart)) >= 0) {
var nextCall = $tw.utils.parseMacroInvocation(this.parser.source,nextStart);
if(nextCall) {
var c = this.parser.source.charAt(nextCall.end);
// Ensure EOL after parsed macro
// If we didn't need to support IE, we'd just use /(?:\r?\n|$)/ym
if ((c === "") || (c === "\n") || ((c === "\r") && this.parser.source.charAt(nextCall.end+1) === "\n")) {
this.nextCall = nextCall;
return nextStart;
}
}
nextStart += 2;
}
return undefined;
};
/*
Parse the most recent match
*/
exports.parse = function() {
var call = this.nextCall;
call.isBlock = true;
this.nextCall = null;
this.parser.pos = call.end;
return [call];
};
})();