1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 23:03:15 +00:00

More parser rules

This commit is contained in:
Jeremy Ruston 2012-12-15 17:35:35 +00:00
parent 0c3c2eeec6
commit a63c7a8822
4 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,33 @@
/*\
title: $:/core/modules/parsers/wikiparser/rules/block/rule.js
type: application/javascript
module-type: wikiblockrule
Wiki text block rule for rules. For example:
{{{
---
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "rule";
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /-{3,}\r?\n/mg;
};
exports.parse = function(match,isBlock) {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
return [{type: "element", tag: "hr"}];
};
})();

View File

@ -0,0 +1,58 @@
/*\
title: $:/core/modules/parsers/wikiparser/rules/block/transcludeblock.js
type: application/javascript
module-type: wikiblockrule
Wiki text rule for block-level transclusion. For example:
{{{
{{MyTiddler}}
{{MyTiddler|tooltip}}
{{MyTiddler}width:40;height:50;}.class.class
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "transclude";
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|([^\{\}]+))?\}([^\}]*)\}/mg;
};
exports.parse = function(match,isBlock) {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Get the match details
var targetTitle = this.match[1],
tooltip = this.match[2],
style = this.match[3];
// Parse any class definitions
var classes = this.parser.parseClasses();
// Return the transclude widget
var node = {
type: "widget",
tag: "transclude",
attributes: {
target: {type: "string", value: targetTitle}
}
};
if(tooltip) {
node.attributes.tooltip = {type: "string", value: tooltip};
}
if(style) {
node.attributes.style = {type: "string", value: style};
}
if(classes.length > 0) {
node.attributes["class"] = {type: "string", value: classes.join(" ")};
}
return [node];
};
})();

View File

@ -0,0 +1,51 @@
/*\
title: $:/core/modules/parsers/wikiparser/rules/run/extlink.js
type: application/javascript
module-type: wikirunrule
Wiki text run rule for external links. For example:
{{{
An external link: http://www.tiddlywiki.com/
A suppressed external link: ~http://www.tiddlyspace.com/
}}}
External links can be suppressed by preceding them with `~`.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "extlink";
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /~?(?:file|http|https|mailto|ftp|irc|news|data):[^\s'"]+(?:\/|\b)/mg;
};
exports.parse = function() {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Create the link unless it is suppressed
if(this.match[0].substr(0,1) === "~") {
return [{type: "text", text: this.match[0].substr(1)}];
} else {
return [{
type: "widget",
tag: "link",
attributes: {
to: {type: "string", value: this.match[0]}
},
children: [{
type: "text", text: this.match[0]
}]
}];
}
};
})();

View File

@ -0,0 +1,47 @@
/*\
title: $:/core/modules/parsers/wikiparser/rules/run/prettylink.js
type: application/javascript
module-type: wikirunrule
Wiki text run rule for pretty links. For example:
{{{
[[Introduction]]
[[Link description|TiddlerTitle]]
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "prettylink";
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /\[\[(.*?)(?:\|(.*?))?\]\]/mg;
};
exports.parse = function() {
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Process the link
var text = this.match[1],
link = this.match[2] || text;
return [{
type: "widget",
tag: "link",
attributes: {
to: {type: "string", value: link}
},
children: [{
type: "text", text: text
}]
}];
};
})();