1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 12:07:19 +00:00

Amended macro mechanism so that the same wikitext rule can act as both a run rule and a block rule

This commit is contained in:
Jeremy Ruston 2012-05-27 00:22:58 +01:00
parent 9d90c8fd78
commit 5738d146e0
10 changed files with 59 additions and 39 deletions

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/blockrules/class.js
type: application/javascript
module-type: wikitextblockrule
module-type: wikirule
Wiki text block rule for assigning classes to paragraphs and other blocks
@ -14,6 +14,8 @@ Wiki text block rule for assigning classes to paragraphs and other blocks
exports.name = "class";
exports.blockParser = true;
exports.regExpString = "\\{\\{(?:[^\\{\\r\\n]*)\\{$";
exports.parse = function(match) {

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/blockrules/heading.js
type: application/javascript
module-type: wikitextblockrule
module-type: wikirule
Wiki text block rule for headings
@ -14,6 +14,8 @@ Wiki text block rule for headings
exports.name = "heading";
exports.blockParser = true;
exports.regExpString = "!{1,6}";
exports.parse = function(match) {

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/blockrules/html.js
type: application/javascript
module-type: wikitextblockrule
module-type: wikirule
Wiki text block rule for block level HTML elements
@ -14,6 +14,9 @@ Wiki text block rule for block level HTML elements
exports.name = "html";
exports.blockParser = true;
exports.runParser = true;
exports.regExpString = "<[A-Za-z]+\\s*[^>]*>";
exports.parse = function(match) {

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/blockrules/list.js
type: application/javascript
module-type: wikitextblockrule
module-type: wikirule
Wiki text block rule for lists.
@ -16,6 +16,8 @@ Wiki text block rule for lists.
exports.name = "list";
exports.blockParser = true;
exports.regExpString = "[\\*#;:]+";
var listTypes = {

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/blockrules/rule.js
type: application/javascript
module-type: wikitextblockrule
module-type: wikirule
Wiki text block rule for rules
@ -14,6 +14,8 @@ Wiki text block rule for rules
exports.name = "rule";
exports.blockParser = true;
exports.regExpString = "-{3,}\r?\n";
exports.parse = function(match) {

View File

@ -45,13 +45,13 @@ WikiTextRenderer.prototype.parseBlock = function() {
return [];
}
// Look for a block rule
this.parser.blockRules.regExp.lastIndex = this.pos;
var match = this.parser.blockRules.regExp.exec(this.source);
if(this.parser.blockRules.rules.length && match && match.index === this.pos) {
this.parser.blockRegExp.lastIndex = this.pos;
var match = this.parser.blockRegExp.exec(this.source);
if(this.parser.blockRules.length && match && match.index === this.pos) {
var rule;
for(var t=0; t<this.parser.blockRules.rules.length; t++) {
for(var t=0; t<this.parser.blockRules.length; t++) {
if(match[t+1]) {
rule = this.parser.blockRules.rules[t];
rule = this.parser.blockRules[t];
}
}
return rule ? rule.parse.call(this,match) : [];
@ -82,8 +82,8 @@ WikiTextRenderer.prototype.parseRun = function(terminatorRegExp) {
terminatorRegExp.lastIndex = this.pos;
var terminatorMatch = terminatorRegExp.exec(this.source);
// Find the next occurrence of a runrule
this.parser.runRules.regExp.lastIndex = this.pos;
var runRuleMatch = this.parser.runRules.regExp.exec(this.source);
this.parser.runRegExp.lastIndex = this.pos;
var runRuleMatch = this.parser.runRegExp.exec(this.source);
// Loop around until we've reached the end of the text
while(this.pos < this.sourceLength && (terminatorMatch || runRuleMatch)) {
// Return if we've found the terminator, and it precedes any run rule match
@ -105,17 +105,17 @@ WikiTextRenderer.prototype.parseRun = function(terminatorRegExp) {
}
// Process the run rule
var rule;
for(var t=0; t<this.parser.runRules.rules.length; t++) {
for(var t=0; t<this.parser.runRules.length; t++) {
if(runRuleMatch[t+1]) {
rule = this.parser.runRules.rules[t];
rule = this.parser.runRules[t];
}
}
if(rule) {
tree.push.apply(tree,rule.parse.call(this,runRuleMatch));
}
// Look for the next run rule
this.parser.runRules.regExp.lastIndex = this.pos;
runRuleMatch = this.parser.runRules.regExp.exec(this.source);
this.parser.runRegExp.lastIndex = this.pos;
runRuleMatch = this.parser.runRegExp.exec(this.source);
}
}
// Process the remaining text
@ -149,10 +149,25 @@ WikiTextRenderer.prototype.parseClassedRun = function(terminatorRegExp) {
The wikitext parser assembles the rules and uses the wikitext renderer to do the parsing
*/
var WikiTextParser = function(options) {
this.wiki = options.wiki;
// Assemble the rule regexps
this.blockRules = this.getRules("wikitextblockrule");
this.runRules = this.getRules("wikitextrunrule");
this.wiki = options.wiki;
// Assemble the rule regexps
this.blockRules = [];
this.runRules = [];
var blockRegExpStrings = [],
runRegExpStrings = [],
rules = ($tw.plugins.moduleTypes.wikirule || []).slice(0);
for(var t=0; t<rules.length; t++) {
if(rules[t].blockParser) {
this.blockRules.push(rules[t]);
blockRegExpStrings.push("(" + rules[t].regExpString + ")");
}
if(rules[t].runParser) {
this.runRules.push(rules[t]);
runRegExpStrings.push("(" + rules[t].regExpString + ")");
}
}
this.blockRegExp = new RegExp(blockRegExpStrings.join("|"),"mg");
this.runRegExp = new RegExp(runRegExpStrings.join("|"),"mg");
};
/*
@ -165,21 +180,6 @@ WikiTextParser.prototype.parse = function(type,text) {
});
};
/*
Merge all the rule regexp strings into a single regexp
*/
WikiTextParser.prototype.getRules = function(moduleType) {
var rules = ($tw.plugins.moduleTypes[moduleType] || []).slice(0),
regExpStrings = [];
for(var t=0; t<rules.length; t++) {
regExpStrings.push("(" + rules[t].regExpString + ")");
}
return {
regExp: new RegExp(regExpStrings.join("|"),"mg"),
rules: rules
};
};
exports["text/x-tiddlywiki-new"] = WikiTextParser;
})();

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/runrules/macro.js
type: application/javascript
module-type: wikitextrunrule
module-type: wikirule
Wiki text run rule for pretty links
@ -14,6 +14,9 @@ Wiki text run rule for pretty links
exports.name = "macro";
exports.runParser = true;
exports.blockParser = true;
exports.regExpString = "<<";
exports.parse = function(match) {

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/runrules/prettylink.js
type: application/javascript
module-type: wikitextrunrule
module-type: wikirule
Wiki text run rule for pretty links
@ -14,6 +14,8 @@ Wiki text run rule for pretty links
exports.name = "prettylink";
exports.runParser = true;
exports.regExpString = "\\[\\[";
exports.parse = function(match) {

View File

@ -1,7 +1,7 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/runrules/wikilink.js
type: application/javascript
module-type: wikitextrunrule
module-type: wikirule
Wiki text run rule for wiki links.
@ -14,6 +14,8 @@ Wiki text run rule for wiki links.
exports.name = "wikilink";
exports.runParser = true;
var textPrimitives = {
upperLetter: "[A-Z\u00c0-\u00de\u0150\u0170]",
lowerLetter: "[a-z0-9_\\-\u00df-\u00ff\u0151\u0171]",

View File

@ -7,6 +7,8 @@ HelloThere
One two three four. With a link to HelloThere. And a link to TiddlyWiki and TiddlyWiki5. And a suppressed link to ~HelloThere. And now a [[pretty link|HelloThere]] and another pretty link: [[Introduction]].
Here's a paragraph with an embedded macro <<image "Motovun Jack.jpg">> and that was it.
Here is a macro <<list all>>
! This is a new heading
@ -75,4 +77,4 @@ This is my nice and simple block of text. HelloThere
And another:
<article class="hello" mysignal data-thing='Nothing'>This time the text is all squashed up, without line breaks</article>
<article class="hello" mysignal data-thing='Nothing'><div class="another" mysignal2 data-thing='NothingElse'>This time the text is all squashed up, without line breaks</div></article>