mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-24 17:10:29 +00:00
Added inline transclusion as well as block transclusion
This commit is contained in:
parent
360e188e49
commit
055bdd8d8f
@ -23,25 +23,25 @@ exports.name = "transclude";
|
|||||||
exports.init = function(parser) {
|
exports.init = function(parser) {
|
||||||
this.parser = parser;
|
this.parser = parser;
|
||||||
// Regexp to match
|
// Regexp to match
|
||||||
this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|([^\{\}]+))?\}([^\}]*)\}/mg;
|
this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|([^\{\}]+))?\}([^\}]*)\}(?:\.(\S+))?(?:\r?\n|$)/mg;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.parse = function(match,isBlock) {
|
exports.parse = function() {
|
||||||
// Move past the match
|
// Move past the match
|
||||||
this.parser.pos = this.matchRegExp.lastIndex;
|
this.parser.pos = this.matchRegExp.lastIndex;
|
||||||
// Get the match details
|
// Get the match details
|
||||||
var targetTitle = this.match[1],
|
var targetTitle = this.match[1],
|
||||||
tooltip = this.match[2],
|
tooltip = this.match[2],
|
||||||
style = this.match[3];
|
style = this.match[3],
|
||||||
// Parse any class definitions
|
classes = this.match[4];
|
||||||
var classes = this.parser.parseClasses();
|
|
||||||
// Return the transclude widget
|
// Return the transclude widget
|
||||||
var node = {
|
var node = {
|
||||||
type: "widget",
|
type: "widget",
|
||||||
tag: "transclude",
|
tag: "transclude",
|
||||||
attributes: {
|
attributes: {
|
||||||
target: {type: "string", value: targetTitle}
|
target: {type: "string", value: targetTitle}
|
||||||
}
|
},
|
||||||
|
isBlock: true
|
||||||
};
|
};
|
||||||
if(tooltip) {
|
if(tooltip) {
|
||||||
node.attributes.tooltip = {type: "string", value: tooltip};
|
node.attributes.tooltip = {type: "string", value: tooltip};
|
||||||
@ -49,8 +49,8 @@ exports.parse = function(match,isBlock) {
|
|||||||
if(style) {
|
if(style) {
|
||||||
node.attributes.style = {type: "string", value: style};
|
node.attributes.style = {type: "string", value: style};
|
||||||
}
|
}
|
||||||
if(classes.length > 0) {
|
if(classes) {
|
||||||
node.attributes["class"] = {type: "string", value: classes.join(" ")};
|
node.attributes["class"] = {type: "string", value: classes.split(".").join(" ")};
|
||||||
}
|
}
|
||||||
return [node];
|
return [node];
|
||||||
};
|
};
|
||||||
|
@ -0,0 +1,57 @@
|
|||||||
|
/*\
|
||||||
|
title: $:/core/modules/parsers/wikiparser/rules/inline/transcludeinline.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: wiki-inline-rule
|
||||||
|
|
||||||
|
Wiki text rule for inline-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 = /\{\{([^\{\}\|]+)(?:\|([^\{\}]+))?\}([^\}]*)\}(?:\.(\S+))?/mg;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.parse = function() {
|
||||||
|
// 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],
|
||||||
|
classes = this.match[4];
|
||||||
|
// 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) {
|
||||||
|
node.attributes["class"] = {type: "string", value: classes.split(".").join(" ")};
|
||||||
|
}
|
||||||
|
return [node];
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
@ -45,7 +45,7 @@ var WikiParser = function(vocabulary,type,text,options) {
|
|||||||
this.blockRules = this.instantiateRules(this.vocabulary.blockRuleClasses,this.pos);
|
this.blockRules = this.instantiateRules(this.vocabulary.blockRuleClasses,this.pos);
|
||||||
this.inlineRules = this.instantiateRules(this.vocabulary.inlineRuleClasses,this.pos);
|
this.inlineRules = this.instantiateRules(this.vocabulary.inlineRuleClasses,this.pos);
|
||||||
// Parse the text into inline runs or blocks
|
// Parse the text into inline runs or blocks
|
||||||
if(this.type === "text/vnd.tiddlywiki-run") {
|
if(options.parseAsInline) {
|
||||||
this.tree = this.parseInlineRun();
|
this.tree = this.parseInlineRun();
|
||||||
} else {
|
} else {
|
||||||
this.tree = this.parseBlocks();
|
this.tree = this.parseBlocks();
|
||||||
@ -218,8 +218,8 @@ WikiParser.prototype.parseInlineRun = function(terminatorRegExp,options) {
|
|||||||
|
|
||||||
WikiParser.prototype.parseInlineRunUnterminated = function(options) {
|
WikiParser.prototype.parseInlineRunUnterminated = function(options) {
|
||||||
var tree = [];
|
var tree = [];
|
||||||
// Find the next occurrence of a runrule
|
// Find the next occurrence of an inline rule
|
||||||
var nextMatch = this.findNextMatch(this.runRules,this.pos);
|
var nextMatch = this.findNextMatch(this.inlineRules,this.pos);
|
||||||
// Loop around the matches until we've reached the end of the text
|
// Loop around the matches until we've reached the end of the text
|
||||||
while(this.pos < this.sourceLength && nextMatch) {
|
while(this.pos < this.sourceLength && nextMatch) {
|
||||||
// Process the text preceding the run rule
|
// Process the text preceding the run rule
|
||||||
@ -230,7 +230,7 @@ WikiParser.prototype.parseInlineRunUnterminated = function(options) {
|
|||||||
// Process the run rule
|
// Process the run rule
|
||||||
tree.push.apply(tree,nextMatch.rule.parse());
|
tree.push.apply(tree,nextMatch.rule.parse());
|
||||||
// Look for the next run rule
|
// Look for the next run rule
|
||||||
nextMatch = this.findNextMatch(this.runRules,this.pos);
|
nextMatch = this.findNextMatch(this.inlineRules,this.pos);
|
||||||
}
|
}
|
||||||
// Process the remaining text
|
// Process the remaining text
|
||||||
if(this.pos < this.sourceLength) {
|
if(this.pos < this.sourceLength) {
|
||||||
|
@ -77,7 +77,7 @@ WidgetRenderer.prototype.render = function(type) {
|
|||||||
|
|
||||||
WidgetRenderer.prototype.renderInDom = function() {
|
WidgetRenderer.prototype.renderInDom = function() {
|
||||||
// Create the wrapper element
|
// Create the wrapper element
|
||||||
this.domNode = document.createElement("span");
|
this.domNode = document.createElement(this.parseTreeNode.isBlock ? "div" : "span");
|
||||||
this.domNode.setAttribute("data-widget-type",this.parseTreeNode.tag);
|
this.domNode.setAttribute("data-widget-type",this.parseTreeNode.tag);
|
||||||
this.domNode.setAttribute("data-widget-attr",JSON.stringify(this.attributes));
|
this.domNode.setAttribute("data-widget-attr",JSON.stringify(this.attributes));
|
||||||
// Render the widget if we've got one
|
// Render the widget if we've got one
|
||||||
|
@ -73,7 +73,7 @@ exports.generateChildNodes = function() {
|
|||||||
})) {
|
})) {
|
||||||
templateParseTree = [{type: "text", text: "Tiddler recursion error in transclude widget"}];
|
templateParseTree = [{type: "text", text: "Tiddler recursion error in transclude widget"}];
|
||||||
} else {
|
} else {
|
||||||
var parser = this.renderer.renderTree.wiki.new_parseTiddler(this.templateTitle);
|
var parser = this.renderer.renderTree.wiki.new_parseTiddler(this.templateTitle,{parseAsInline: !this.renderer.parseTreeNode.isBlock});
|
||||||
templateParseTree = parser ? parser.tree : [];
|
templateParseTree = parser ? parser.tree : [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -363,19 +363,26 @@ exports.new_initParsers = function() {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Parse a block of text of a specified MIME type
|
Parse a block of text of a specified MIME type
|
||||||
|
type: content type of text to be parsed
|
||||||
|
text: text
|
||||||
|
options: see below
|
||||||
|
Options include:
|
||||||
|
parseAsInline: if true, the text of the tiddler will be parsed as an inline run
|
||||||
*/
|
*/
|
||||||
exports.new_parseText = function(type,text) {
|
exports.new_parseText = function(type,text,options) {
|
||||||
return this.vocabulary.parseText(type,text);
|
return this.vocabulary.parseText(type,text,options);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Parse a tiddler according to its MIME type
|
Parse a tiddler according to its MIME type
|
||||||
*/
|
*/
|
||||||
exports.new_parseTiddler = function(title,options) {
|
exports.new_parseTiddler = function(title,options) {
|
||||||
var tiddler = this.getTiddler(title),
|
options = options || {};
|
||||||
|
var cacheType = options.parseAsInline ? "newInlineParseTree" : "newBlockParseTree",
|
||||||
|
tiddler = this.getTiddler(title),
|
||||||
self = this;
|
self = this;
|
||||||
return tiddler ? this.getCacheForTiddler(title,"newParseTree",function() {
|
return tiddler ? this.getCacheForTiddler(title,cacheType,function() {
|
||||||
return self.new_parseText(tiddler.fields.type,tiddler.fields.text);
|
return self.new_parseText(tiddler.fields.type,tiddler.fields.text,options);
|
||||||
}) : null;
|
}) : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,8 +22,20 @@ var WikiVocabulary = function(options) {
|
|||||||
this.widgetClasses = $tw.modules.createClassesFromModules("widget",$tw.WidgetBase);
|
this.widgetClasses = $tw.modules.createClassesFromModules("widget",$tw.WidgetBase);
|
||||||
};
|
};
|
||||||
|
|
||||||
WikiVocabulary.prototype.parseText = function(type,text) {
|
/*
|
||||||
return new $tw.WikiParser(this,type,text,{wiki: this.wiki});
|
Parse a block of text of a specified MIME type
|
||||||
|
type: content type of text to be parsed
|
||||||
|
text: text
|
||||||
|
options: see below
|
||||||
|
Options include:
|
||||||
|
parseAsInline: if true, the text of the tiddler will be parsed as an inline run
|
||||||
|
*/
|
||||||
|
WikiVocabulary.prototype.parseText = function(type,text,options) {
|
||||||
|
options = options || {};
|
||||||
|
return new $tw.WikiParser(this,type,text,{
|
||||||
|
parseAsInline: options.parseAsInline,
|
||||||
|
wiki: this.wiki
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.WikiVocabulary = WikiVocabulary;
|
exports.WikiVocabulary = WikiVocabulary;
|
||||||
|
@ -17,6 +17,8 @@ some<br>thing
|
|||||||
|
|
||||||
{{Acknowledgements}width:40;height:50;}.one
|
{{Acknowledgements}width:40;height:50;}.one
|
||||||
|
|
||||||
|
And this is an inline transclusion {{Acknowledgements}width:40;height:50;}.one
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
{{{
|
{{{
|
||||||
|
Loading…
Reference in New Issue
Block a user