1
0
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:
Jeremy Ruston 2012-12-20 15:07:38 +00:00
parent 360e188e49
commit 055bdd8d8f
8 changed files with 99 additions and 21 deletions

View File

@ -23,25 +23,25 @@ exports.name = "transclude";
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|([^\{\}]+))?\}([^\}]*)\}/mg;
this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|([^\{\}]+))?\}([^\}]*)\}(?:\.(\S+))?(?:\r?\n|$)/mg;
};
exports.parse = function(match,isBlock) {
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];
// Parse any class definitions
var classes = this.parser.parseClasses();
style = this.match[3],
classes = this.match[4];
// Return the transclude widget
var node = {
type: "widget",
tag: "transclude",
attributes: {
target: {type: "string", value: targetTitle}
}
},
isBlock: true
};
if(tooltip) {
node.attributes.tooltip = {type: "string", value: tooltip};
@ -49,8 +49,8 @@ exports.parse = function(match,isBlock) {
if(style) {
node.attributes.style = {type: "string", value: style};
}
if(classes.length > 0) {
node.attributes["class"] = {type: "string", value: classes.join(" ")};
if(classes) {
node.attributes["class"] = {type: "string", value: classes.split(".").join(" ")};
}
return [node];
};

View File

@ -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];
};
})();

View File

@ -45,7 +45,7 @@ var WikiParser = function(vocabulary,type,text,options) {
this.blockRules = this.instantiateRules(this.vocabulary.blockRuleClasses,this.pos);
this.inlineRules = this.instantiateRules(this.vocabulary.inlineRuleClasses,this.pos);
// Parse the text into inline runs or blocks
if(this.type === "text/vnd.tiddlywiki-run") {
if(options.parseAsInline) {
this.tree = this.parseInlineRun();
} else {
this.tree = this.parseBlocks();
@ -218,8 +218,8 @@ WikiParser.prototype.parseInlineRun = function(terminatorRegExp,options) {
WikiParser.prototype.parseInlineRunUnterminated = function(options) {
var tree = [];
// Find the next occurrence of a runrule
var nextMatch = this.findNextMatch(this.runRules,this.pos);
// Find the next occurrence of an inline rule
var nextMatch = this.findNextMatch(this.inlineRules,this.pos);
// Loop around the matches until we've reached the end of the text
while(this.pos < this.sourceLength && nextMatch) {
// Process the text preceding the run rule
@ -230,7 +230,7 @@ WikiParser.prototype.parseInlineRunUnterminated = function(options) {
// Process the run rule
tree.push.apply(tree,nextMatch.rule.parse());
// Look for the next run rule
nextMatch = this.findNextMatch(this.runRules,this.pos);
nextMatch = this.findNextMatch(this.inlineRules,this.pos);
}
// Process the remaining text
if(this.pos < this.sourceLength) {

View File

@ -77,7 +77,7 @@ WidgetRenderer.prototype.render = function(type) {
WidgetRenderer.prototype.renderInDom = function() {
// 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-attr",JSON.stringify(this.attributes));
// Render the widget if we've got one

View File

@ -73,7 +73,7 @@ exports.generateChildNodes = function() {
})) {
templateParseTree = [{type: "text", text: "Tiddler recursion error in transclude widget"}];
} 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 : [];
}
}

View File

@ -363,19 +363,26 @@ exports.new_initParsers = function() {
/*
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) {
return this.vocabulary.parseText(type,text);
exports.new_parseText = function(type,text,options) {
return this.vocabulary.parseText(type,text,options);
};
/*
Parse a tiddler according to its MIME type
*/
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;
return tiddler ? this.getCacheForTiddler(title,"newParseTree",function() {
return self.new_parseText(tiddler.fields.type,tiddler.fields.text);
return tiddler ? this.getCacheForTiddler(title,cacheType,function() {
return self.new_parseText(tiddler.fields.type,tiddler.fields.text,options);
}) : null;
};

View File

@ -22,8 +22,20 @@ var WikiVocabulary = function(options) {
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;

View File

@ -17,6 +17,8 @@ some<br>thing
{{Acknowledgements}width:40;height:50;}.one
And this is an inline transclusion {{Acknowledgements}width:40;height:50;}.one
---
{{{