2012-12-29 22:29:24 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/parsers/wikiparser/rules/typedblock.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: wikirule
|
|
|
|
|
|
|
|
Wiki text rule for typed blocks. For example:
|
|
|
|
|
|
|
|
```
|
|
|
|
$$$.js
|
|
|
|
This will be rendered as JavaScript
|
|
|
|
$$$
|
|
|
|
|
|
|
|
$$$.svg
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="150" height="100">
|
|
|
|
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
|
|
|
|
</svg>
|
|
|
|
$$$
|
|
|
|
|
|
|
|
$$$text/vnd.tiddlywiki>text/html
|
|
|
|
This will be rendered as an //HTML representation// of WikiText
|
|
|
|
$$$
|
|
|
|
```
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var widget = require("$:/core/modules/widgets/widget.js");
|
2013-10-27 22:53:22 +00:00
|
|
|
|
2012-12-29 22:29:24 +00:00
|
|
|
exports.name = "typedblock";
|
|
|
|
exports.types = {block: true};
|
|
|
|
|
|
|
|
exports.init = function(parser) {
|
|
|
|
this.parser = parser;
|
|
|
|
// Regexp to match
|
|
|
|
this.matchRegExp = /\$\$\$([^ >\r\n]*)(?: *> *([^ \r\n]+))?\r?\n/mg;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.parse = function() {
|
2013-12-25 22:06:42 +00:00
|
|
|
var reEnd = /\r?\n\$\$\$\r?(?:\n|$)/mg;
|
2012-12-29 22:29:24 +00:00
|
|
|
// Save the type
|
|
|
|
var parseType = this.match[1],
|
|
|
|
renderType = this.match[2];
|
|
|
|
// Move past the match
|
|
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
|
|
|
// Look for the end of the block
|
|
|
|
reEnd.lastIndex = this.parser.pos;
|
|
|
|
var match = reEnd.exec(this.parser.source),
|
|
|
|
text;
|
|
|
|
// Process the block
|
|
|
|
if(match) {
|
|
|
|
text = this.parser.source.substring(this.parser.pos,match.index);
|
|
|
|
this.parser.pos = match.index + match[0].length;
|
|
|
|
} else {
|
|
|
|
text = this.parser.source.substr(this.parser.pos);
|
|
|
|
this.parser.pos = this.parser.sourceLength;
|
|
|
|
}
|
|
|
|
// Parse the block according to the specified type
|
2013-11-08 08:51:14 +00:00
|
|
|
var parser = this.parser.wiki.parseText(parseType,text,{defaultType: "text/plain"});
|
2012-12-29 22:29:24 +00:00
|
|
|
// If there's no render type, just return the parse tree
|
|
|
|
if(!renderType) {
|
|
|
|
return parser.tree;
|
|
|
|
} else {
|
|
|
|
// Otherwise, render to the rendertype and return in a <PRE> tag
|
2013-10-29 14:51:35 +00:00
|
|
|
var widgetNode = this.parser.wiki.makeWidget(parser),
|
2014-01-15 14:57:35 +00:00
|
|
|
container = $tw.fakeDocument.createElement("div");
|
2013-10-27 22:53:22 +00:00
|
|
|
widgetNode.render(container,null);
|
2014-08-30 19:44:26 +00:00
|
|
|
text = renderType === "text/html" ? container.innerHTML : container.textContent;
|
2012-12-29 22:29:24 +00:00
|
|
|
return [{
|
|
|
|
type: "element",
|
|
|
|
tag: "pre",
|
|
|
|
children: [{
|
|
|
|
type: "text",
|
2013-05-17 09:12:25 +00:00
|
|
|
text: text
|
2012-12-29 22:29:24 +00:00
|
|
|
}]
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|